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# and Java in our previous articles, but now in Ruby.
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 infamous HTTP request, that we want to send in Ruby today is the following:
http://api.htm2pdf.co.uk/urltopdf?apikey=yourapikey&url=http://www.google.com&userpass=htm2pdf
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 Ruby
In our API documentation we used code similar to the following to do this.
require 'net/http' apikey = 'abcde12345' url = 'http://www.google.com' userpass = 'htm2pdf' uri = URI("http://api.htm2pdf.co.uk/urltopdf?apikey=#{apikey}&url=#{url}&userpass=#{userpass}") Net::HTTP.start(uri.host, uri.port) do |http| request = Net::HTTP::Get.new uri.request_uri http.request request do |response| open 'mypdf.pdf', 'w' do |io| response.read_body do |chunk| io.write chunk end end end end
Now this is quite a bit of lines so let’s break it down.
The code starts with the inclusion of net/http. Well right off the bat – we’re not counting that line as we agreed in the prerequisites (inclusion of modules). Then it breaks up the parameters apikey, url and userpass in separate lines and then feeds them to a URI after which this is fed to the Net module and then the request is executed HTTP::GET.
After that we’ll have the lines that move the result of the request to a file.Also – we’ll not be counting those as we said we’d only SEND the HTTP request in our example…
So how can shorten this? If we wouldn’t break up the parameters in separate lines and forget about including the module and feeding the response to a file, we would get the following:
uri = URI("http://api.htm2pdf.co.uk/urltopdf?apikey=yourapikey&url=http://www.google.com&userpass=htm2pdf") Net::HTTP.start(uri.host, uri.port) do |http| request = Net::HTTP::Get.new uri.request_uri
We’re getting close! Only three lines left.
Now after some more searching and searching we think we found the holy grail! Apparently, you can just do the following in Ruby and you’re done:
result = Net::HTTP.get(URI.parse('http://api.htm2pdf.co.uk'), '/urltopdf?apikey=yourapikey&url=http://www.google.com&userpass=htm2pdf')
Conclusion
In the previous posts we could convert HTML to PDF in one (PHP) or two (C#) lines, but we didn’t manage to do it in Java so quickly. In Ruby we succeeded again! Just one single line and you’ll get a great looking PDF from our simple API.
Python, Perl and maybe some other programming languages will be next on the menu of this series and hopefully with similar success.
Stay tuned!
For now – if you want to learn more about the HTML to PDF API – have a look at the documentation! If you want to use it – SIGN UP!