0

I need to build automatic kiosk, where people will get their pdf's printed. I will use firefox with silent.print mode on, but I have trouble getting pdf to print in a page that will be a popup (so creating a popup will print pdf inside).

The thing is, that printing is printing white page because print occurs before pdf is loaded, so I tried adding function that will make printing wait. Results is the same, because waiting function will make generating pdf wait also.

Any ideas how to make this work or find workaround?

<!DOCTYPE html>
<html>
  <head>
    <title>Pdf Result</title>
  </head>
  <body>

    <p>Loading resources....</p>
<-- loading pdf -->
    <div>
        <div>
            <iframe onload="window.print()" id="pdf-js-viewer" src="lib/pdfjs/web/viewer.html?file=test.pdf" title="webviewer" frameborder="0" width="500" height="600"></iframe>
        </div>
    </div>


    <script>
//making script wait
    function wait(ms){
       var start = new Date().getTime();
       var end = start;
       while(end < start + ms) {
         end = new Date().getTime();
      }
    }

    wait(5000);

//printing itself
    document.getElementById("pdf-js-viewer").contentWindow.print(); 
    
    </script>
  </body>
</html>
baron_bartek
  • 1,073
  • 2
  • 20
  • 39
  • I have a feeling that the onload attribute shouldn't be placed in the ` – KIKO Software May 11 '23 at 10:01
  • A better way to wait 5 seconds is to use [setTimeout()](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout), which is an asynchronous function allowing the PDF to load and render. But I still think you need to place the `window.print()` inside the popup. – KIKO Software May 11 '23 at 10:04
  • Yes I have removed print from iframe - same thing. – baron_bartek May 11 '23 at 10:08
  • @KIKO Software This entire page is suppoused to be popup :) (I open it in new window and pdf should print) I will try setTimeout – baron_bartek May 11 '23 at 10:10
  • @KIKO Software THANK YOU it worked just fine. And it was so easy :) const myTimeout = setTimeout(delay_printing, 5000); function delay_printing() { document.getElementById("pdf-js-viewer").contentWindow.print(); } - do you want to post this as Aswear so I could accept it, and you get reputation points? – baron_bartek May 11 '23 at 10:13
  • Yes, it works, but it is not the correct answer. You cannot predict how long it will take for the PDF to load and render. Sure, 5 seconds will be enough in 99.9% of the time, but not always. Please try to put the `window.print()` inside the `lib/pdfjs/web/viewer.html` if at all possible. – KIKO Software May 11 '23 at 10:15
  • Rather than using an arbitrary timeout (which might not be enough in some cases), I'd rather look into handling this via events provided by the library (see duplicate.) – CBroe May 11 '23 at 10:15
  • Or use the advice of CBroe. – KIKO Software May 11 '23 at 10:16
  • Yes I will try these sugessions. Thx – baron_bartek May 11 '23 at 10:17
  • If I may ask: is it possible to use pdf.js with readfile from php somehow? I plan to copy file , print it, delete it. But readfile would be more clean. – baron_bartek May 11 '23 at 10:20
  • Sure, anything is possible, but that doesn't mean it is easy. As the name suggests, pdf.js needs Javascript to render a PDF file, and Javascript runs on a client (= browser), not on the server where PHP runs. There are native PHP libraries that can read and render PDF files. But I am confused. A PDF is a file, and of course `readfile()` can read those. No need to render the PDF at all. – KIKO Software May 11 '23 at 10:25
  • This is more complicated matter - I was using fpdi-1.6.1 to autoprint and yes there was no rendering, but there were problem with newer pdf versions/sign pdfs (sig), so I can with idea to use pdf... unfortunatelly I just checked and my new idea has the same problem (it says file does not exist). But pdf.js build into firefox works just fine..... don't understand it. Crap... I still donk knot how to print pdf :/ – baron_bartek May 11 '23 at 10:46
  • Oh, yes, pdf's are notoriously difficult. They are [basically proprietary](https://www.talkingpdf.org/is-pdf-an-open-standard/). You cannot expect to be able to get free software that will be able to open all PDF files, with the exception of Adobe's PDF reader, of course. – KIKO Software May 11 '23 at 10:52

0 Answers0