Electron 打印功能的实现

您所在的位置:网站首页 vue调用打印机的两种方式 Electron 打印功能的实现

Electron 打印功能的实现

2023-06-02 17:39| 来源: 网络整理| 查看: 265

        Electron中的打印功能有以下几种方式:webContents的print和printToPDF方法、webview标签的print和printToPDF方法、iframe的print方法。

        关于print方法,webContents、webview和iframe都是调用的浏览器自带的打印功能,虽然Electron文档中罗列了很多打印配置项,但实际使用时看不到实际效果,打印的最终效果也较差。而printToPDF方法效果就好很多。

        另外,print方法在调用时会弹出打印配置窗口,printToPDF方法则可以实现无弹窗静默打印。

      Print示例 // webContent let electron = require('electron') let webContent = electron.remote.getCurrentWebContents() webContent.print({printBackground: true}, (success, errorType) => {   if (!success) console.log(errorType) }) // webview let webviewObj = document.querySelector('webview') webviewObj.print({printBackground: true}) // iframe let iframeObj = document.createElement('iframe') iframeObj.width = '400' iframeObj.height = '300' document.body.appendChild(iframeObj) iframeObj.src = URL.createObjectURL(new Blob([`                   Print         自定义打印内容   `], { type: 'text/html' })) iframeObj.contentWindow && iframeObj.contentWindow.print()       PrintToPDF示例 // webContent let electron = require('electron') let fs = require('fs') let path = require('path') let webContent = electron.remote.getCurrentWebContents() let pdfPath = path.join(electron.remote.app.getPath('desktop'), '1.pdf') webContent.printToPDF({printBackground: true, landscape:true}).then(data => {   fs.writeFile(pdfPath, data, (error) => {     if (error) throw error     console.log(`Wrote PDF successfully to ${pdfPath}`)   }) }).catch(error => {   console.log(`Failed to write PDF to ${pdfPath}: `, error) }) // webview let webviewObj = document.querySelector('webview') webviewObj.printToPDF({printBackground: true}).then(data => {   fs.writeFile(pdfPath, data, (error) => {     if (error) throw error     console.log(`Wrote PDF successfully to ${pdfPath}`)   }) }).catch(error => {   console.log(`Failed to write PDF to ${pdfPath}: `, error) })



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3