Ognjen Regoje bio photo

Ognjen Regoje

I make things that run on the web (mostly).
More /ABOUT me.

me@ognjen.io Twitter LinkedIn Github

Generate PDF of Jekyll Page

Updated post

I strongly suggest you follow the new way instead:

Original post

I wanted to generate a PDF of a page rendered in Jekyll.

I found a gem called jekyll-pdf but it seems to have been abandoned.

The alternative I came up with was to add a hook that triggers once Jekyll re-renders the site that executes wkhtmltopdf directly.

That turned out to be very simple to do:

# _plugins/pdf.rb

Jekyll::Hooks.register :site, :post_write do |page|
  `wkhtmltopdf http://localhost:4000/full full.pdf`

There are two minor downsides to this approach:

  1. It doesn’t work when the site is first booted using jekyll s because the server starts only after the hooks run. At that point localhost:4000 isn’t up yet so the command fails. It doesn’t stop the boot however.

  2. It doesn’t entirely work on jekyll b but it doesn’t overwrite the file so it’s good enough.

Both could be solved by running wkhtmltopdf on the generated local file (in this case that’d be _site/full.html). But wkhtmltopdf is a pain to get to work with local files. Because it requires absolute paths for all assets it would mean that there would have to be a pre-processing step where all the paths are prepended with . in order to make them relative to the current directory.

#jekyll #technical #wkhtmltopdf