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, C#, Java and Ruby in our previous articles, but now in Perl. This will also be the conclusion of this series and I certainly hope you enjoyed it!
Now let’s look at 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 have a look at the challenge request that we’d like to send.
The challenge HTTP request
Our sample HTTP request, that we want to send in Perl is the following:
This will use our API with API key yourapikey, URL http://www.google.com and turn it into an encrypted PDF with user password htm2pdf.
Coding the conversion in Perl
In our API documentation we used something like this.
use File::Fetch; my $apikey = 'yourapikey'; my $url = "https://www.google.com"; my $up = "htm2pdf"; my $ff = File::Fetch->new(uri => "https://api.htm2pdf.co.uk/urltopdf?apikey=$apikey&url=$url&userpass=$up"); my $where = $ff->fetch( to => '/tmp/mypdf.pdf');
Now let’s see what we’ve got.
The code starts with the inclusion of File::Fetch. We won’t be counting that as a real line, since we said in the prerequisites that we won’t the count inclusion of modules. Then the code breaks up the parameters apikey, url and userpass in separate lines and then uses them in the declaration of a new instance of File::Fetch. Then the result is received into the file /tmp/mypdf.pdf in the next line.
Now here’s a little catch – we said we wouldn’t be counting the storing in the file of the result. In this situation, the sending of the request as well as storing the result is actually in that one line.
So how can shorten this? If we wouldn’t break up the parameters in separate lines and forget about including the module, we would get the following:
my $ff = File::Fetch->new(uri => "https://api.htm2pdf.co.uk/urltopdf?apikey=yourapikey&url=https://www.google.com&userpass=htm2pdf"); my $where = $ff->fetch( to => '/tmp/mypdf.pdf');
That’s only two lines!
Now we can start the debate about whether or not we can remove an extra line, due to the requirements that we laid out. Or we can just say :
WOW! Only two lines of code needed to get a PDF of the highest quality including conversion options!
So we’ll do that instead of argue with you 🙂
This is the end of our series on converting HTML to PDF in one line of code. We set a high goal and sometimes we could achieve it e.g. when we called the API in PHP and Ruby in just a single line or come very close, like today where we could do it in only two lines with Perl or recently when we did it in two lines with C# / .NET.
So I’d like to think that our API could not be much easier to use and therefore quicker to deploy. I hope you enjoyed the series and that you’re at least a little bit impressed by the power of our service.