問題描述
我正在嘗試創建一個用于打印字母大小 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 文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!