我正在尝试创建一个用于打印字母大小 PDF 的 Electron JS 应用程序.
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,那么你可以获取文件位置然后你可以使用使用 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窗户
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 for windows https://github.com/sumatrapdfreader/sumatrapdf
You can also use SumatraPDF for windows https://github.com/sumatrapdfreader/sumatrapdf
这篇关于使用 Electron JS 打印 PDF 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!