0

I'm having difficulties to show print modal passing a pdf file.

    setTimeout(() => {            
        printJS({
        printable: '53544.pdf',
        type: 'pdf',
        showModal: true,    
        fallbackPrintable: () => console.log("FallbackPrintable"),      
        onPrintDialogClose: () => console.log('The print dialog was closed'),
        onIncompatibleBrowser: () => console.log('Pdf was opened in a new tab due to an incompatible browser')
        })
        console.log("TimeOut Executado");
        }
        ,3000)

It is happing just with pdf files, I tested with image like logo.png and it worked. I insert that params to callback to trying to figured out what is happing, and OnPrintDialogClose() is trigged, but the modal box did not show! The screen blink and this message is showed 'The print dialog was closed'.

Any suggestion?

Hasunohana
  • 565
  • 8
  • 22
  • In case you didn't know, a **lot** of people do **not** use the in-browser PDF-renderer, instead downloading the files and using Adobe Acrobat or Adobe Reader. In those cases, **which are beyond the control of the web application or JavaScript**, there is no way to access/force the print dialog. – manassehkatz-Moving 2 Codidact Nov 29 '20 at 03:07
  • it is a functionality in which the user print a "receipt". I didnt like the browser pdf render idea, I would like that print function to occur in background... but I think that it would be necessary a API to comunicate with printer driver right? – Hasunohana Nov 29 '20 at 03:14
  • Exactly! There are point-of-sale/invoicing systems which either communicate directly to the printer (requires configuring printer & router appropriately) or that use a special printer driver that grabs the web data and sends it to the local printer - which requires installing software on the client machine *outside the browser*. AFAIK (and I've looked a bit in years past) there is, **by design**, no way to force printing from a web application without additional hardware and/or out-of-browser-software. – manassehkatz-Moving 2 Codidact Nov 29 '20 at 03:20

1 Answers1

0

Looking at https://printjs.crabbly.com/#configuration I see there are some more configuration options that might give you clues. I would recommend trying this (I added onError and modalMessage):

setTimeout(() => {            
  printJS({
    printable: '53544.pdf',
    type: 'pdf',
    showModal: true, 
    modalMessage: "Document Loading...",
    onError: (err) => console.log(err),
    fallbackPrintable: () => console.log("FallbackPrintable"),      
    onPrintDialogClose: () => console.log('The print dialog was closed')
  });
  console.log("TimeOut Executado");
  }, 3000)

Then my troubleshooting questions would be:

  • If you look at the url of that page, appending /53544.pdf the right location?
  • If you look in the network tab of your developer tools do you see the pdf being requested and is it successful?
Sydney Y
  • 2,912
  • 3
  • 9
  • 15
  • 2
    I forgot to mention, the address is localhost:3500, when the setTimout is loaded if I insert /53544.pdf it does not open, but if I open a new tab and write localhost:3500/53544.pdf, works! About the location, I have at same folder an image and using it for testing, it worked! – Hasunohana Nov 29 '20 at 15:28
  • I did that test `onError`, nothing trigged! and the network tab, the pdf file is being requested – Hasunohana Nov 29 '20 at 15:37
  • Another test: what about a different pdf? The Print.js documentation link has sample PDFs. Maybe printjs can't handle the encoding of your pdf. – Sydney Y Nov 29 '20 at 16:52