KrispyDonuts
KrispyDonuts

Reputation: 1262

Rendering HTML (w/ javascript) and Converting to an Image

I have an HTML page that has Javascript code. It needs to be rendered first before it can be converted into an image.

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

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

Or if there are any other ways of doing this, please let me know. I am just trying to find the best solution possible.

Upvotes: 4

Views: 2785

Answers (2)

KrispyDonuts
KrispyDonuts

Reputation: 1262

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

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328644

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

I'd try either of these approaches:

  1. Use env.js + rhino to simulate a browser in which you can run the JavaScript. That should give you a DOM which you can render using FlyingSaucer, for example.

  2. Add SWT to your classpath (plus the binary for your platform). It contains a Browser component that uses your system's browser to render URLs or an HTML string.

You probably need SWTBot to run the browser in headless mode.

If that doesn't work and you're on Linux, then you can start an in-memory X server Xvfb to open your browser. Or you can use vncserver to start a desktop on your server.

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

PhantomJS (www.phantomjs.org) is a headless WebKit scriptable with JavaScript or CoffeeScript.
[...]
Use cases: Headless web testing, Site scraping, Page rendering
Multiplatform, 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!
Pure headless (X11) on Linux, ideal for continuous integration systems. Also runs on Amazon EC2.

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

Upvotes: 1

Related Questions