8

I have a small HTML template using which i have to create an image. The HTML consists of text and formatting. The generated image is used by other services. It is similar to product price display in retail shops.

Is there a Java library that can render HTML to an image file or byte array ? I saw Cobra but it seems old.

EDIT: Setting basic HTML to JLabel and using BufferedImage should work, but i'm not sure if the CSS and Style stuff will get properly handled.

Sample Style

<styles> width: "240", height: "96", background: { type: "solid", color: "#ffffff" } </styles>

basiljames
  • 4,777
  • 4
  • 24
  • 41
  • Would parsing the HTML to find the image fit your needs? – David Jun 12 '13 at 09:11
  • @david99world i have to create an image similar to what the html would look like when rendered in a browser. There is no image in the HTML. – basiljames Jun 12 '13 at 09:13
  • *"The HTML consists of text and formatting."* Give us an example. I would initially look to simplify (if needed) the HTML, and render it in a `JLabel` or `JEditorPane`. BTW - *"CSS and Style stuff"* Unless you mean something completely different to what I understand by 'Style stuff', the two are one and the same. – Andrew Thompson Jun 12 '13 at 09:17
  • @AndrewThompson added a sample style. – basiljames Jun 12 '13 at 09:34
  • `` That element does not exist in any HTML specification I ever heard of. Did you copy/paste it or are you just making it up as you go along? But I don't want just the style, show a complete (**valid**) HTML as might be supplied for rendering. – Andrew Thompson Jun 12 '13 at 13:51
  • I just realized that even beyond `styles`/`style`, that is not valid **CSS.** Make sure to validate ***that** in an appropriate validation service also. – Andrew Thompson Jun 12 '13 at 16:50

4 Answers4

6

My solution involves 3 steps:

  1. Create a BufferedImage and create the its Graphics
  2. Create an JEditorPane and invoke print(Graphics)
  3. Output the BufferedImage via ImageIO

Code:

import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JEditorPane;

public class Test {

    public static void main(String[] args) {
        String html = "<h1>Hello, world.</h1>Etc. Etc.";
        int width = 200, height = 100;
        // Create a `BufferedImage` and create the its `Graphics`
        BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getDefaultScreenDevice().getDefaultConfiguration()
                .createCompatibleImage(width, height);
        Graphics graphics = image.createGraphics();
        // Create an `JEditorPane` and invoke `print(Graphics)`
        JEditorPane jep = new JEditorPane("text/html", html);
        jep.setSize(width, height);
        jep.print(graphics);
        // Output the `BufferedImage` via `ImageIO`
        try {
            ImageIO.write(image, "png", new File("Image.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Result:

result

johnchen902
  • 9,531
  • 1
  • 27
  • 69
  • +1 The tool mentioned below is based in JEditorPane but has done few lines of additional coding to make sure that the page loading is complete before the print is done. – basiljames Jun 14 '13 at 11:20
5

Hello I use HTML2Image for this purpose.

It's quite simple:

HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
imageGenerator.loadHtml("<b>Hello World!</b> Please goto <a title=\"Goto Google\" href=\"http://www.google.com\">Google</a>.");
imageGenerator.saveAsImage("hello-world.png");
imageGenerator.saveAsHtmlWithMap("hello-world.html", "hello-world.png");
Ricardo Simmus
  • 334
  • 4
  • 19
1

Try flying saucer (a.k.a. xhtml renderer). It it supports many CSS 3 features and can render to images and PDF. Its image rendering is experimental though and missing things such as DPI setting (assumes 1 point == 1 pixel).

rustyx
  • 80,671
  • 25
  • 200
  • 267
0

You could probably use WebVector. It is based on the CSSBox rendering engine that actually does all the job. It's quite easy to generate a bitmap image from a web page using this library. Or you may take look at a similar topic here at StackOverflow.

Community
  • 1
  • 1
radkovo
  • 868
  • 6
  • 10