栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在ElectronJS中打印DIV

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在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 workerWindowipcMain.on("printPDF", (event: any, content: any) => {    console.log(content);    workerWindow.webContents.send("printPDF", content);});// when worker window is readyipcMain.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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/668769.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号