打印或取消打印后销毁BrowserView

时间:2019-07-30 13:16:57

标签: electron

我有一个可以打印图像的电子应用程序。为此,我创建一个浏览器视图,编写一些html并将html加载到其中并打印。 现在,我需要一种在用户完成打印后或用户取消打印后销毁此浏览器视图的方法。我当前的代码是:

function showPrintImageDialog() {
    // command to open the print image dialog, this will open up a new window for image setup before printing
    try {
        // get array of images for printing. if selectMode is enabled use that, else use the currently visible image
        let currentPrintList = (selectedImages && selectModeOn) ? selectedImages : [ currentFileList[currentFileIndex] ];
        // build printer html string
        let printerHTML = '<!DOCTYPE html><html><head><link rel="stylesheet" href="printer.css"></head><body>';
        // get all images to print
        for (let i in currentPrintList) {
            // create div's for paper and image
            printerHTML += '<div class="tempPrinterPaper"><img class="tempPrinterPaperImage" src="' + currentPrintList[i] + '" /></div>';
        }
        // close html string
        printerHTML += '<script src="printer.js"></script></body></html>';
        // get location of printer html file
        let printerHTMLPath = 'static/html/printer.html';
        // write html string to the file
        fs.writeFileSync( printerHTMLPath, printerHTML );
        // create a browser window
        printerWindow = new remote.BrowserView();
        // load html
        printerWindow.webContents.loadFile('static/html/printer.html');
        // print window
        printerWindow.webContents.on('did-finish-load', function() {
            printerWindow.webContents.print({silent: false, printBackground: false}, function(res) {
                if (res) {
                    printerWindow.destroy();
                }
            });
        });
    } catch (e) { console.log(e); }
    return false;
}

这将在完成打印后销毁broswerview,但无效,因为用户决定不取消。我非常担心应用程序中的内存。有提示吗?

0 个答案:

没有答案