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
You can refer the below OutSystems forum discussions.
Hope it helps.
https://www.outsystems.com/forums/discussion/75505/open-document-in-new-tab-instead-of-downloading/
https://www.outsystems.com/forums/discussion/73748/pdf-preview-in-a-new-window-reactive/
https://www.outsystems.com/forums/discussion/90584/how-to-view-pdf-in-new-tab/
https://www.outsystems.com/forums/discussion/90599/open-link-in-new-tab/
Thanks,
Sriyamini J
Hi Sriyamini,
Actually this piece of code I was using was from this solutionhttps://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
@Rita Barbosa,
Can you try with the code below. Hope it helps.
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);
This downloads the document, I just want the document to open in a new tab, without downloading it
Hi @Rita Barbosa ,
Refer this link,i hope it will helpful to you,
https://www.outsystems.com/forums/discussion/87562/ultimate-pdf-is-it-possible-to-open-pdf-in-a-new-tab-instead-of-downloading-wit/
Sudha Narayanan
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/
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);
Hope this helps
Thanks
Hi, thank you but this didn't work. And it downloaded a broken file instead of opening in new tab
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:
_____________
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.
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?
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:
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.
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.
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 ?
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:;
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 blob = new Blob([byteArray], { type: 'application/pdf' });
const blobUrl = URL.createObjectURL(blob);
window.open(blobUrl, '_blank');
// *********** END************
Working OAP Example Bellow :
Instead of opening the blob directly, open a new HTML page with an embedded that shows the PDF.
Change your JavaScript like this:
// 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:
Happy Coding