Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printing PDF programatically #21

Open
patrickrobbins opened this issue Feb 28, 2018 · 15 comments
Open

Printing PDF programatically #21

patrickrobbins opened this issue Feb 28, 2018 · 15 comments

Comments

@patrickrobbins
Copy link

I would like to load a PDF in a hidden window, and then programmaticly send this to the printer without any interaction from the user. (The use would click the print button on my app's view)

I'm not sure what the object is that I should be interacting with in the PDFWindow that would give me access to the pdf.js PDFPrintService.

Thanks

@ram-you
Copy link

ram-you commented Sep 15, 2018

@patrickrobbins
Hi try my solution, it works

let code ="var button = document.getElementById('print'); button.click()";

win.webContents.executeJavaScript(code);

@wujingshi
Copy link

How do I close the hidden window?
Too many hidden windows can cause CPU to use too much.
I mean, how to monitor the print end before closing the window.

@CARodriguezG
Copy link

@ram-you solution really works! But we have to make something before, because it seems it can't print until PDF gets render (and that has sense).

This is what I got:

// We define an interval to check until the PDF is ready to print
var interval = window.setInterval(function() {
    var pdf = pdfjsWebLibs.pdfjsWebApp.PDFViewerApplication;

    // Checks if PDF view is render and ready to continue, otherwise, it returns
    if(!pdf.pdfViewer.pageViewsReady) return;

    // Clears this interval to avoid to keep checking and running
    clearInterval(interval);

    // Finally, executes the click function on the print button
    document.getElementById("print").click();
}, 500); // Defines at how much ms it will check

And about @wujingshi question:

How do I close the hidden window?
Too many hidden windows can cause CPU to use too much.
I mean, how to monitor the print end before closing the window.

I found a good post here How to detect window.print() finish.
So using the same method as before (by executing window.webContents.executeJavaScript(code);) we have something like this:

(function() { 
    var beforePrint = function() { 
        // Do stuff before print
    }; 
    
    var afterPrint = function() { 
        // Do stuff after print
        // This will close the window after 500ms because just closing it will close it immediately and the print will not work, you can modify the value if needed
        setTimeout(function() { window.close(); }, 500);
    }; 
             
    if (window.matchMedia) { 
        var mediaQueryList = window.matchMedia("print"); 
        mediaQueryList.addListener(function(mql) { 
            if (mql.matches) { 
                beforePrint(); 
            } else { 
                afterPrint(); 
            } 
        }); 
    } 
             
    window.onbeforeprint = beforePrint; 
    window.onafterprint = afterPrint; 
}());

This is the way I used it on my Electron application and until now is working nice.

Finally it would be nice if this module could improve and handle those events for us. Greets.

@guilhermebruzzi
Copy link

If you are interested on print direct and don't even show the window, this did the trick for me:

const PDFWindow = require('electron-pdf-window')

const win = new BrowserWindow({ show: false })
PDFWindow.addSupport(win)
win.loadURL('file://PATH_TO_FILE.pdf')

win.webContents.on('did-finish-load', () => {
    setTimeout(() => {
        win.webContents.print(options || {}, (success) => {
            if (success) {
              console.log('Finished printing with success')
            } else {
              console.error('Finished printing with error')
            }
            win.close()
        })
    }, 2000) // A time to load and render PDF
})

@mralbertchen
Copy link

If you are interested on print direct and don't even show the window, this did the trick for me:

const PDFWindow = require('electron-pdf-window')

const win = new BrowserWindow({ show: false })
PDFWindow.addSupport(win)
win.loadURL('file://PATH_TO_FILE.pdf')

win.webContents.on('did-finish-load', () => {
    setTimeout(() => {
        win.webContents.print(options || {}, (success) => {
            if (success) {
              console.log('Finished printing with success')
            } else {
              console.error('Finished printing with error')
            }
            win.close()
        })
    }, 2000) // A time to load and render PDF
})

This prints a blank page for me. It seems like webContents is blank when you use PDF? How did you get it to work?

@wujingshi
Copy link

Sorry, this plug-in is not perfect. I've abandoned it.
I've developed another better way to print PDF silently.
If you're willing to wait, I'll write a new plug-in this Saturday.

@mralbertchen
Copy link

Sorry, this plug-in is not perfect. I've abandoned it.
I've developed another better way to print PDF silently.
If you're willing to wait, I'll write a new plug-in this Saturday.

Sure I'll wait. Let me know when you do.

@arslanmughal99
Copy link

@wujingshi any updates ? Please provide link for your plugin , Thanks !

@wujingshi
Copy link

@arslanmughal99
I'm sorry to hear from you so long
I have finished writing the plug-in, but there is an error when packing, so I am still solving this problem.

@arslanmughal99
Copy link

@wujingshi if you can push repo on github maybe i can help fixing . Also are you facing error on macOs ?

@arslanmughal99
Copy link

By the way i fix me issue using Sumatra PDF .
By calling using child_process.exec.
SumatraPDF.exe label.pdf -print-dialog -silent -print-settings "fit,portrait" -exit-when-done

@wujingshi
Copy link

@arslanmughal99
I used this to solve problems.
But there is a version limit, the latest version of Sumatra PDF. The data will be enlarged during transmission. For example, 100kb PDF will become 100MB data and transmitted to the printer.
But use Sumatra PDF version 2.2.0. It can solve this problem, but it is not as powerful as the latest version.
However, it is impossible to package when packing.

@arslanmughal99
Copy link

I didn't encounter the first issue you have mentioned , but as for packaging issue it is totally possible simple add binary in your resources folder . electron-builder will package it with app.

@wujingshi
Copy link

@arslanmughal99
What version of Sumatra PDF do you use?

@arslanmughal99
Copy link

3.2 64bit-build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants