使用electron.js打印当前的vue视图

时间:2019-03-01 15:34:20

标签: javascript vue.js electron

使用电子时,如何打印由vue渲染的当前网页视图?普通的js函数window.print()打印为空。那么电子方式是什么?是否有可能在不转换为.pdf的情况下进行静默打印?

1 个答案:

答案 0 :(得分:0)

打印当前窗口使用下面的代码

let win = BrowserWindow.getFocusedWindow(); 

 let win = BrowserWindow.getAllWindows()[0]; 

打印

  win.webContents.print(options, (success, failureReason) => { 
            if (!success) console.log(failureReason); 
      
            console.log('Print Initiated'); 
        }); 
    }); 

完整代码:

const electron = require('electron') 
const BrowserWindow = electron.remote.BrowserWindow; 
  
var current = document.getElementById('current');  
var options = { 
    silent: false, 
    printBackground: true, 
    color: false, 
    landscape: false, 
    pagesPerSheet: 1, 
    collate: false, 
    copies: 1, 
    header: 'Header of the Page', 
    footer: 'Footer of the Page'
} 
  
current.addEventListener('click', (event) => { 
    let win = BrowserWindow.getFocusedWindow(); 
    
  
    win.webContents.print(options, (success, failureReason) => { 
        if (!success) console.log(failureReason); 
  
        console.log('Print Initiated'); 
    }); 
});