【问题标题】:electron, printToPDF from BrowserWindow电子,来自 BrowserWindow 的 printToPDF
【发布时间】:2017-08-18 07:25:32
【问题描述】:

我了解电子中 printToPDF 的常用方法是在main 进程中调用以下代码:-

const {BrowserWindow} = require('electron')
const fs = require('fs')

let win = new BrowserWindow({width: 800, height: 600})
win.loadURL('http://github.com')

win.webContents.on('did-finish-load', () => {
  // Use default printing options
  win.webContents.printToPDF({}, (error, data) => {
    if (error) throw error
    fs.writeFile('/tmp/print.pdf', data, (error) => {
      if (error) throw error
      console.log('Write PDF successfully.')
    })
  })
})

但是,我想要实现的是通过单击按钮从BrowserWindow 内有效地调用 printToPDF。

我从中了解到:https://github.com/electron/electron/pull/1835/commits/1eba552a8d1ab4479824275f0e0a2cea9337bd8c printToPDF 已暴露给 BrowserWindow,但没有关于如何从网页中实际调用 printToPDF 的文档。

它的谷歌也没有透露一个例子。有什么线索吗?

【问题讨论】:

    标签: javascript pdf electron


    【解决方案1】:

    接受的答案涵盖使用 printToPDF() 将内容保存为 pdf 的方面,使用 ipc 事件。但是我们也可以从渲染器进程中printToPDF。这是如何从渲染器进程打印而不是从main 保存的示例。

    按照给定的方式导入remotefsshell(用于预览PDF),

    const remote = require("electron").remote;
    const fs = remote.require('fs');
    const shell = remote.shell;
    

    使用remote,检索当前窗口并调用printToPDF()方法,如下所示,

    function saveInvoiceToStorage(){
        remote.getCurrentWindow().webContents.printToPDF({
            pageSize : 'A4',
        } , function(error , data){
    
                if(error){
                    console.log(error);
                    return;
                }
    
                let pdfPath = `Users/vkiranmaniya/Desktop/print.pdf`;
    
                fs.writeFile(pdfPath, data, function (error) {
                    if (error) {
                         console.log(error);
                    }
    
                    shell.openExternal('file://' + pdfPath);
                });
        });
    }
    

    您有更多选项来配置printToPDF 功能,请访问Official Docs 了解更多信息。

    【讨论】:

      【解决方案2】:

      导出当前窗口:

      // In renderer process
      
      let remote = require('electron').remote
      
      remote.getCurrentWindow().webContents.printToPDF(...)
      

      【讨论】:

        【解决方案3】:

        renderer.js

        const ipc = require('electron').ipcRenderer
        
        const printPDFBtn = document.getElementById('pdfME')
        
        printPDFBtn.addEventListener('click', function (event) {
          ipc.send('print-to-pdf')
        })
        

        main.js

        const electron = require('electron')
        const fs = require('fs')
        const app = electron.app
        const BrowserWindow = electron.BrowserWindow
        const Menu = electron.Menu
        const Tray = electron.Tray
        const ipc = electron.ipcMain
        
        const path = require('path')
        const url = require('url')
        const shell = electron.shell
        
        let mainWindow
        
        ipc.on('print-to-pdf', function (event) {
          const pdfPath = path.join(__dirname, '/reports/print.pdf')
          const win = BrowserWindow.fromWebContents(event.sender)
          win.webContents.printToPDF({printBackground: true, landscape: true}, function (error, data) {
            if (error) throw error
            fs.writeFile(pdfPath, data, function (error) {
              if (error) {
                throw error
              }
              shell.openExternal('file://' + pdfPath)
              event.sender.send('wrote-pdf', pdfPath)
            })
          })
        })
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-12
          • 1970-01-01
          • 2020-12-05
          • 2019-09-11
          • 2016-07-13
          • 1970-01-01
          • 2016-04-25
          • 2018-06-26
          相关资源
          最近更新 更多