25
Views
12
Comments
How to open pdf, png,jpg, bmp in a new window using javascript?
Question

Is there a way to open a new window, not new tab, using javascript?

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

Hello @Siaming Ming,

Please use Window.open() Method to open a new window.

window.open(fileUrl, '_blank');

Hope it helps.

Thank you.

UserImage.jpg
Siaming Ming

Hi, it opens in new tab instead of new window

2023-06-13 12-29-43
Sakthivel P

Hi @Siaming Ming,

Can you please look into the existing solution for your requirement.

https://www.outsystems.com/forums/discussion/73748/pdf-preview-in-a-new-window-reactive/

UserImage.jpg
Siaming Ming

Hi, doing this results to an empty and loading url:

2023-06-13 12-29-43
Sakthivel P

Is it possible to share the OML for debugging?

2025-03-12 07-08-15
Nilesh Trivedi
UserImage.jpg
Siaming Ming

Hi @Nilesh Trivedi  @Sakthivel P
I am not able to share the oml, however here is my approach


Javascript are changing only based on the application type like image/png to image/jpg.

var byteCharacters = atob($parameters.In1);

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);


$parameters.URL = fileURL;


var pdf_newTab = window.open("$parameters.FileName,'_blank");

pdf_newTab.document.write(

  ""

);


I am sorry if its unclean, still beginner level


2023-06-13 12-29-43
Sakthivel P

It works, directly pass the file URL into the JS parameter node as text.

window.open(  $parameters.FileUrl,  '_blank',  'width=1000,height=800,resizable=yes,scrollbars=yes');

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


U can try with this also. Please write name instead of "_blank".

 window.open($parameters.FileUrl, "myWindow", "width=800,height=600,top=100,left=100,resizable=yes,scrollbars=yes"); 

2024-07-12 05-57-50
Gourav Shrivastava
Champion

Hello @Siaming Ming,

Use the JS code below to open a PDF in a new window, replace google.com to you PDF path

  1. const popup= window.open("https://www.google.com", '_blank', 'width=800,height=600');
  2. if (!popup ) {
  3.   alert('Please allow pop-ups in your browser to open the file.');
  4. }

Thanks

Regards,

Gourav Shrivastava


2024-10-09 04-44-30
Bhanu Pratap

Hi @Siaming Ming,

Below JS is for JPG, JPEG, and PNG image - 

(function () {

    const fileName = $parameters.FileName;

    const fileExtension = $parameters.FileExtension.toLowerCase();

    let base64Data = $parameters.Base64File;

    const fullFileName = `${fileName}`;


    // If base64 string includes data URI prefix, strip it

    if (base64Data.startsWith('data:')) {

        base64Data = base64Data.split(',')[1];

    }


    // Decode base64 to binary

    const byteCharacters = atob(base64Data);

    const byteNumbers = new Array(byteCharacters.length);

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

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

    }

    const byteArray = new Uint8Array(byteNumbers);


    // MIME types

    const mimeTypes = {

        jpg: "image/jpeg",

        jpeg: "image/jpeg",

        png: "image/png"

    };


    const mimeType = mimeTypes[fileExtension] || "application/octet-stream";

    const blob = new Blob([byteArray], { type: mimeType });

    const fileURL = URL.createObjectURL(blob);


    if (["jpg", "jpeg", "png"].includes(fileExtension)) {

        const contentHTML = `

                ${fileName}

                    body {

                        margin: 0;

                        background: #f8f9fa;

                        font-family: Arial, sans-serif;

                    }

                    .top-bar {

                        display: flex;

                        justify-content: space-between;

                        align-items: center;

                        background-color: #ffffff;

                        padding: 10px 20px;

                        box-shadow: 0 2px 5px rgba(0,0,0,0.1);

                    }

                    .filename {

                        font-size: 16px;

                        color: #012E5D;

                        font-weight: bold;

                    }

                    .download-btn {

                        background-color: #012E5D;

                        color: white;

                        text-decoration: none;

                        padding: 8px 16px;

                        border-radius: 4px;

                        font-weight: bold;

                        font-size: 14px;

                    }

                    .image-container {

                        text-align: center;

                        margin-top: 20px;

                    }

                    img {

                        max-width: 95%;

                        height: auto;

                    }

${fullFileName}

                    Download

${FileName}

        `;


        const htmlBlob = new Blob([contentHTML], { type: 'text/html' });

        const htmlBlobURL = URL.createObjectURL(htmlBlob);

        const newTab = window.open(htmlBlobURL, '_blank');

        if (!newTab) {

            alert("Popup blocked! Please allow popups for this site.");

        }

        return;

    }

    alert("Unsupported file type: " + fileExtension);

})();



For PDF use the below Forge Component which also have a view layout - 
pdfjs-viewer-reactive-o11

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