endryha
endryha

Reputation: 7256

HTML with JS to Image/PDF

Is it possible to convert an html page with charts generated by javascript to an Image or PDF in Java?

I familiar with iText framework and it seems to be suitable but I am not sure how it handle JS generated things.

Upvotes: 1

Views: 1175

Answers (3)

alexcoco
alexcoco

Reputation: 6665

A quick search turned up this as a possible answer. Using a library to convert to XSL-FO then another one to convert that to PDF.

Edit: This might interest you as well. There's a bit on some JBrowser class that seems to let you print web pages.

Upvotes: 1

Yohan Liyanage
Yohan Liyanage

Reputation: 7000

Solution 1
If you have access to plain HTML (taken after the JavaScript has executed and built the page), you can easily pass it to iText and convert it to PDF. I would recommend using Flyng-Saucer (which in turn uses iText) which has a very good and convenient API for this (See http://code.google.com/p/flying-saucer/ ).

Solution 2
On the other hand, if you do not have access to the final HTML output, you could use the Swing libraries to render the page and then take a screenshot of it. This will allow you to even use Flash, but I'm not sure whether this approach will be suitable to your problem.

However, if it is the case, you can load the web page into a Swing application (you will need to rely on a third-party browser component for JS support, but there are quite a lot out there), and then you can use the Robot class to get a screenshot of it.

Take a look at http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html

Upvotes: 0

bjornd
bjornd

Reputation: 22943

It depends on how the were generated. I suppose three possibilities:

  1. Canvas tag. You need to add a bit of JS code to get image using toDataURL canvas method

  2. SVG. You can add some code to get the full code of generated SVG document via innerHTML method.

  3. Flash. The worst case. I think it's hardly possible to achive what you want.

Upvotes: 0

Related Questions