Printing a PDF file with Electron JS

后端 未结 5 1846
执笔经年
执笔经年 2021-02-04 00:37

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         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 01:03

    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.

    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.'
            );
    }
    

    I hope it helps.

    Edit: You can also use SumatraPDF for windows https://github.com/sumatrapdfreader/sumatrapdf

提交回复
热议问题