简体   繁体   中英

Print PDF file with Javascript in all popular browser

How to print a PDF file using Javascript across all popular browser? I have done quite extensive research on this question and there is no real solutions.

First approach is to use iFrame, however, iFrame is not a good practise. Code is listed as following.

function printIframePdf(){
    window.frames["printf"].focus();
    try {
        window.frames["printf"].print();
    }
    catch(e){
        window.print();
        console.log(e);
    }
}

Second approach is to use <embed> or <object> tag to display pdf and use document.getElementById(pdfID).print() to print. However, Chrome does not support this function. Code is listed as following.

function printObjectPdf() {
    document.getElementById('idPdf').Print();
}

Is there a good solution for this?

Did you consider PDF.js?

https://mozilla.github.io/pdf.js/

This is web standards-based platform.

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