使用Electron JS打印PDF文件
我正在尝试创建一个Electron JS应用程序,其目的是打印字母大小的PDF。
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({
width: 378,
height: 566,
show: true,
webPreferences: {
webSecurity: false,
plugins: true
}
});
// load PDF
win.loadURL('file://' + __dirname + '/header1_X_BTR.pdf');
// if pdf is loaded start printing
win.webContents.on('did-finish-load', () => {
win.webContents.print({silent: true, printBackground:true});
});
我的问题是:如果我有 print({silent:true})
我的打印机打印一个空页面。如果我有 print({silent:false})
,则打印机的打印方式与屏幕截图相同,包括标题,控件等。
My issues are: if I have print({silent:true})
my printer prints an empty page. If I have print({silent:false})
, the printer prints in the same way as the screenshot, with headers, controls, etc.
我需要对PDF内容进行静音打印,但我不能设法做了好几天。有没有人与Electron有同样的事情?
I need a silent print of the PDF content, and I can't manage to do it for days. Did anyone experience the same thing with Electron?
如果您已经有pdf文件或者在打印前保存了pdf我假设它是,那么你可以获取文件位置,然后你可以使用externals进程使用 child_process
进行打印。
If you have already have the pdf file or you save the pdf before printing "I assuming it is", then you can grab the file location then you can use externals process to do the printing using child_process
.
您可以使用 lp命令
或 PDFtoPrinter for windows
You can use lp command
or PDFtoPrinter for windows
const ch = require('os');
switch (process.platform) {
case 'darwin':
case 'linux':
ch.exec(
'lp ' + pdf.filename, (e) => {
if (e) {
throw e;
}
});
break;
case 'win32':
ch.exec(
'ptp ' + pdf.filename, {
windowsHide: true
}, (e) => {
if (e) {
throw e;
}
});
break;
default:
throw new Error(
'Platform not supported.'
);
}
我希望它有所帮助。
修改:
您还可以将SumatraPDF用于Windows https: //github.com/sumatrapdfreader/sumatrapdf