How to convert HTML to Image and Saving into a File using Java

Posted By : Parveen Kumar Yadav | 03-Jul-2015

Sometimes we need to convert the HTML into Images and saved that into a file as i need in my Project. So in this blog i will explain how to convert Html file to an image and also maintain the good quality of images those are used in Html using java without using any external Plugin.

And take full control over image output.

we use the " org.xhtmlrenderer.swing.Java2DRenderer "class here.

//Generate an image from a file:

File file = new File("source.xhtml");
int width = 1024;
int height = 1024;
Java2DRenderer renderer = new Java2DRenderer(file, width, height);
BufferedImage image = renderer.getImage();

// write it out full size png defaults to png.
FSImageWriter imagWriter = new FSImageWriter();
imagWriter.write(image, "/home/parveen/Desktop/full-image.png");

// write  as uncompressed JPEG (the 1f parameter)
imagWriter = FSImageWriter.newJpegWriter(1f);
imagWriter.write(image, "/home/parveen/Desktop/full.jpg");

// now we compress it this is using by ImageIO utilities
imagWriter = FSImageWriter.newJpegWriter(0.76f);
imagWriter.write(image, "/home/parveen/Desktop/threeqtr.jpg");

// we can use the same writer, but at a different compression rate
imagWriter.setWriteCompressionQuality(0.8f);
imagWriter.write(image, "/home/parveen/Desktop/ninety.jpg");

// now we scale it ,ScalingOptions lets us control some quality options and pass in rendering hints
ScalingOptions scalingOption = new ScalingOptions (BufferedImage.TYPE_INT_ARGB , DownscaleQuality.LOW_QUALITY ,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR );

//target size -:you can reuse the option instance for different sizes
scalingOption.setTargetDimensions(new Dimension(200, 200));
Image scaled = ImageUtil.getScaledInstance(scalingOptions, image);

// we can also scale multiple dimensions at once (well, not at once, but.....)
// be careful because quality setting in the options instance can affect performance.

List dimensions = new ArrayList();
dimensions.add(new Dimension(150, 150)); 
dimensions.add(new Dimension(200, 200)); 
dimensions.add(new Dimension(600, 600));
dimensions.add(new Dimension(760, 760)); 
List images = ImageUtil.scaleMultiple(scalingOptions, image, dimensions);

Note the folowing things:- 

  • You can control some aspects of the image quality and internal (Java2D) rendering using rendering hints on the renderer , use setRenderingHints()
  • You can control over what type of image the "Java2DRenderer" creates in memory by overriding the "createBufferedImage()" method in the class.
  • To rescale the image, you can use "Java2D API" or use  "org.xhtmlrenderer.util.ImageUtil utility class".
  • You must always specify the width, the height  either specified or determined based on the document content . Once the image is created  it can be scaled , but there is always an image created at the original  target size during rendering.
  • Java2DRenderer is  mostly-immutable object, in the sense that once you have used it to render  document it would not re-render. You must have set your renderer up , provide all options , then render. You can reuse the instances of scaling options , rendering hint , image writer , just not the renderer itself.

Thanks!

 

About Author

Author Image
Parveen Kumar Yadav

Parveen is an experienced Java Developer working on Java, J2EE, Spring, Hibernate, Grails ,Node.js,Meteor,Blaze, Neo4j, MongoDB, Wowza Streaming Server,FFMPEG,Video transcoding,Amazon web services, AngularJs, javascript. He likes to learn new technologies

Request for Proposal

Name is required

Comment is required

Sending message..