Convert html file to an image file using java

Posted on 2007-10-05
Last Modified: 2013-12-14
Hello All, Can anybody provide me with an example, how to convert html file to an image file using Java?

Thanks in advance
Question by:altima4u
    LVL 13

    Accepted Solution

    The code below will work for simple HTML pages, as JEditorPane can only render not to complex pages. The componentToImage method can be used for any Component subclass, so if you find a better HTML renderer somewhere you can use that to get the image result.

    Most components will work with just the method, but JEditorPane needs some extra magic to make sure the page is loaded before the converter method is called. That's where the PropertyChangeListener and the Thread.yield() come in.

    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;

    public class Test {
        private static volatile boolean loaded;

        public static void main(String[] args) throws IOException {
            loaded = false;
            URL url = new URL("");
            JEditorPane editorPane = new JEditorPane();
            editorPane.addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {
                    if (evt.getPropertyName().equals("page")) {
                        loaded = true;
            while (!loaded) {

            File file = new File("out.png");

            componentToImage(editorPane, file);

        public static void componentToImage(Component comp, File file) throws IOException {
            Dimension prefSize = comp.getPreferredSize();
            System.out.println("prefSize = " + prefSize);
            BufferedImage img = new BufferedImage(prefSize.width, comp.getPreferredSize().height,
            Graphics graphics = img.getGraphics();
            ImageIO.write(img, "png", file);

    LVL 1

    Author Comment

    Thanks a lot..
    i have a small can i have a jpg/ jpeg file?  as my requirement is to have a jpeg file.. pls let me know
    LVL 13

    Expert Comment

    You can simply change the "png" in the ImageIO.write method to "jpg". ImageIO will use this parameter to define the output format.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now