Home » Programming » Archive by category "Ruby"

Convert HTML to PDF with HTML2PDF Web Service

HTML2PDF Web ServiceRecently I launched my new product HTML2PDF Web Service — a web service for converting HTML to PDF.

In this post I’d like to talk about HTML2PDF Web Service. Why to choose it, how to use it and what technologies were used to create it.

Why Choose HTML2PDF Web Service?

Programmatically generating PDF documents is a painful and time consuming problem that neither makes your developers nor designers happy. With HTML2PDF Web Service you can design your invoices or reports in HTML, style them with CSS and convert the resulting page into a PDF document. Using HTML2PDF Web Service saves your developers and designers time which is better spent making your product better.

Say your web application or mobile app (or any application for that matter) needs to generate invoices or reports in PDF format. Unless you can install special HTML to PDF conversion software you’re probably stuck with some of the libraries available for your language that can programmatically generate PDF documents. To do this you would probably design your document in something like MS Word, LibreOffice Writer or perhaps HTML. After this design has been approved you can start programming your PDF module; setting up coordinates, font sizes etc. And then all of the sudden you notice your library has limited support for doing actual document layouts and presenting tabular data that can span multiple lines. Now you need to write your own routines for splitting text over multiple lines, keep track of coordinates and make sure nothing overlaps. If like me you’ve already been there, it’s quite the nightmare.

So being able to design in HTML, style with CSS (heck, even use a bit of JavaScript) and convert the resulting page to PDF would speed up this process a lot. Am I starting to tickle your interest?

How to use HTML2PDF Web Service

Simply create your soon to be PDF documents in HTML, style them with CSS and if wanted you can use JavaScript as well. The final document is best previewed in a WebKit based browser such as Google Chrome, since that’s the technology HTML2PDF Web Service uses in the background to render the HTML and convert it to PDF.

Here are some examples on how to call the web service. Converting HTML to PDF is easy with the HTML2PDF Web Service. You can pass an URL to the page you want to convert or either send the HTML code with the request.

cURL

$ curl -H "X-API-Key: F8802062-4D31-11E3-8F59-BFD4058B6BFF"
       -H "X-API-Username: MyUsername"
       -d '{"content":"<html><head><title>My page</title></head><body><h1>Hello World!</h1><p>I am an HTML page converted to PDF!</p></body></html>"}'
       https://html2pdfwebservice.com/api/convert > page.pdf

Perl

#!/usr/bin/env perl
use strict;
use warnings;
use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new;
my $tx = $ua->post(
    'https://html2pdfwebservice.com/api/convert' => {
        'X-API-Username' => 'MyUsername',
        'X-API-Key'      => 'F8802062-4D31-11E3-8F59-BFD4058B6BFF'
    } => json => {url => 'http://domain.com/invoice.html'}
);
if (my $res = $tx->success) {
    my $pdf_data = $res->body;
}

Ruby

require 'net/https'
require 'uri'

uri           = URI.parse('https://html2pdfwebservice.com/api/convert')
https         = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
# In case the SSL certificate isn't accepted
https.verify_mode = OpenSSL::SSL::VERIFY_NONE

req = Net::HTTP::Post.new(uri.path)
req['X-API-Username'] = 'MyUsername'
req['X-API-Key']      = 'F8802062-4D31-11E3-8F59-BFD4058B6BFF'
req.body              = '{"url": "http://domain.com/invoice.html"}'

res = https.request(req)
if res.code == '200'
    pdf_data = res.body
    # - or write to file -
    # File.open('invoice.pdf', 'w') { |file| file.write(res.body) }
end

PHP

$settings = array(
    'url' => 'http://domain.com/invoice.html',
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($settings));
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'X-API-Username: MyUsername',
    'X-API-Key: F8802062-4D31-11E3-8F59-BFD4058B6BFF'
));

curl_setopt($curl, CURLOPT_URL, 'https://html2pdfwebservice.com/api/convert');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Helps to debug in case of issues
// curl_setopt($curl, CURLOPT_VERBOSE, 1);

// In case the SSL certificate isn't accepted because of outdated certificates
// on your server
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$res = curl_exec($curl);
// Save PDF to disk
file_put_contents('document.pdf', $res);
curl_close($curl);

Technologies used to develop HTML2PDF Web Service

The most interesting part in developing HTML2PDF Web Service was choosing which technology to use for converting HTML to PDF. After doing research on the subject and testing several solutions I eventually went with a WebKit based solution. By using WebKit it’s easier for the end user to preview their document using a WebKit based browser.

The HTML to PDF conversion server was developed using Go. Go is a fun language to program with, does concurrency in a really nice way and can produce a native executable for Linux, OS X, Windows and some other platforms. Thanks to Go the conversion server is fast, snappy and low on memory and CPU usage. Being able to create a binary executable allows me to sell the conversion server as a standalone product as well.

To get access to the web service there’s also a web application which is written in Perl. My favorite web framework of choice has become Mojolicious for quite some time now and thus HTML2PDF Web Service has been written with it. DBIx::Class has been used for database interaction and Validation::Class is used to validate all user inputted data.

Used databases are PostgreSQL and Redis. The former is used to store user accounts, subscriptions and more. The latter is used to keep track of token usage per user.

Sign up now for a free trial

If after reading all this and you’re still reading, please do sign up for a free trial. The trial gives full access to all the features of the web service so if you like it, please consider buying a subscription.

In case of any questions, please do contact me either through the comments on this page or send an e-mail to support at support@html2pdfwebservice.com.

Help beta test an HTML2PDF Web Service

In an earlier post I asked if anyone would be interested to help me out test a web service for converting HTML to PDF. Today I’m opening up the beta to anyone that’s interested.

Please visit https://html2pdfwebservice.com/ and sign-up for a 7-day trial account. No credit card required! Trial length can be extended upon request.

Converting HTML to PDF is easy with the HTML2PDF Web Service. Here are some examples:

cURL

$ curl -H "X-API-Key: F8802062-4D31-11E3-8F59-BFD4058B6BFF"
       -H "X-API-Username: MyUsername"
       -d '{"content":"<html><head><title>My page</title></head><body><h1>Hello World!</h1><p>I am an HTML page converted to PDF!</p></body></html>"}'
       https://html2pdfwebservice.com/api/convert > page.pdf

Perl

#!/usr/bin/env perl
use strict;
use warnings;
use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new;
my $tx = $ua->post(
    'https://html2pdfwebservice.com/api/convert' => {
        'X-API-Username' => 'MyUsername',
        'X-API-Key'      => 'F8802062-4D31-11E3-8F59-BFD4058B6BFF'
    } => json => {url => 'http://domain.com/invoice.html'}
);
if (my $res = $tx->success) {
    my $pdf_data = $res->body;
}

Ruby

require 'net/https'
require 'uri'

uri           = URI.parse('https://html2pdfwebservice.com/api/convert')
https         = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
# In case the SSL certificate isn't accepted
https.verify_mode = OpenSSL::SSL::VERIFY_NONE

req = Net::HTTP::Post.new(uri.path)
req['X-API-Username'] = 'MyUsername'
req['X-API-Key']      = 'F8802062-4D31-11E3-8F59-BFD4058B6BFF'
req.body              = '{"url": "http://domain.com/invoice.html"}'

res = https.request(req)
if res.code == '200'
    pdf_data = res.body
    # - or write to file -
    # File.open('invoice.pdf', 'w') { |file| file.write(res.body) }
end

PS: Prices are subject to change. During the beta you can’t use your own credit card for payments since we’re still running in sandbox mode. All data will be wiped after the beta ends. Expected launch date will be some time in January 2014.

How to catch and test sent e-mails in your app

A nice gem (literally, it’s a Ruby program) I stumbled upon yesterday is called MailCatcher. Using MailCatcher you can catch all the mail that’s being sent by the app you’re developing and (re)view their contents.

MailCatcher makes this possible by running (or rather being) a simple SMTP server. All you’ve got to do is direct your app to use the SMTP server provided by MailCatcher. Mail sent with this SMTP server stays inbound so you don’t have to worry a test mail is receiving by a customer.

It also provides a neat web interface in which you can review the mails that have been sent. Aside from viewing the contents of the mail you can also do further inspection on it by taking a deeper look into the source of the mail to view headers and such.

Screen shot 2011-06-23 at 11.39.03 PM(source: mailcatcher.me)

By using WebSockets the webinterface immediately updates when a mail has been sent. You can also download the e-mail to load it up in your e-mail client. This can be useful to review HTML based e-mails to see how they render.

If you’re using Vagrant I suggest you add the port the webinterface runs on to the forwarded ports list. Also run mailcatcher with --http-ip 0.0.0.0 so you can reach the webinterface from the host OS.

Server provisioning: my experience with Puppet and Chef

Server provisioning tools such as Chef and Puppet make it easy to automate installation and configuration of your servers. I’ve used both now in two projects where they provisioned my virtual servers built with Vagrant.

I’ve first heard of Chef and Puppet when I started using Vagrant. With Vagrant you can quickly get a virtual server up and running for your project. In its most basic form Vagrant can execute a shell script in which you setup your server. Because this is a bit limiting it’s interesting to look into a more automated provisioning process.

Just over a year ago I started using Vagrant for Maximus and after giving both Chef and Puppet a quick review I decided to go with Puppet. At the time I thought it looked easier to use and it had lots of modules available for installing software.

Earlier this week I decided to add Vagrant to a new project of mine: a small and simple CRM system which is adapted to my workflow, and decided to give Chef (Solo) a try. At first Chef looked like a lot more hassle than Puppet, but that’s mainly because you start off with a big chef directory just to get started and cookbooks have a folder hierarchy of their own as well. In contrast, with Puppet modules basically have a files, manifests and templates directory.

If I compare my experience between Puppet and Chef I had more trouble setting up my Puppet manifests than I did setting up my Chef cookbooks and recipes. I’ve found Chef cookbooks easier to use than Puppet modules. Mainly because configuring software with Chef seemed a lot easier. Maybe that’s because the available Chef cookbooks actually provide this functionality as it seemed totally absent in most Puppet modules I’ve come across. Maybe it was there but I couldn’t find it. That’s another thing, the Chef cookbooks seem to be better documented than the Puppet modules I’ve seen. One thing I do like about Puppet is that you can define dependencies between every step or command you want to run. I’m not sure if Chef supports this as well.

Just to make clear, my experience with Puppet and Chef is limited and I currently don’t use it to provision servers used in production (at least not yet, but I’m sure that’ll change in the future). This is just based on my experience. If you’re interested in these tools I suggest you try them both to see what it’s all about. They both have a solo/standalone version which doesn’t require a main server where your cookbooks or modules are stored.

One great resource of getting started with Chef can be found at Christopher H. Laco’s website.