Matt Gornick

Using PDFKit on Heroku

Posted in Uncategorized by mgornick on August 15, 2010
Recently, I've been working on a small project that crawls the web to gather information about trademarks (e.g. number of Google results, placement of sponsored links, etc.).  In the requirements, we needed to have PDF versions of the webpages that were crawled, so I found this to be a great opportunity to try PDFKit and try hosting the project on Heroku.  Initially I got everything working locally, but once I tried running the app on Heroku it didn't have access to the wkhtmltopdf library.  I've outlined my steps to get the PDFKit gem working on Heroku.

1. Setup PDFKit to work locally (gem install pdfkit, added the config.gem lines to your environment.rb)
2. Download the wkhtmltopdf-0.9.9 Linux Static Binary (amd64) executable (via http://code.google.com/p/wkhtmltopdf/downloads/list) and place it inside your vendor folder of your Rails project.
3. Create an initializer, so in production Heroku will use this to generate the PDF's.  I put this inside of config/initializers/pdfkit.rb

</div><div><div># config/initializers/pdfkit.rb</div> <div>PDFKit.configure do |config|</div><div>  config.wkhtmltopdf = Rails.root.join('vendor', 'wkhtmltopdf-amd64').to_s if RAILS_ENV == 'production'</div><div>end</div></div><div>

4. (In-progress) We are using the functionality of PDFKit to generate a PDF of an external webpage (e.g. a PDF of www.google.com) and not a view that resides in our Rails app.  This is pretty easy to do with PDFKit via PDFKit.new("www.google.com").  The problem is that we want the user to be able to download this file and have it generated at that time.  Currently, our code is just exporting the PDF to file and storing it to the /tmp directory on Heroku.  Then we use send_file to pass the PDF back to the user.  There must be an easier way to do this.  Anyone have any ideas?

If you'd like to fork this project, check it out at http://github.com/mgornick/Trademark-Search.  We're using Capybara, Culerity, Nokogiri, and PDFKit.

Advertisements