tags:

views:

103

answers:

2

Does anyone know how to do this? I've tried with JEditorPane but it does not work? Any other idea?

Thanks in advance.

This is the code I'm using:

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

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

public class HtmlToImage
    {

        protected static File generateOutput() throws Exception
        {
            // Create a temporary output file for the PNG image.
            File outputFile =  new File("Reporte.png");
            outputFile.deleteOnExit();

            JEditorPane pane = new JEditorPane();
            pane.setContentType("text/html");
            pane.setPage("http://www.google.com");
            final JFrame frame = new JFrame();
            frame.pack();

            // Time Delay for the correct loading of the file.
            try
            {
                Thread.sleep(5000);
            }
            catch(NumberFormatException nfe)
            {
            }

            frame.add(pane);
            frame.pack();

            Dimension prefSize = pane.getPreferredSize();
            pane.setSize(prefSize);

            BufferedImage img = new BufferedImage(  prefSize.width, prefSize.height,
                                                  BufferedImage.TYPE_INT_RGB);
            Graphics2D g = (Graphics2D) img.getGraphics();

            SwingUtilities.paintComponent(g, pane, frame, 0, 0, prefSize.width, prefSize.height);

            ImageIO.write(img, "png", outputFile);

            return outputFile;
        }

        public static void main(String[] args)
        {
            try
            {               
                generateOutput();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

    }
+1  A: 

You need to render the HTML and output the result as a picture file. Currently there is no full-fledged HTML renderer in core Java so you'll need a separate library or an application, WebRenderer for example. Simply invoke it from a servlet filter and override the response with rendering results.

Edit Open source alternative to WebRenderer is Cobra

Saul
Thanks @Saul I am building an opensourced app so I would like to use opensourced tools/libs.
hernangarcia
Swing's HTMLEditorKit does provide HTML rendering (albeit primitive). http://download.oracle.com/javase/6/docs/api/javax/swing/text/html/HTMLEditorKit.html
dogbane
@dogbane: HTMLEditorKit supports only a very minimal set of functionality modern websites use. It's not a full-fledged web rendering engine.
Saul
+2  A: 

You could try using a JEditorPane as follows:

//load the webpage into the editor
JEditorPane ed = new JEditorPane(new URL("http://www.google.com"));
ed.setSize(200,200);

//create a new image
BufferedImage image = new BufferedImage(ed.getWidth(), ed.getHeight(),
                                        BufferedImage.TYPE_INT_ARGB);

//paint the editor onto the image
SwingUtilities.paintComponent(image.createGraphics(), 
                              ed, 
                              new JPanel(), 
                              0, 0, image.getWidth(), image.getHeight());

//save the image to file
ImageIO.write((RenderedImage)image, "png", new File("google.png"));
dogbane