How to send a pdf file directly to the printer using JavaScript?

前端 未结 6 1688
离开以前
离开以前 2020-12-02 08:42

How to send a PDF file directly to the printer using JavaScript?

I found two answers in a forum:



        
6条回答
  •  庸人自扰
    2020-12-02 09:03

    There are two steps you need to take.

    First, you need to put the PDF in an iframe.

      
    

    To print the iframe you can look at the answers here:

    Javascript Print iframe contents only

    If you want to print the iframe automatically after the PDF has loaded, you can add an onload handler to the

    the loader can look like this:

    function isLoaded()
    {
      var pdfFrame = window.frames["pdf"];
      pdfFrame.focus();
      pdfFrame.print();
    }
    

    This will display the browser's print dialog, and then print just the PDF document itself. (I personally use the onload handler to enable a "print" button so the user can decide to print the document, or not).

    I'm using this code pretty much verbatim in Safari and Chrome, but am yet to try it on IE or Firefox.

提交回复
热议问题