电子,来自 BrowserWindow 的 printToPDF

electron, printToPDF from BrowserWindow(电子,来自 BrowserWindow 的 printToPDF)
本文介绍了电子,来自 BrowserWindow 的 printToPDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I understand the usual method within electron to printToPDF is within the main process to call the following code:-

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.

However, what I am trying to achieve is to effectively call printToPDF from within BrowserWindow on the click of a button.

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

I understand from this: https://github.com/electron/electron/pull/1835/commits/1eba552a8d1ab4479824275f0e0a2cea9337bd8c that printToPDF has been exposed to BrowserWindow, but there is no documentation on how to actually call printToPDF from within a webpage.

它的谷歌也没有显示一个例子.有什么线索吗?

A google of it doesn't reveal an example either. Any clues?

推荐答案

renderer.js

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)
    })
  })
})

这篇关于电子,来自 BrowserWindow 的 printToPDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Google apps script get range of bytes from binary file(谷歌应用程序脚本从二进制文件中获取字节范围)
Sending Multiple attachments with Google Script from Google Drive(使用 Google 脚本从 Google Drive 发送多个附件)
Distributing Google Apps Scripts for Sheets in your company network(在您的公司网络中分发适用于表格的 Google Apps 脚本)
Upload file to my google drive from anyone using javascript(使用 javascript 将文件从任何人上传到我的谷歌驱动器)
quot;Shared Drivequot; support in Google Apps Script(“共享驱动器Google Apps 脚本中的支持)
Angular 2+ HTTP POST and GDrive API. Resumable file upload with name(Angular 2+ HTTP POST 和 GDrive API.带名称的可恢复文件上传)