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

Today I’m going to continue the series on converting HTML to PDF with one line of code with our HTML to PDF API. I’ll be trying to accomplish the same we did in PHP and C# last time, but this time in Java.

Now just to summarize the ‘rules of the game’ again:

  • we’ll only send the HTTP request necessary to do the URL to PDF conversion
  • we’ll refrain from error handling (of course you’d normally do that in your real implementation)
  • we’ll not destroy created objects properly (of course you’d normally do this as well in your own implementation)
  • we’ll not count lines of code that we need to include libraries (mind you: we only use standard libraries, as our API does not require you to install anything you don’t already have)

Now let’s look at the challenge request again.

The challenge HTTP request

Our challenge HTTP request, that we will want to send in Java today is going to be the following:

http://api.htm2pdf.co.uk/urltopdf?apikey=yourapikey&url=http://www.google.com&userpass=htm2pdf

This will call our API with the API key yourapikey, the URL http://www.google.com and turn it into an encrypted PDF with user password htm2pdf.

Coding the conversion in Java

In our API documentation we used code similar to the following example code to get this done.

String apikey = "yourapikey";
String url = "http://www.google.com";
String userpass = "htm2pdf";

File outs = new File("C:\\temp\mypdf.pdf");

URL u = new URL("http://api.htm2pdf.co.uk/urltopdf?apikey=" + apikey + "&url=" + url + "&userpass=" + userpass);
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();

This code breaks up the parameters apikey, url and userpass in separate lines and then feeds them to a BufferedInputStream after which this is read and the results fed to a BufferedOutputStream. Now all in all those are probably the most lines of code we’ll be needing in any language to use our API. So let’s see what we can do to reduce it a little and maybe end up with just one line.

Now if we wouldn’t break up the parameters in separate lines, we’d loose 3 lines in the beginning. We would then get:

File outs = new File("C:\\temp\mypdf.pdf");

URL u = new URL("http://api.htm2pdf.co.uk/urltopdf?apikey=yourapikey&url=http://www.google.com&userpass=htm2pdf");
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();

Hmmm…. That’s just swell (Clint would say this back in the day), but we still have a lot of lines left. What can we do now?

The truth is – we’re not really Java experts and a lot of browsing on the web also indicates we’re running out of possibilities here. The lines that we need to transfer the inputstream to the outputstream can not really be shortened so we’re stuck with at least that.

So we’re gonna be stuck with approximately 12 lines of code to get the job done. But then again – you will also have the PDF stored as a file and not just in a buffer or in memory.

Conclusion

In the previous posts we could convert HTML to PDF in one (PHP) or two (C#) lines, but we didn’t manage to get that close in this post. I blame this partly on our limited knowledge of Java (yes, this is a shoutout to anyone who wants to comment!), but also on the fact that Java is a little bit more elaborate and strict than other languages. But althoug I wasn’t able to trim it down to one or two lines, it’s still only a really limited set of lines and you’ll still get a very high quality PDF from a HTML page in return!

Now, before I sign off – if you have a way of doing this in fewer lines – then please shout it out at the comments!

In the next articles in this series I’ll try to show you that you can accomplish the same in Ruby, Python, Perl and maybe some other programming languages.

Stay tuned!

For now – if you want to learn more about our HTML to PDF API – have a look at the documentation! If you want to use it – SIGN UP!

Leave a Reply

You must be logged in to post a comment.