Skip to content

Instantly share code, notes, and snippets.

@tiberiucorbu
Last active August 29, 2015 14:02
Show Gist options
  • Save tiberiucorbu/de33c5c83ed01f32e9dc to your computer and use it in GitHub Desktop.
Save tiberiucorbu/de33c5c83ed01f32e9dc to your computer and use it in GitHub Desktop.
Middleman - Pdf Tilt template engine

Middleman - PDF template engine

This article explain how to configure PDF-Tilt in a middleman project.

After hours of investigations it turned out to be easier than expected, initially I have tried to make an extension and register a custom made engine into tilt, after numerous fails google came in handy once again with pdf-tilt implementation.

Note: pdf-tilt relies on PDFKit which in its turn relies on wkhtmltopdf. wkhtmltopdf needs an X server (screen), and in my case using Ubuntu Saucy some dependencies for the patched (full feature) version ware hard to be satisfied, I found no solution so far.

For a working solution here is what you can do:

Add the gems into projects Gemfile :

${projectPath}/Gemfile

...

gem 'pry'

gem 'tilt-pdf' 

...

Pull ruby libraries

shell in ${projectPath}

$ bundle install

Install wkhtmltopdf on your system:

shell

$ sudo apt-get install wkpdftohtml 

For a more detailed installation instructions or other OS please refer to : http://wkhtmltopdf.org/downloads.html

Disable the layout for pdf files in the project config.rb file

${projectPath}/config.rb

...

page "*.pdf", :layout => false 

...

The preparation is ready, now you can create your source file for the pdf, add in your project sources a file named test.pdf.rpdf.

This file is actually a yml format file which links together what is going to be send as parameters to wkpdftohtml, the file format is described here : https://github.com/lloeki/tilt-pdf

In my example I striped all the unsupported parameters:

${projectPath}/source/test.pdf.rpdf

page-size: A4
main: index.haml
stylesheets:
  - stylesheets/pdf.css

Run your server and check.

Browser

http://localhost:4567/test.pdf

A last note : if a parameter is unrecognized wkpdftohtml will throw an error, look into server console for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment