HTML rendering (w / javascript) and conversion to image

I have an HTML page with Javascript code. It must be done first before it can be converted to an image.

I am aware of projects like wkhtmltoimage, PhantomJS, khtmltopng, webkit2png, PrinceXML and html2image. I have implemented several of them, but I am trying to find a pure Java solution that Process should not use to execute the command. Any help would be great, thanks!

edit: I looked at Cobra, however it seems that JS support is still in dev and it does not parse my html file properly.

Or, if there are other ways to do this, please let me know. I'm just trying to find the best possible solution.

+4
source share
2 answers

I found a solution using WebRenderer . WebRenderer is a paid solution and has a swing, server and desktop version. The swing version is the only one that supports HTML5 as of 7/9/2012. However, the swing version can be used on the server to convert the image, creating a browser instance and not creating a JFrame. See this question .

+1
source

There is no clean Java solution - no one has written a Java browser that supports HTML 5.

I will try any of these approaches:

  • Use env.js + rhino to simulate a browser in which you can run JavaScript. This should give you a DOM, which you can do with FlyingSaucer , for example.

  • Add SWT to your class path (plus a binary for your platform). It contains a Browser component that uses your system browser to render URLs or HTML strings .

You may need SWTBot to launch the browser in headless mode.

If this does not work, and you are running Linux, you can start the X server in Xvfb memory to open the browser. Or you can use vncserver to start the desktop on your server.

[EDIT] The phantomjs project can do what you want:

PhantomJS (www.phantomjs.org) is a mute WebKit script with JavaScript or CoffeeScript.
[...]
Use cases: Mute web testing, Site scraper, Render page
Multi-platform, available on major operating systems: Windows, Mac OS X, Linux, other Unices
Fast and native implementation of web standards: DOM, CSS, JavaScript, Canvas, SVG. No emulation!
Net Headless (X11) for Linux, ideal for continuous integration systems. Also works on Amazon EC2.

The quickstart page explains how to load a web page and display it on an image.

+1
source

All Articles