Comments (15)
@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.
from electron-pdf-window.
@patrickrobbins
Hi try my solution, it works
let code ="var button = document.getElementById('print'); button.click()"
;
win.webContents.executeJavaScript(code);
from electron-pdf-window.
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.
from electron-pdf-window.
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
})
from electron-pdf-window.
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?
from electron-pdf-window.
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.
from electron-pdf-window.
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.
from electron-pdf-window.
@wujingshi any updates ? Please provide link for your plugin , Thanks !
from electron-pdf-window.
@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.
from electron-pdf-window.
@wujingshi if you can push repo on github maybe i can help fixing . Also are you facing error on macOs ?
from electron-pdf-window.
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
from electron-pdf-window.
@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.
from electron-pdf-window.
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.
from electron-pdf-window.
@arslanmughal99
What version of Sumatra PDF do you use?
from electron-pdf-window.
3.2 64bit-build
from electron-pdf-window.
Related Issues (20)
- Shows an invalid package message when in production HOT 1
- working fine before build using electron . , but not working after i build as exe file HOT 4
- Read PDF from String HOT 1
- Call print event directly HOT 1
- Opening local pdf files on Windows downloads them, shows blank window.
- Update GOT module HOT 3
- Change locale
- Update dependencies
- Pdf empty when created with custom font
- Blank window with download option
- [email protected] dependency contains high-severity vulnerability
- Default filename while downloading pdf file.
- 加载本地pdf一直是空白 HOT 2
- location.reload() not working with electron-pdf-window
- Quality of printed pdf (windows) HOT 3
- throws error Error: Cannot find module 'electron-pdf-window' after build HOT 1
- can i disable the open file button??
- win.loadURL(`file://${__dirname}/helloworld.pdf`) not work
- Will there be any updates for this project? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from electron-pdf-window.