Electron: How to print only part of html (div) electronically silently?

What I am trying to achieve is that a webpage remotely hosted and loaded into my e-app wants the Electron app to print only a specific div . I know that if I use webContents.print({silent:true})

, the whole page will print silently. But I want the same thing to happen with only a specific div. Thanks in advance.

+3


source to share


1 answer


One way to do this is to send the div a new hidden window and then print from there.


main.js

app.on('ready', function(){
  mainWindow = new BrowserWindow({ width: 1080, height:720})
  workerWindow = new BrowserWindow();
  workerWindow.loadURL("file://" + __dirname + "/printerWindow.html");
  workerWindow.hide();
});

// retransmit it to workerWindow
ipcMain.on("printPDF", function(event, content){
  workerWindow.webContents.send("printPDF", content);
});

// when worker window is ready
ipcMain.on("readyToPrintPDF", (event) => {
  workerWindow.webContents.print({silent: true});
})

      




controller.js

// target the object you want to print and send it to the new window
ipcRenderer.send("printPDF", div_to_be_targeted);

      

0


source







All Articles