PDF printing using pdf.js

I embed a PDF page in one page using pdf.js and I want to be able to print only PDF, not the entire HTML page.

Is it possible?

+3
source share
3 answers

I finally worked out.

I cannot post my code here, but here is what I did:

I made a PDF of 2 canvases, one small for thumbnail and one huge for printing (hidden). Then I had a print button that opened a new window containing the img tag containing the contents of the huge canvas using toImageURL() . The print() function was called in a new window, and then close() to automatically close it after printing. A.

This led to full-length PDf printing, albeit with a normal no and datestamp page from the browser.

0
source

I preloaded the PDF document onto the canvas using pdf.js.

The canvas contains only one page. So this is what worked for me on one page:

  var canvas = document.getElementById('pdfPage'); var win = window.open('', '', ''); var html = "<img src='" + canvas.toDataURL() + "'>"; win.document.write(html); win.document.close(); win.focus(); win.print(); win.close(); 

I still need to figure out what is needed for multiple pages. If I do, I will edit this answer.

I must say that this approach is not optimal, because it does not print the PDF page "camera ready" or, in other words, in its original form. It prints a pdf page image. The difference is the fields that should not be there, and the header / footer, which should not be, since they are not in the original document. Therefore, I will look for an approach that prints it, as the pdf.js viewer does - it has an original form accurate to an orignal document.

+2
source

We can put the following code at the end of the viewer.js file, which automatically prints pdf:

 (function () { function printWhenReady() { try{ if (PDFViewerApplication.initialized) { window.print(); } else { window.setTimeout(printWhenReady, 3000); } }catch(ex){ window.setTimeout(printWhenReady, 3000); } }; printWhenReady(); })(); 
0
source

Source: https://habr.com/ru/post/1242404/


All Articles