A mechanism for converting an HTML element to an image (e.g., PNG) is "rendering" it via a JEditorPane
, as follows:
public void render(String html, int width, int height, file output) {
JEditorPane jep = new JEditorPane("text/html", html);
jep.setSize(width, height);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
jep.print(image.getGraphics());
ImageIO.write(image, "PNG", output);
}
This approach works for simple HTML code, including basic support for CSS.
Is there any way to "autodetect" the width and height, instead of specifying it explicitly in the render()
method?
Or any better way to convert HTML to an image, via "plain vanilla" Java
?