16

Is there a ready-to-use program, that I can run from my web application, to convert an HTML file to an image? Preferably using Webkit, or another mature browser engine?

I tried wkhtmltoimage, but it doesn't render backgrounds correctly (while wkhtmltopd works fine). Is there one that works?

Michael Scheper
  • 6,514
  • 7
  • 63
  • 76
Djangonaut
  • 5,511
  • 7
  • 40
  • 53

2 Answers2

25

Method A

Using phantomjs

$ phantom rasterize.js http://www.google.com google.png

Download and unzip the binary archive. rasterize.js is in the example directory.

Method B

Using chrome (Tested on MacOSX, should work on other platforms)

$ alias chrome='/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'
$ chrome --headless --disable-gpu --screenshot=google.png http://www.google.com

Method C

Using webkit2png (macOS only)

kev
  • 155,172
  • 47
  • 273
  • 272
  • Better use 'sudo -u user'. And with subprocess.Popen use '--log', because it tries put log somewhere in project root. – Kriplins Apr 12 '12 at 07:07
  • 1
    Here is a direct link on the [`rasterize.js`](https://raw.githubusercontent.com/ariya/phantomjs/master/examples/rasterize.js). – luator Apr 20 '18 at 14:29
  • 2
    On some systems, 'libqt4-webkit' is not available, so replace that with 'libqtwebkit4'. – Respectech Jun 02 '18 at 01:57
  • This worked very well for me, but with the rename mentioned in Respectech's comment – Holger Ludvigsen Sep 07 '18 at 21:00
  • phantomjs has been abandoned – Benedikt M. Feb 13 '22 at 23:42
  • If you are running Chromium on Ubuntu then Method B would be chromium --headless etc. Worth noting: it takes a screenshot, so it's not going to convert the entire html page, just what's visible. Add --window-size=3000,3000 or something similar to capture the entire html page – rearThing Aug 23 '22 at 19:25
5

simply use firefox

firefox --headless --screenshot google.com

or visit here

Kasra
  • 766
  • 7
  • 18