DEV Community

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

Posted on

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.


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.


Photo by Wei Zeng on Unsplash

Discussion (2)

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.

immannino profile image
Tony Author

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.