1

I want to create a functionality that converts HTML source code of some websites into jpg/png images. I also have to do that on external server - which means I don't know if it has all necessary libraries, and I don't wanna install there too much things, the less the better.

I have found two options:

- wkhtmltoimage
- cutycapt

But they both require dependencies to install, not to mention emulating server, which sucks.

Is there a way to do this without any external libraries? Thanks!

khernik
  • 2,059
  • 2
  • 26
  • 51
  • you can use old `` method. elements inside canvas can be saved as image from mouse right click menu. but IE the only one who couldn't save it as image. – Nikko Jun 11 '15 at 09:49
  • Hi i think this link has an answer for you.. http://stackoverflow.com/questions/757675/website-screenshots-using-php – phpfresher Jun 11 '15 at 09:49
  • use webkit2png. This saves a clipped image (-C) at 100% scale (-s 1): webkit2png -C -s 1 -W 1000 -H 1000 --clipwidth 1000 --clipheight 1000 http://t.co -F only saves a full size image: webkit2png -F http://stackoverflow.com –  Jun 11 '15 at 09:51

2 Answers2

0

You can use this Java Library, Html2Image.This simple Java library converts plain HTML markup to image and provides client-side image-map using HTML element and also supports different image formats such as GIF, PNG or JPEG image.

This library has very handy, simple and sober API's.

Common usage is this:

HtmlImageGenerator imageGenerator = new HtmlImageGenerator();

imageGenerator.loadHtml("<b>Hello World!</b> Please goto <a title=\"Goto Google\" href=\"http://www.google.com\">Google</a>.");
// OR you can fetch HTML from URL
imageGenerator.loadUrl("http://www.example.com/xyz.html");

imageGenerator.saveAsImage("hello-world.png");
imageGenerator.saveAsHtmlWithMap("hello-world.html", "hello-world.png");

Which will generate hello-world.png image of the HTML and hello-world.html file containing client-side image-map .

There are several others API's of this library which you can explore as per your needs.

Akshay Pratap Singh
  • 3,197
  • 1
  • 24
  • 33
0

This is very much possible in linux using pbmtext. This tool converts any text into a pbm image file.

For you case,

curl -L google.com | pbmtext > google.pbm

However, beware that this will produce a very large file (several 100s of MB). So you will have to do some post-processing, like converting it into another format or resizing it.

shivams
  • 923
  • 12
  • 27