Generate Hugo website as a PDF

I’ve done it recently, and this is how I’ve done it:

There’s a page in Hugo, with a different layout, that combines all pages and outputs an HTML that is suitable for PDF. Then, this is the [simplified] build script I run each time I want to update my website:

hugo --cleanDestinationDir --minify
cat ./public/all-content/index.html | wkhtmltopdf --outline-depth 2 --enable-internal-links - ./public/downloads/all-content.pdf
rm -r ./public/all-content

I’ve also excluded the source HTML page from the sitemap.

Most of the styles in the PDF file will come from the HTML file, but some that are specific to PDF, like margins, can be set using a YAML settings file for wkhtmltopdf.

I’ve also piped a few SED’s to the script above to adjust the HTML content for PDF conversion (e.g., replacing iframes with fixed images), but you can also do it in Hugo when setting up that page.

4 Likes