简体   繁体   中英

Cross-browser solution for printing a PDF that's encoded as a data URL

On Firefox, const printWindow = window.open(pdfDataUrl) opens a new window with the PDF loaded and ready to print. However following it with printWindow.print() fails to wait long enough for the PDF to be ready before printing, and printWindow.onload = printWindow.print never fires for some reason.

On Chrome, const printWindow = window.open(pdfDataUrl) doesn't work. Answers to other Stack Overflow questions recommend wrapping the data URL in an iframe. That works, but then calling iframeElement.contentWindow.print() does nothing. As a potential workaround, the user can right-click on the frame contents and click print.

Is there a cross-browser solution to this issue?

You can just execute this on load:

window.onload => function(){
    setTimeout(print,1000);
}
function print(){
    window.print();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM