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

Converting HTML to PDF in Java is very simple with our easy-to-use HTML to PDF API. In this post we will show you some basic examples of how you can use the PDF API in Java and what kind of options you can use for the conversion as well as additional options like password protection and watermarking.

Our HTML to PDF API works by sending a HTTP GET request. You’ll need an API key and then you’ll be ready to use it in any language you want, so also Java, which we’ll be discussing now. In this post we’ll assume your api key is ‘yourapikey’. You’ll get your own key when you sign up for a HTML to PDF api plan.

A basic URL to PDF conversion of http://www.google.com is done by sending the following HTTP request:
[html light=”true”]http://api.htm2pdf.co.uk/urltopdf?apikey=yourapikey&url=http://www.google.com[/html]
In the above example you’re converting the URL ‘http://www.google.com’ into a PDF, which you’ll receive from us. The conversion is done with the defaults that you have in your own members area. If you want to use different settings for the conversions you will append other parameters to the HTTP request, which we’ll show you later on this post.

Now let’s see how you would do this URL to PDF conversion in Java.
We’ll assume you want to store the PDF we send you in the file ‘mypdf.pdf’:

String apikey = "yourapikey";
String url = "http://www.google.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();

From the above example you see it’s really nothing more then forming a URL that calls our API, setting up the connection and writing the results to your output stream.

The most important command in the example is:

URL u = new URL("http://api.htm2pdf.co.uk/urltopdf?apikey=" + apikey + "&url=" + url);

That command controls the way the API is called. If you want to pass additional settings to the API for the HTML to PDF conversion then you’ll have to do that here by appending them to the URL.

There are a lot of conversion settings you can use with our API – you can find a detailed overview of them in the description of the HTML to PDF API. There are basicly three types of settings:

  1. Settings for the actual conversion itself – page format, single page PDF, margins, header and footer etc
  2. Settings for security and protection of the resulting PDF like password protection, encryption etc
  3. Settings for watermarking or stamping of the resulting PDF

Now let’s look at an example where you want to convert a URL to PDF and you’d like get a password protected PDF from us. You’d then need to pass two additional parameters to the API: the owner password and the user password. The owner password is the password that controls the security settings of the PDF and the user password is the password that you give to the user who needs to open the document.

You would need to append these two options to the URL we already had and to do this you just change the first couple of lines of the code to:

String apikey = "yourapikey";
String url = "http://www.google.com";
String op = "verysecret";
String up = "alsosecret";
File outs = new File("C:\\temp\mypdf.pdf");

URL u = new URL("http://api.htm2pdf.co.uk/urltopdf?apikey=" + apikey + "&url=" + url + "&ownerpass=" + op + "&userpass=" + up);

This will get the job done and get you PDF protected with owner password “verysecret” and document open password “alsosecret”.

Hopefully this shows you how easy it can be to convert HTML to PDF in Java with our HTML to PDF API. If you want to learn more about the PDF API then please check out the documentation. If you want to use it then please sign up for a HTML to PDF API plan.

Leave a Reply

You must be logged in to post a comment.