PDF Generation in Javascript

Krzysztof Kalamarski

PDF is one of the most commonly chosen document formats to share information. One of its main advantages is portability (hence the name: PDF – Portable Document Format), which means that it will display in the same manner on all the devices, no matter what operating system is running and what program is used to open it. Due to its simplicity of usage, many clients request a feature that will enable users to download some kind of information in PDF format from their web app.

From the technical standpoint though, it’s a little bit more complicated. It can contain almost anything – from simple text to interactive elements like links, inputs and even videos. To make that possible, Adobe (the creator of the format) has used a PostScript language, which is a powerful yet quite intimidating tool. To give you some example, here is a simple Hello World written in PostScript (source: Wikipedia):

 /Courier             % name the desired font
 20 selectfont        % choose the size in points and establish
                      % the font as the current one
 72 500 moveto        % position the current point at
                      % coordinates 72, 500 (the origin is at the
                      % lower-left corner of the page)
 (Hello world!) show  % stroke the text in parentheses
 showpage             % print all on the page

Most of us, web developers, probably don’t know PostScript well enough to be able to quickly generate a simple PDF file, not to mention some more advanced documents.

So, is it even possible to programmatically generate PDF file using web technologies? Spoiler alert – it is. And it’s probably much easier than you thought.