7

How can i easily convert html to image and then to byte array without create it

thanks

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
cls
  • 109
  • 1
  • 3
  • 7

4 Answers4

15

If you do not have any complex html you can render it using a normal JLabel. The code below will produce this image:

<html>
  <h1>:)</h1>
  Hello World!<br>
  <img src="http://img0.gmodules.com/ig/images/igoogle_logo_sm.png">
</html>

alt text

public static void main(String... args) throws IOException {

    String html = "<html>" +
            "<h1>:)</h1>" +
            "Hello World!<br>" +
            "<img src=\"http://img0.gmodules.com/ig/images/igoogle_logo_sm.png\">" +
            "</html>";

    JLabel label = new JLabel(html);
    label.setSize(200, 120);

    BufferedImage image = new BufferedImage(
            label.getWidth(), label.getHeight(), 
            BufferedImage.TYPE_INT_ARGB);

    {
        // paint the html to an image
        Graphics g = image.getGraphics();
        g.setColor(Color.BLACK);
        label.paint(g);
        g.dispose();
    }

    // get the byte array of the image (as jpeg)
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", baos);
    byte[] bytes = baos.toByteArray();

    ....
}

If you would like to just write it to a file:

    ImageIO.write(image, "png", new File("test.png"));
dacwe
  • 43,066
  • 12
  • 116
  • 140
  • i don't need to create it only to save it as byte array – cls Dec 14 '10 at 10:31
  • You have to go through something like an `ImageIO.write`. You can't magically construct the byte array without having the image first. – aioobe Dec 14 '10 at 10:35
  • what do you mean - byte array formt? – cls Dec 14 '10 at 11:06
  • When you convert a `BufferedImage` to a `byte[]` you need to know how the data should be packed to the array. There are many formats `bmp`, `jpg`, `gif` etc. – dacwe Dec 14 '10 at 11:26
  • i want to create the image from html (with image that just when i create it i have the image source. therefore i want to convert to image with the image) – cls Dec 14 '10 at 11:40
  • I need to convert it to pdf, do you have any idea ? – cls Dec 14 '10 at 13:00
  • To pdf? There is no native way of doing it in java. Check http://java-source.net/open-source/pdf-libraries – dacwe Dec 14 '10 at 13:10
  • HTML to PDF can be very tricky, see this related question (with answers) : http://stackoverflow.com/questions/6133581/html-to-pdf-using-itext-how-can-produce-a-checkbox – RealHowTo Aug 18 '11 at 01:43
  • It's worth noting that this code seems to be not deterministic between different servers. I'm using the same Java package, with the same fonts, and getting different results between Ubuntu and Windows. http://stackoverflow.com/questions/9554935/java-imageio-generating-images-with-different-quality-on-linux-on-windows – ripper234 Mar 04 '12 at 15:05
4

I think you can use the library

html2image-0.9.jar

you can download this library at this page: http://code.google.com/p/java-html2image/

canadian_scholar
  • 1,315
  • 12
  • 26
3

What about using an in memory ByteArrayStream instead of a FileOutputStream in the code above? That would be a byte array, at least ...

toxicate20
  • 5,270
  • 3
  • 26
  • 38
phil
  • 31
  • 1
0

This is nontrivial because rendering a HTML page can be quite complex: you have text, images, CSS, possibly even JavaScript to evaluate.

I don't know the answer, but I do have something that might help you: code for iText (a PDF writing library) to convert a HTML page to a PDF file.

public static final void convert(final File xhtmlFile, final File pdfFile) throws IOException, DocumentException
{
    final String xhtmlUrl = xhtmlFile.toURI().toURL().toString();
    final OutputStream reportPdfStream = new FileOutputStream(pdfFile);
    final ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(xhtmlUrl);
    renderer.layout();
    renderer.createPDF(reportPdfStream);
    reportPdfStream.close();
}
Wouter Lievens
  • 4,019
  • 5
  • 41
  • 66