How to render in-memory HTML code to an image?

2.3k views Asked by At

I found http://code.google.com/p/flying-saucer/ which should be the most suitable solution for my project. But the documents only seem to be able to read from file.

But I my case I want to render from code and I don't want to save a temporary XML file. For example renderToImageAutoSize in Graphics2DRenderer

static java.awt.image.BufferedImage renderToImageAutoSize(java.lang.String url, int width, int bufferedImageType) A static utility method to automatically create an image from a document, where height is determined based on document content.

Is there any way to let the renderer read directly from HTML code in memory?

1

There are 1 answers

2
Adam On BEST ANSWER

Graphics2DRenderer is not a utility class. You should create a new instance of it.

Graphics2DRenderer gr = new Graphics2DRenderer();
gr.setDocument(doc, uri);
gr.layout(g2, dim);
Rectangle minSize = r.getMinimumSize();
//create new image with this size
gr.render(newG2);

See http://today.java.net/pub/a/today/2006/10/31/combine-facelets-and-flying-saucer-renderer.html#xhtml-to-image-conversion