Open patrickrobbins opened 6 years ago
@patrickrobbins Hi try my solution, it works
let code ="var button = document.getElementById('print'); button.click()"
;
win.webContents.executeJavaScript(code);
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.
@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.
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
})
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?
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.
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.
@wujingshi any updates ? Please provide link for your plugin , Thanks !
@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.
@wujingshi if you can push repo on github maybe i can help fixing . Also are you facing error on macOs ?
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
@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.
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.
@arslanmughal99 What version of Sumatra PDF do you use?
3.2 64bit-build
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