electron: webContents.printToPDF never ends

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

I’m creating an “html to pdf” feature for my app, the user can enter a list of urls or html file, clicks a button and the app converts all of them. I’m using the Promise version of loadUrl and printToPdf, this is a fiddle I managed to create to reproduce the issue. My issue is that it works most of the times but for some urls it just stays there and never ends. It reaches the printToPDF and then nothing happens, no error is printed or pdf file written, it just stays there in the printToPDF. I tried the 7.0.0.beta7 and it does the same, I tried on Windows and Linux and I have the same issue. If I enable logs I get many of these, they don’t seem to be related to the printToPdf issue but still, they don’t seem normal either:

[1:1018/121218.543891:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121218.544037:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121218.612767:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121220.512380:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed

When I trigger the printToPDF these are the logs I get:

[1:1018/121435.112578:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[12207:1018/121435.113286:WARNING:ipc_message_attachment_set.cc(49)] MessageAttachmentSet destroyed with unconsumed attachments: 0/1
[1:1018/121435.141258:VERBOSE1:sandbox_linux.cc(69)] Activated seccomp-bpf sandbox for process type: utility.
[1:1018/121435.141539:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121435.141857:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121435.141901:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121435.141952:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121435.141980:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121435.142012:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121435.142036:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121435.142060:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121435.142098:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121435.142127:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121435.142158:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121435.142187:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121435.145491:VERBOSE1:v8_context_snapshot.cc(152)] A context is created from snapshot for main world
[1:1018/121435.173524:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed
[1:1018/121520.471001:ERROR:paint_controller.cc(547)] PaintController::FinishCycle() completed

Any idea on how to debug this or is there anything wrong in my code? As I said it works most of the times for some urls it seems to hang without any feedback Thanks

  • Electron Version:
    • 6.0.12
  • Operating System:
    • Linux Mint Linux Mint 19.1 Cinnamon 64bits, Windows 7 64 bits
  • Last Known Working Electron version:
    • don’t know

Expected Behavior

The page is printed to PDF

Actual Behavior

printToPDF never ends

To Reproduce

https://gist.github.com/f062371cd25fd38130af2e8e2151556e

Screenshots

This the fiddle screeshot, ‘after print’ is never reached. image

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 4
  • Comments: 16 (6 by maintainers)

Most upvoted comments

For others facing the same issue you can try something like win.webContents.insertCSS(‘iframe {display: none !important;}’)

seems like printing with iframe on the page is the culprit of printing pdf hanging.

Gonna try to spend some time on this during the upcoming week.

I am seeing this behavior when the page being printed contains an iFrame. But in my case it only occurs on MacOS. Windows/Linux are working.

I can confirm what @codecounselor said, under v8.3.0 it hangs forever when the HTML contains an iframe. I haven’t checked if this issue occurs only under macOS though.


Same thing under v9.

The docs are a bit confusing. The contents.printToPdf(options) returns a promise, but the docs have a callback example. I got this to work:

ipcMain.on('printPDF', (event, arg) => {
  const pdfPath = `${app.getPath('desktop')}/${arg.name}.pdf`
  mainWindow.webContents.printToPDF({
    pageSize: 'Letter'
  }).then(data => {
    fs.writeFileSync(pdfPath, data, (err) => {
      if (err) throw err
      console.log('PDF success!')
    })
  }
  )
})

I tried both, Promise and callback and got the same result, it works with some address and silently fails with others. Did you try the address in the gist ?

I’ve just updated to v8.0.0-beta.7 which includes #21783, but printToPDF still never ends in my scenario (https://github.com/neopostmodern/wohnungsbot/issues/11).

The docs are a bit confusing. The contents.printToPdf(options) returns a promise, but the docs have a callback example. I got this to work:

ipcMain.on('printPDF', (event, arg) => {
  const pdfPath = `${app.getPath('desktop')}/${arg.name}.pdf`
  mainWindow.webContents.printToPDF({
    pageSize: 'Letter'
  }).then(data => {
    fs.writeFileSync(pdfPath, data, (err) => {
      if (err) throw err
      console.log('PDF success!')
    })
  }
  )
})