3

Converting a pretty small HTML-file to PDF takes 2 seconds for my test enviroment (Windows 7). On the production web server the same file (Debian) needs 193 seconds.

The web server is being run with a managed hosting solution. They installed wkhtmltopdf and told me I had to use xvfb-run for it to work:

xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf "input_file.html" "output_file.pdf"

Now that I've complained that using this command it takes 193 seconds to generate a simple PDF, they say there is nothing they can do about it. Is that really so?

Edit: Somehow, my managed hosting support solved this after some nudging (now it takes 0.8 seconds). I think they might have recompiled the static wkhtmltopdf binaries or something. I now run wkhtmltopdf without xvfb-run.

Vilhelm
  • 148
  • 1
  • 7

2 Answers2

2

I assume you could leave the xvfb running

xvfb :1 -screen 0 640x480x16 &
DISPLAY=:1 wkhtmltopdf "input_file1.html" "output_file1.pdf"
DISPLAY=:1 wkhtmltopdf "input_file2.html" "output_file2.pdf"
DISPLAY=:1 wkhtmltopdf "input_file3.html" "output_file3.pdf"
  • I don't have experience with xvfb (I use x11vnc, Xnest and Xephyr with similar approaches like shown above).
  • Use an unused vtty (:1, :2, :3...)
  • perhaps there is a race condition now, in which case you might want to sleep .5 or something before launching wkhtmltopdf)
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Unfortunately, using your code no PDF-file is created and nothing is returned. – Vilhelm Jun 09 '11 at 10:06
  • You could try to use x11vnc or one of the other alternatives. x11vnc can is headless. I'm assuming wkhtmltopdf needs a X11 server in order to use a gui toolkit (gecko? webkit?) to render the page. Any X11 server (even a remote one...) would do. Check log files to see what is wrong with the X session. – sehe Jun 09 '11 at 10:28
  • The command should be `Xvfb` rather than `xvfb`. However, having the Xvfb server running ahead does not help speed up the `wkhtmltopdf` execution time, not even on subsequent runs of the same file. – user2468842 Dec 10 '22 at 18:28
1

wkhtmltopdf is going to be very slow, it is doing software rendering of the image in this case, since there is no GPU available.

An alternative is to go to http://wkhtmltopdf.org and download one of the static images. They can be run in true headless mode, and do not require an X server at all. This is still slow, but it has the advantage of having fewer moving parts.

Sean Fahey
  • 1,850
  • 3
  • 26
  • 36
Jim Penny
  • 81
  • 1
  • 2