PDF.js How to print a multi-page pdf file?

I am trying to add print functionality to a multi-page PDF embedded in a web page using the PDF.js. This is problematic because I only have one page displayed while the user is viewing it, and the page actually displays as an image in the canvas element.

This question does not help in this case, because it is for a single PDF page where printing the current contents of the canvas is acceptable. Same thing with this question . I also want you to not open the PDF in another tab / window and not tell the user to print, which is detrimental to the goal of embedding it in the page.

Looking through the documentation from Mozilla, I did not find any of my own functions that would simply print PDF, however I will start playing with renderingIntent , which seems to be set to "print".

EDIT: redingIntent does not seem to affect anything, and PDF files are saved the same way, regardless of whether it is set to "display" or "print."

+5
source share
1 answer

Remember that PDF.js is another web page. ATM, at least not in the standard HTML5 APIs, there is no way for a web page to transfer random information directly to printers (but you can click it on the cloud print service) - you can print only what you see. "Cm." It means that in the DOM, and now CSS can be used to hide information for the screen or printer. DOM can also change beforeprint / afterprint events.

In your case, since your PDF view is inline, you need to fake the DOM to have all the PDF pages / canvases on the main web page, make them visible for printing and hide for the screen (see, for example, [4]). It is necessary to solve several different problems that may be off topic: deleting fields and not rasterizing canvas. Firefox works with them through moznomarginboxes [1] and mozPrintCallback [2] - both of them are created in support of PDF.js and are not supported by other browsers. (See also polyfill for the latter [3])

+1
source

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


All Articles