Printing a PDF file with Electron JS

后端 未结 5 1859
执笔经年
执笔经年 2021-02-04 00:37

I am trying to create an Electron JS app that has the purpose to print letter size PDFs.

This is my snippet of code for printing:

win = new BrowserWindow         


        
5条回答
  •  终归单人心
    2021-02-04 01:03

    Since your are using contents.print([options], [callback]) I will assume that you want to print on paper and not on your Disk.


    The answer to your issue is simple. It is the event you are listening on which is causing the error. So if you simply do this:

      winObject.webContents.on('did-frame-finish-load', () => {
        setTimeout(() => {winObject.webContents.print({silent: true, printBackground:true})}, 3000);
      });
    

    everything will work fine if the default printer is the right one. I did test this and it will do its job more or less. You can change my event to whatever event you like, the important part is the waiting with setTimeout. The PDF you are trying to print is simply not available in the frame when using silent:true.

    However let me get into detail here a little bit to make things clear:

    Electron will load Files or URLs into a created window (BrowserWindow) which is bound to events. The problem is that every event "can" behave differently on different systems. We have to live with that and cannot change this easily. But knowing this will help improve the development of custom Apps.

    If you load urls or htmls everything will work without setting any custom options. Using PDFs as source we have to use this:

    import electron, { BrowserWindow } from 'electron';
    const win = new BrowserWindow({
      // @NOTE I did keep the standard options out of this.
      webPreferences: { // You need this options to load pdfs
        plugins: true // this will enable you to use pdfs as source and not just download it.
      }
    });
    

    hint: without webPreferences: { plugins: true } your source PDF will be downloaded instead of loaded into the window.

    That said you will load your PDF into the webContents of your window. So we have to listen on events compatible with BrowserWindow. You did everything right, the only part you missed was that printing is another interface.

    Printing will capture your webContents as it is when you press "print". This is very inportant to know when working with printers. Because if something will load slightly longer on a different system, for example the PDFs viewer will be still dark grey without the letters, then your printing will print the dark grey background or even the buttons.

    That little issue is easily fixed with setTimeout().

    Useful Q&A for printing with electron:

    • Silent printing in electron
    • Print: How to stick footer on every page to the bottom?

    However there are alot more possible issues with printing, since most of the code is behind closed doors without worldwide APIs to use. Just keep in mind that every printer can behave differently so testing on more machines will help.

提交回复
热议问题