简体   繁体   中英

JS Print pdf file

I try to print pdf file in javaScript. I get the file's url from the server.

var iframe = document.createElement('iframe');
                document.body.appendChild(iframe);

                iframe.style.display = 'none';
                iframe.src = urlBaseImage + 'Report//' + result;
                iframe.focus();
                iframe.contentWindow.print();

But he give me empty page, I checked the url and it is really correct. What Can I do? Thanks!

You can use this library, Print.js: http://printjs.crabbly.com/

It is very easy to print PDF files with it.

Just pass the PDF file url to the printJS() function;

For example:

printJS('docs/my_pdf_file.pdf');

 function printDisclosureDocument() {
  var doc = document.getElementById('pdfDocument');
 if (doc == 'undefined' || doc == null) {
    var pdfbox = document.createElement('embed');
    pdfbox.type = 'application/pdf';
    pdfbox.src = 'ShowPDF.aspx?refid=' + $('#MainContent_hdnRefId').val();
    pdfbox.width = '1';
    pdfbox.height = '1';
    pdfbox.id = 'pdfDocument';
    document.body.appendChild(pdfbox);
 }

if (doc != null && doc != 'undefined') {
    //Wait until PDF is ready to print    
    if (typeof doc.print === 'undefined') {
      setTimeout(function () { printDisclosureDocument(); }, 500);
    } else {
      doc.print();
    }
 }
 else {
         setTimeout(function () { printDisclosureDocument(); }, 500);
     }
 }

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