A lot of web applications require generating PDF documents such as invoices, reports, cards etc. There are a lot of PDF libraries for variety of programming languages and platforms. You’d pick one up and start churning out documents right? Wrong!. PDF is a bizarre format, creating one often involves weird APIs and dependencies.
Given that you already have a web application, it makes sense to generate documents in HTML and then convert them to PDF files. There are a lot of solutions for this. Some do a half assed job on rendering the HTML. Some generate image based PDF files where files are big, users can’t select text or easily annotate stuff.
Restpack has a great web page capturing service that renders perfect documents and generates structured PDF.
It is as easy as calling
https://restpack.io/api/screenshot/v2/capture?access_token=TOKEN&url=http://mysite.com/invoice.html&format=pdf
That’s it, you have a PDF render of supplied url
at this point. Let’s dig deeper though.
You obviously need a Restpack account. If you don’t have one, go ahead and register your account.
Now, just go ahead and copy your access token, it looks something like Cziyt0MiapHBgLKKOuqDgC6a11C44RdmZay7qg99qb6LAoMH
and can be found at the tokens page.
Since we’ll convert your HTML documents to PDF, you need publicly accessible URLs for these files. If you don’t want to keep them public, you can always whitelist our IP addresses, or even better, add a validation signature to your document urls that expire after a while. Our servers will need to access them once anyway.
Let’s say you need to send an invoice to a user via email. This invoice is accessible at
http://mysite.com/invoices/1
Now you only need to call https://restpack.io/api/screenshot/v2/capture
with querystring parametes
http://mysite.com/invoices/1
YOUR TOKEN
Here’s a sample Node.JS code snippet that uses request
module (which is fantastic BTW, if you need to access http services like Restpack, it’s a must have)
var request = require("request");
var options = { method: 'GET',
url: 'https://restpack.io/api/screenshot/v2/capture',
qs: {
url: 'http://mysite.com/invoices/1',
access_token: 'TOKEN',
format: 'pdf'
},
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
// body is a PDF file
});
Note that, while our topic here is PDF, you can ask for an image file, such as a PNG. There are also options for width and height settings. Go ahead and check the documentation. We have samples for other languages and a live preview tool so you can check the render quality.