How to convert an HTML page to an Image with good quality on Linux
Posted By : Parveen Kumar Yadav | 21-Jul-2015
There are following ways available to covert an Html page to an Image:-
1)using Java2DRenderer
2)using Wkhtmltoimage
3)using Iecapt
4)using Khtml2png
5)using PyWebShot
6)using CutyCapt
As i tried 3-4 from them,i found some disadvantages as well Like:-
- Wkhtmltoimage Fails many times due to unknown reasons with an error saying "Painter not active" .
- PyWebShot opens up an annoying browers every time on the desktop.
- And some will not give you good quality or fails at CSS3 fonts.
Also Java2DRenderer is working good for me, but i found some text and image quality issue with that as well.
So finally i found CutyCapt is best fit for my requirement for image quality and also good for generating SVG (Scaled Vector Graphics).
CutyCapt:- is a small cross-platform command-line utility to capture WebKit's rendering of a web page into a variety of vector and bitmap formats.
So here i discuss how to install and use CutyCapt on linux:-
Follow the steps to Install CutyCapt on Debian , Ubuntu or Linux Mint:-
$ sudo apt-get install cutycapt
Follow the steps to Install on Fedora:-
$ sudo yum install subversion qt-devel qtwebkit-devel gcc-c++ make
$ svn co svn://svn.code.sf.net/p/cutycapt/code/ cutycapt
$ cd cutycapt/CutyCapt
Before compilation on Fedora, you need to patch source code as follows:-
Open CutyCapt.hpp with a text editor, and add the following two lines at the beginning of the file.
#include <QNetworkReply>
#include <QSslError>
Then hit qmake command as follows.
$ qmake-qt4
$ make
$ sudo cp CutyCapt /usr/local/bin/cutycapt
Finally to convert an Html to image, type the following command into terminal:-
$ cutycapt --url=http://www.demourl.com --out=demo.png
where
--url:-is for giving any url
--out:-for output File name, set format according to your need i set png you can set .jpg , .svg , .gif etc.
In this way it will create an image from Html named as demo.png.
you can also set the width, height ,delay and so many properties of this to check just simply write in your terminal:-
cutycapt help
And this will show you available option with usage.
And if you want to use this into your java project then simply execute this command via taking reference of Runtime as follows:-
Runtime runtime = Runtime.getRuntime()
String runtimeCommand="cutycapt --url=file://"+filePath+fileName+".html --min-width=540 --min-height=268 --zoom-factor=2.0 --out="+filePath+fileName+".png"
try{
Process croppingProcess = runtime.exec(runtimeCommand)
int processOutput = croppingProcess.waitFor()
if(processOutput == 0){
success = true
File file = new File(filePath+fileName+".png");
result = file
}
else{
success = false
result = Properties.SERVER_ERR_TECHNICAL
log.debug("There is some error in extracting thumbnail command.")
}
}
catch(Exception e){
log.debug(e)
}
In this way you can convert any html into Image.
Thanks!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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