0

I need to send base 64 for printing, I need to open the chrome print window, but my code just opens the pdf file in a new window. there is no problem with a regular html page, and when there is a pdf element on the page, it is not displayed

var objbuilder = '';
    objbuilder += ('<object width="100%" height="100%"      data="data:application/pdf;base64,');
    objbuilder += (base64PDF);
    objbuilder += ('" type="application/pdf" class="internal">');
    objbuilder += ('<embed src="data:application/pdf;base64,');
    objbuilder += (base64PDF);
    objbuilder += ('" type="application/pdf" />');
    objbuilder += ('</object>');
var win = window.open("","_blank","titlebar=yes");
        win.document.title = "My Title";
        win.document.write('<html><body>');
        win.document.write(objbuilder);
        win.document.write('</body></html>');
        win.print()

2 Answers2

1
                const binary = atob(pdfSrc.replace(/\s/g, ''));
                const len = binary.length;
                const buffer = new ArrayBuffer(len);
                const view = new Uint8Array(buffer);
                for (let i = 0; i < len; i++) {
                    view[i] = binary.charCodeAt(i);
                }
                const blob = new Blob([view], {type: "application/pdf"});
                const url = URL.createObjectURL(blob);
                const iframe = document.createElement('iframe');
                iframe.style.display = 'none';
                iframe.src = url;
                document.body.appendChild(iframe);
                iframe.contentWindow.print();
0

We define the function that base64 string convert to ArrayBuffer

const base64ToArrayBuffer = (data) => {
    const bString = window.atob(data);
    const bLength = bString.length;
    let bytes = new Uint8Array(bLength);
    for (let i = 0; i < bLength; i++) {
        const ascii = bString.charCodeAt(i);
        bytes[i] = ascii;
    }
    return bytes;
}

 const content = base64ToArrayBuffer(base64PDF);
 const blob = new Blob([content], { type: "application/pdf" });
 const url = window.URL.createObjectURL(blob);

 const iframe = document.createElement('iframe');
 iframe.style.display = 'none';
 iframe.src = url;
 document.body.appendChild(iframe);
 iframe.contentWindow.print();
tiny
  • 131
  • 1
  • 4