如何在ElectronJS中打印DIV

我试图将我的网站转换成在ElectronJS中制作的应用程序

在我的网站上打印一个带有条形码的div。 这工作相当好,但在电子我无法达到这一点。

原本我会使用这个function

$scope.printDiv = function (divName) { var printContents = document.getElementById(divName).innerHTML; var popupWin = window.open('', '_blank', 'width=500,height=500'); popupWin.document.open(); popupWin.document.write('<html><head><link rel="stylesheet" type="text/css" href="styles/main.css" type=\"text/css\" media=\"print\" /></head><body onload="window.print()">' + printContents + '</body></html>'); popupWin.document.close(); } 

与电子js

我不知道如何传递对象来打印。

另外我试图从内容,我可以加载生成PDF。 但PDF的损坏

 var windowPrint = require('electron').remote.BrowserWindow; var fs = require('fs'); var newWindow = new windowPrint({width: 800, height: 600, show: false}); console.log(newWindow); newWindow.loadURL('http://github.com'); newWindow.show(); newWindow.webContents.print({silent: true, printBackground: true}); newWindow.webContents.printToPDF({printSelectionOnly : true, printBackground: true}, function (error, data) { if (error) { throw error; } console.log(error); console.log(data); fs.writeFile('print.pdf', function (data, error) { if (error) { throw error; } console.log(error); console.log(data); }); }); 

有一个简单的方法来打印一个DIV与electronjs?

谢谢你的阅读。

在加载完成之前,您已打印此页面。

我的方法:1.创build一个主窗口和一个(不可见的)工作窗口

 import {app, BrowserWindow, Menu, ipcMain, shell} from "electron"; const os = require("os"); const fs = require("fs"); const path = require("path"); let mainWindow: Electron.BrowserWindow = undefined; let workerWindow: Electron.BrowserWindow = undefined; async function createWindow() { mainWindow = new BrowserWindow(); mainWindow.loadURL("file://" + __dirname + "/index.html"); mainWindow.webContents.openDevTools(); mainWindow.on("closed", () => { // close worker windows later mainWindow = undefined; }); workerWindow = new BrowserWindow(); workerWindow.loadURL("file://" + __dirname + "/worker.html"); // workerWindow.hide(); workerWindow.webContents.openDevTools(); workerWindow.on("closed", () => { workerWindow = undefined; }); } // retransmit it to workerWindow ipcMain.on("printPDF", (event: any, content: any) => { console.log(content); workerWindow.webContents.send("printPDF", content); }); // when worker window is ready ipcMain.on("readyToPrintPDF", (event) => { const pdfPath = path.join(os.tmpdir(), 'print.pdf'); // Use default printing options workerWindow.webContents.printToPDF({}, function (error, data) { if (error) throw error fs.writeFile(pdfPath, data, function (error) { if (error) { throw error } shell.openItem(pdfPath) event.sender.send('wrote-pdf', pdfPath) }) }) }); 

2,mainWindow.html

 <head> </head> <body> <button id="btn"> Save </button> <script> const ipcRenderer = require("electron").ipcRenderer; // cannot send message to other windows directly https://github.com/electron/electron/issues/991 function sendCommandToWorker(content) { ipcRenderer.send("printPDF", content); } document.getElementById("btn").addEventListener("click", () => { // send whatever you like sendCommandToWorker("<h1> hello </h1>"); }); </script> </body> 

3,worker.html

 <head> </head> <body> <script> const ipcRenderer = require("electron").ipcRenderer; ipcRenderer.on("printPDF", (event, content) => { document.body.innerHTML = content; ipcRenderer.send("readyToPrintPDF"); }); </script> </body> 

谢谢,也可以使用print()来打印

 ipcMain.on('print', (event, content) => { workerWindow.webContents.send('print', content); }); ipcMain.on('readyToPrint', (event) => { workerWindow.webContents.print({}); }); 

(事件相应地重新命名)