gpt4 book ai didi

javascript - 如何在 ElectronJS 中打印 DIV

转载 作者:IT老高 更新时间:2023-10-28 22:59:56 29 4
gpt4 key购买 nike

我正在尝试将我的网络转换为使用 ElectronJS 制作的应用

在我的网页中,我打印了一个带有条形码的 div。这工作得很好,但在 electronjs 中我无法做到这一点。

本来我会用这个功能

$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();
}

用 electronjs

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

我也在尝试从我可以加载的内容生成 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);
});
});

有一种简单的方法可以用 electronjs 打印一个 DIV 吗?

感谢您的阅读。

最佳答案

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

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

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({}).then((data) {
fs.writeFile(pdfPath, data, function (error) {
if (error) {
throw error
}
shell.openItem(pdfPath)
event.sender.send('wrote-pdf', pdfPath)
})
}).catch((error) => {
throw error;
})
});

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>

关于javascript - 如何在 ElectronJS 中打印 DIV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37627064/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com