44

I'm trying to use chrome headless to print my webpage to a PDF file. The PDf file is with no data, because the headless chrome is printing it before the ajax commands finish.

Any idea on how I can get it to wait?

Here's the command I currently use:

chrome --headless http://localhost:8080/banana/key --run-all-compositor-stages-before-draw --print-to-pdf=C:\\tmp\\tmp.pdf
Gil Shamgar
  • 578
  • 1
  • 6
  • 12
  • 2
    A quick Google search shows that a few other people have run into this issue too. Have you explored using [Puppeteer](https://developers.google.com/web/updates/2017/04/headless-chrome#puppeteer) to programmatically run Chrome Headless instead? It may expose properties such as requests in flight that you can use to control your delay, or if you wish you could even manually include a timed delay yourself. – Mike Hill Apr 02 '18 at 16:40
  • 1
    @MikeHill Puppeteer looks like the way to go, thanks for the suggestion! It can wait for the page to load, and network idle, plus you get full control over the PDF layout. – OverZealous May 31 '18 at 18:12

1 Answers1

62
chrome --headless http://localhost:8080/banana/key --run-all-compositor-stages-before-draw --print-to-pdf=C:\\tmp\\tmp.pdf --virtual-time-budget=10000

Try this, virtual-time-budget is the parameter which can delay it.

qwr
  • 9,525
  • 5
  • 58
  • 102
Izayoi Sakuya
  • 731
  • 4
  • 5
  • 2
    This also has a positive effect on Web Fonts, even when the fonts are loaded from the local disk. Otherwise Chrome seems to have problems loading *some* Web Fonts in time... strange world. – JepZ Jul 19 '18 at 00:34
  • 1
    Works like a charm. Thanks for pointing this out. Thinking about it it's obvious that Chrome cannot guess when the page is actually ready for a screenshot. – Sir Hackalot Nov 01 '18 at 00:01
  • 9
    --virtual-time-budget is in milliseconds, just for future reference – JimJty Nov 29 '18 at 17:29
  • 1
    @Ivan: The argument specifies the maximum time chrome is waiting (10000 ms in this case). It will continue sooner, if page loading has completed sooner. – lauxjpn Dec 23 '18 at 02:18
  • 2
    This successfully waits until Google Fonts are being loaded that are referenced within a CSS file via @import. – lauxjpn Dec 23 '18 at 02:22
  • 2
    there is also a `--timeout` option but did not really worked for me same as `--virtual-time-budget` – Wlad Jul 28 '19 at 10:56
  • 2
    The options are explained here https://docs.google.com/document/d/1PppegrpXhOzKKAuNlP6XOEnviXFGUiX2hop00Cxcv4o/edit# – qwr Aug 20 '21 at 02:33