Posted by & filed under HTML to PDF API, HTML to PDF conversion.

Converting a HTML web page into a PDF can be easily done in Java with our HTML to PDF API. We’ll show you the basic steps and point you to all options you can use to customize the conversion process.

You don’t need to install any additional libraries or anything. All you need to do is sign up for our service and you’ll get an API key. Then you’ll just call our API over a HTTP connection and we’ll send you the converted PDF in return. You’ll be up and running in minutes!

Example code for HTML to PDF conversion in Java

Suppose you’d like to convert the web page ‘http://www.digg.com’ to PDF, just because Digg is awesome. We’ll use ‘yourapikey’ as the example API key in the code below. You’d use the following simple statements to get this job done.

String apikey = "yourapikey";
String url = "http://www.digg.com";
File outs = new File("C:\\temp\mypdf.pdf");

URL u = new URL("http://api.htm2pdf.co.uk/urltopdf?apikey=" + apikey + "&url=" + url);
URLConnection uc = u.openConnection();
BufferedInputStream is = new BufferedInputStream(uc.getInputStream());
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(outs));

byte[] b = new byte[8 * 1024];
int read = 0;
while ((read = is.read(b)) > -1) {
bout.write(b, 0, read);
}
bout.flush();
bout.close();
is.close();

As you can see you’ll set up a connection to the URL of our API service and pass the ‘apikey’ and ‘url’ parameters to get the job done. Easy peasy. That’s really all there is to it. If you want to use more options, like watermarking, pdf encryption, different page formats or what not – you just add extra parameters to the URL and it will work.

The options you can add can be found in our online documentation. If you are excited to get going now – Sign up here!

One Response to “how to convert a HTML web page into a PDF file using Java”

  1. adrianart.com

    This specific posting, “how to convert a HTML web page into a PDF file using Java” displays
    the fact that u actually know what u r speaking about!
    I really fully agree. With thanks -Abbie

Leave a Reply

You must be logged in to post a comment.