Electron Print issue

You could 1) generate a hidden window for printing purposes, then 2) generate your html content and 3) load it to this window; 4) print.

Example with one text input and one image, if you need it:

// creating a hidden window for print
let printWindow = new BrowserWindow({
    useContentSize: true,
    show: false,
  });

// when you need to print, just generate and load needed html contents to the window. And print that window.
await printWindow.loadURL(`
      data:text/html;charset=utf-8,<head> 
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> 
      <title>My Title</title> 
      <style type="text/css">img{page-break-before: always;}@page{margin: 0;} </style>
      </head> 
      <body style="margin: 0; padding: 0;">
      <input type="text" value="${testValue1}"/>
      <img style="margin: 0; padding: 0;" src="data:image/png;base64,${pngBase64}" /> 
      </body>`)

await printWindow.webContents.print({
    silent: true,
})

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top