使用Electron JS打印PDF文件 [英] Printing a PDF file with Electron JS

查看:2828
本文介绍了使用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

这篇关于使用Electron JS打印PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆