Generate PDF of Jekyll Page
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
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` end
There are two minor downsides to this approach:
It doesn’t work when the site is first booted using
jekyll sbecause the server starts only after the hooks run. At that point
localhost:4000isn’t up yet so the command fails. It doesn’t stop the boot however.
It doesn’t entirely work on
jekyll bbut 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
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.
Get in touch
Or send me feedback. It will take less than 20 seconds.