PDF Generation On The Web

Our project to build a PDF generator explained step-by-step and based on Golang, the different technologies we used, the challenges to face in details.

As the Wiredcraft team has been working on the Myanmar voter registration system for several months, we’ve spoken about our work with the Township Voter Registration and the “Check My Name” tools on our blog. For part of the system, we wanted a native Windows application that could work offline, in the Myanmar language, and generate a PDF from a list of voters.

Challenges for the Project:

Native application needs to run on Windows

Client needs to run offline due to limited Internet connection in Myanmar

Needs to generate a single PDF of 1000 pages with web technology

PDF text should be in Myanmar language (and there is not much font support for the Myanmar language)

wkhtmltopdf: wkhtmltopdf and wkhtmltoimage are open source (LGPLv3) command line tools to render HTML into PDF and various image formats using the QT Webkit rendering engine.

Pandoc: If you need to convert files from one markup format into another, pandoc is your swiss-army knife.

We didn’t have a server running somewhere in our stack, but luckily we had node.js/io.js in Electron. All the tools above have a command interface that we could start a child_process to run it, and send back the progress with a callback function.

PhantomJS

The first thing we tried is PhantomJS together with a rasterize.js, which rasterizes a web page to an image or a PDF:

This could solve the second problem, but the UX is even worse. We have to open two unnecessary windows just to generate a PDF. The UX is totally unacceptable. Plus, the user may want to generate several PDFs at the same time, which means that it would automatically open many windows.

The code here just opens a new BrowserWindow in the background, then renders the template. Once the content is fully loaded, it prints the content to a PDF, and finally, we can write it to the local file system.

Share this post

Scan to open in WeChat

Stay tuned

Follow our newsletter or our WeChat account; every other week you'll receive news about what we're up to, our events and our insights on digital products, omnichannel, cross-border ecommerce or whatever the team fancies.