80
Views
16
Comments
Open pdf in a new tab
Application Type
Reactive
Service Studio Version
11.55.18 (Build 64106)

Hi,

I have to open a pdf in a new tab when clicking an "open document" button, and I found this piece of code to use in a javascript node:


var byteCharacters = atob($parameters.Base64Data);

var byteNumbers = new Array(byteCharacters.length);

for (var i = 0; i < byteCharacters.length; i++) {

    byteNumbers[i] = byteCharacters.charCodeAt(i);

}

var byteArray = new Uint8Array(byteNumbers);

var file = new Blob([byteArray], { type: 'application/pdf' });

var fileURL = URL.createObjectURL(file);


window.open(fileURL, "_blank");


I used it and it worked perfectly. But now for some reason it seems like it is not working properly in Chrome and edge (works as it used to in Safari and Firefox).

Now it shows the document, I can see that the whole document is there, but in a weird way. It opens a full page in a new tab but we can only see part of the document on the top of the page with a scroll bar. (I added an image, I just uploaded a blank document).

Does anyone know why? And what can I do to get the document to be in the full page?


Thank you


image.png
UserImage.jpg
Rita Barbosa

Hi Sriyamini,

Actually this piece of code I was using was from this solution
https://www.outsystems.com/forums/discussion/75505/open-document-in-new-tab-instead-of-downloading/
But it is not working anymore..

None of the others seem to work for me either, but are even worse because they don't even show the document

2025-03-12 07-08-15
Nilesh Trivedi

@Rita Barbosa,

Can you try with the code below. Hope it helps.


var byteCharacters = atob($parameters.Base64Data);

var byteNumbers = new Array(byteCharacters.length);

for (var i = 0; i < byteCharacters.length; i++) {

    byteNumbers[i] = byteCharacters.charCodeAt(i);

}

var byteArray = new Uint8Array(byteNumbers);

var file = new Blob([byteArray], { type: 'application/pdf' });

var fileURL = URL.createObjectURL(file);

var a = document.createElement('a');

a.href = fileURL;

a.target = '_blank';

a.download = $parameters.FileName || 'document.pdf';

document.body.appendChild(a);

a.click();

setTimeout(function() {

    document.body.removeChild(a);

    window.URL.revokeObjectURL(fileURL);

}, 100);

UserImage.jpg
Rita Barbosa

This downloads the document, I just want the document to open in a new tab, without downloading it 

2025-04-14 11-22-14
Aditi Saraswat

Hi @Rita Barbosa,

Please refer to the following discussion 

https://www.outsystems.com/forums/discussion/65561/how-to-preview-a-pdf-word-file-without-download-it/

https://www.outsystems.com/forums/discussion/57001/how-to-preview-a-pdf-in-outsystems-without-downloading-it/

https://www.outsystems.com/forums/discussion/87562/ultimate-pdf-is-it-possible-to-open-pdf-in-a-new-tab-instead-of-downloading-wit/

and you can try with javascript code as well

function base64ToBlob(base64Data, contentType = 'application/pdf', sliceSize = 1024) {

    // Decode base64 string

    const byteCharacters = atob(base64Data);

    const byteArrays = [];

    

    // Process the Base64 string in slices

    for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {

        const slice = byteCharacters.slice(offset, offset + sliceSize);

        const byteNumbers = new Array(slice.length);

        for (let i = 0; i < slice.length; i++) {

            byteNumbers[i] = slice.charCodeAt(i);

        }

        // Convert slice to a Uint8Array and store it

        const byteArray = new Uint8Array(byteNumbers);

        byteArrays.push(byteArray);

    }

    

    // Create a Blob from the byte arrays

    return new Blob(byteArrays, { type: contentType });

}


// Use the helper function to convert Base64 data to a PDF Blob

const pdfBlob = base64ToBlob($parameters.Base64Data, 'application/pdf');


// Generate a URL for the Blob and open it in a new tab

const fileURL = URL.createObjectURL(pdfBlob);

window.open(fileURL, "_blank");


Hope this helps

Thanks

UserImage.jpg
Rita Barbosa

Hi, thank you but this didn't work. And it downloaded a broken file instead of opening in new tab

2026-01-28 16-57-48
Mihai Melencu
Champion

Hi @Rita Barbosa ,

There's no guaranteed way to make a PDF always open in a new tab, it really depends on each user's browser settings. Even if you use target="_blank", browsers like Chrome or Edge might handle it differently. That could be why it’s not working as expected in those browsers. 

Chrome, Edge, Firefox, and Safari can be configured to:

  • Open PDFs in a new tab,
  • Automatically download them,
  • Or use a custom PDF viewer plugin.

_____________

I tested your code in Chrome using a simple PDF, and it worked as expected, the page loaded completely. 

Could you check if you have any browser extensions that might be causing a conflict? 

Also, have you considered using a component to help you with this feature?

_________________

EDIT:

I played a bit with the code and managed somehow to get this result, which is similar to yours:


I updated the code by generating an HTML blob that contains an iframe stretched to 100% width and height, giving you a full-page display. This should ensure that the pdf is displayed properly.

I attached a sample OML with the updated code.

SampleOML_pdf.oml
UserImage.jpg
Rita Barbosa

Hi Mihai, 

Thank you for your response. It still doesn't work for me.. And it is even worse because it doesn't load the document

Could it be because of these errors on the console?

- Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". Either the 'unsafe-inline' keyword, a hash , or a nonce ('nonce-...') is required to enable inline execution.

- Refused to load plugin data from 'blob' because it violates the following Content Security Policy directive: "default-src 'self' gap:". Note that 'object-src' was not explicitly set, so 'default-src' is used as a fallback.

But what can I do to overcome this?

2026-01-28 16-57-48
Mihai Melencu
Champion

Hi Rita,

You need to set your content security policy. You need to allow the rules specified in the error.

style-src 'self' 'sha256-XXrSipEth61aLDyZ8hc7X1RosWbT6mqaHyXQ1GyJjsQ=';

default-src 'self' gap:; 

object-src 'self' blob:;

You can find out how to apply content security policy here: https://success.outsystems.com/documentation/11/security/apply_content_security_policy/ 

___

Also please try your old code and see if you get this error: 

- Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". Either the 'unsafe-inline' keyword, a hash , or a nonce ('nonce-...') is required to enable inline execution. 

UserImage.jpg
Rita Barbosa

Hi Mihai,

Thank you. I'm going to try.

And is that safe?

--

yes, my old code had 2 of these:

-Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". Either the 'unsafe-inline' keyword, a hash, or a nonce ('nonce-...') is required to enable inline execution. Note that hashes do not apply to event handlers, style attributes and javascript: navigations unless the 'unsafe-hashes' keyword is present.

2026-01-28 16-57-48
Mihai Melencu
Champion


Letting URLs that use the blob: scheme through can be dangerous., which could open the door to serious security risks. Also 'unsafe-inline' makes you vulnerable to XSS attacks.

I recommend using the old code and move inline styles to CSS classes.

UserImage.jpg
Rita Barbosa

But the old code also uses the blob.

And how do I do that? How do I apply a css class if a pdf opens in a new page ?

2026-01-28 16-57-48
Mihai Melencu
Champion

The inline error appears to be coming from the screen where you're using the action. I'm not completely sure if that's the root cause. Could you please try using the action on a new blank screen to check? 

Have you also tried updating the Content Security Policy (CSP)? You could adjust it step by step to help identify the exact root cause. 

  frame-src 'self' blob:;

  object-src 'self' blob:;

2023-03-12 08-39-51
Iheb Maatali Riahi

Hi , Use This JS : 

const base64String = $parameters.PDF;

 const byteCharacters = atob(base64String);

const byteNumbers = new Array(byteCharacters.length);

for (let i = 0; i < byteCharacters.length; i++) {

    byteNumbers[i] = byteCharacters.charCodeAt(i)

const byteArray = new Uint8Array(byteNumbers);

const blob = new Blob([byteArray], { type: 'application/pdf' });

const blobUrl = URL.createObjectURL(blob);

window.open(blobUrl, '_blank');

// *********** END************


  • $parameters.PDF => base64PDF : convert binaryData to base64 using BinaryToBase64 from BinaryData Extention

Working OAP Example Bellow : 

PDF Viewer.oap
2018-08-26 20-34-32
Pankaj pant


Instead of opening the blob directly, open a new HTML page with an embedded  that shows the PDF.


Change your JavaScript like this:


var byteCharacters = atob($parameters.Base64Data);

var byteNumbers = new Array(byteCharacters.length);

for (var i = 0; i < byteCharacters.length; i++) {

    byteNumbers[i] = byteCharacters.charCodeAt(i);

}

var byteArray = new Uint8Array(byteNumbers);

var file = new Blob([byteArray], { type: 'application/pdf' });

var fileURL = URL.createObjectURL(file);


// Create HTML to embed the PDF

var html = `

    

      

        

      

    

`;


// Open in new tab

var newTab = window.open();

newTab.document.write(html);

newTab.document.close();


What this does:


  • Creates a temporary new HTML page.
  • Embeds the PDF properly in an <iframe>.
  • The iframe fills 100% width and height.
  • Solves the scroll bar, partial loading issue.

Happy Coding 

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.