DEV Community

Cover image for Building a HTML to PDF API using Go and wkhtmltopdf
Tony
Tony

Posted on • Edited on • Originally published at engineering.sharebuilder401k.com

Building a HTML to PDF API using Go and wkhtmltopdf

Generating PDFs in code is difficult. Many solutions revolve around unfamiliar proprietary APIs that require pixel perfect placement of text and images that can become a nightmare in the long run.

This tutorial uses HTML, CSS, and Go to achieve a lightweight, modern approach to dynamically creating and serving PDFs from an API endpoint.

Introduction

This tutorial follows how our team replaced our exsting PDF generation system. The decision for us to use wkhtmltopdf was primarily driven by our use of Golang on the backend and the
allure of keeping our services as small as possible.

I recognize some teams may be interested in a Chromium/Puppeteer approach to PDF's and I've linked some helpful articles to get you started.

Continue reading on the ShareBuilder 401k Blog ->

You can checkout the template golang + go-wkhtmltopdf project if you'd like to skip straight to code.


Cheers,
Tony

Photo by Wei Zeng on Unsplash

Top comments (3)

Collapse
 
sloan profile image
Sloan the DEV Moderator

Hi there, we encourage authors to share their entire posts here on DEV, rather than mostly pointing to an external link. Doing so helps ensure that readers don’t have to jump around to too many different pages, and it helps focus the conversation right here in the comments section.

If you choose to do so, you also have the option to add a canonical URL directly to your post.

Collapse
 
rafaelgfirmino profile image
Rafael Firmino

Chromium/Puppeteer for my application is very slowly in lambda. In furst time 6 or 9 seconds for generate one pdf.

Collapse
 
immannino profile image
Tony

I can believe it! We have a puppeteer web crawler that we run daily in a lambda to audit our sales funnel's and it is fairly chunky. I think the docker container for it is at least 1.3 GB. Our wkhtmltopdf one is ~90mb tops. We haven't tried running a lambda image with wkhtmltopdf yet but it would be a good test.