Generating PDF from HTML using DocRaptor on Heroku

There comes a time one has to create PDFs for a Rails application. Searching the web will most likely bring you to libraries like “PDF Kit”:http://github.com/jdpace/PDFKit/ and “Wicket PDF”:http://github.com/mileszs/wicked_pdf/ that use “wkhtmltopdf”:http://code.google.com/p/wkhtmltopdf/ as a driver.

If your app is hosted on “Heroku”:http://heroku.com you wonder weather wkhtmltopdf is available so that you can use one of these awesome libraries. “Searching the Heroku docs”:http://docs.heroku.com/search?q=pdf, you will probably come to same conclusion: nothing on there!

As the Heroku support states:

You’re correct that there’s no official documentation. Most of our customers seem to be using wkhtmltopdf, generally with pdfkit. I do hope to document this usage soon.

http://github.com/jdpace/PDFKit
http://code.google.com/p/wkhtmltopdf/

If you want us to offer a Prince add-on, I encourage you to write them inviting them to check out our Add-on Provider Programme: http://addons.heroku.com/provider

Of course, you can always purchase your own license and run it on EC2 or another server of your choosing.

h2. PrinceXML

One of the libraries that were used at “my last job”:http://www.blaulabs.de/ was “PrinceXML”:http://princexml.com, which did a great jog generating PDFs from HTML pages. It “supports most of the HTML and CSS”:http://princexml.com/doc/7.0/ stuff, passing the “ACID2 test”:http://princexml.com/samples/acid2/. PrinceXML has “some additional CSS attributes”:http://princexml.com/doc/7.0/page-size/ that enable you to configure additional PDF specific layout settings.

h2. DocRaptor

Since PrinceXML is a commercial product, Heroku won’t support it and I did not find anything on the web, that would offer PDF generation as a service. Asking the “PrinceXML Forum”:http://www.princexml.com/bb/viewtopic.php?f=3&t=3820&sid=cbbae46520b98f82e259f53e350d650d I found out about “DocRaptor”:http://docraptor.com/. These guys provide a service to convert HTML to XLS or PDF over a webservice interface, extactly what I was looking for. As an additional bonus, they “just implemented a gem”:http://rubygems.org/gems/doc_raptor for supporting the Heroku Add-on interface. Mail to “Expected Behavior Support”:mailto:[email protected] if you want to participate in the private beta.

h3. Improvements

DocRaptor offers a great service, but they are still in early development. There were some Issues that were resolved recently. If you are a user of PrinceXML you probably know the _–baseurl_ option that allows usage of relative paths for images and stylesheets. DocRaptor “adds support for command line options”:http://docraptor.com/documentation#pdf_options just yet. The feature for “generating a PDF from a given URL”:http://docraptor.com/documentation#api_document_url is even better!

h3. Using DocRaptor from Rails

The latest “DocRaptor documentation for the Heroku Add-on”:http://doc-raptor-docs.heroku.com/ is decent and it provides “some nice examples”:http://docraptor.com/examples.

h4. PDF from raw HTML

Here is what I did to get it running on “my Rails 3 project”:http://phoet.de:

# Gemfile
gem "doc_raptor", "0.1.1"

# mime_types.rb
Mime::Type.register_alias "application/pdf", :pdf

# your_controller.rb
def your_pdf_action
  respond_to do |format|
    format.pdf do
      data = DocRaptor.create(:name => 'DocRaptor.pdf', :document_content => render_to_string, :document_type => "pdf", :prince_options => {:baseurl => 'http://nofail.de'})
      send_data data, :type => 'application/pdf', :filename => 'DocRaptor.pdf'
    end
  end
end

If you registered the pdf mime-type you will have to provide an additional layout for this. I added some PrinceXML specific parameters to the styles to make it a fullscreen PDF. One thing that is essential for making the stylesheets work is the _media => ‘screen, print’_ settings:

// application.pdf.haml
= stylesheet_link_tag 'style', :media => 'screen, print'
// you can provide a base tag for images and stylesheets
// %base{:url=>'http://blog.nofail.de'}

%style
  @page { size: A4 }
  @page { margin: 0px }
  @page { border: none }
  @page { padding: 0px }
  @page { prince-shrink-to-fit: auto }

h4. PDFs from an URL

The simplest solution for generating a PDF is to send an url to the service, so you can re-use all your view logic:

data = DocRaptor.create(:name => "DocRaptor.pdf", :document_url => "http://blog.nofail.de", :document_type => "pdf")
send_data data, :type => 'application/pdf', :filename => "DocRaptor.pdf"

One caveat though, you got to have at least two “dynos”:http://docs.heroku.com/dynos to serve the additional request from DocRaptor!

See a “working example on my homepage”:http://www.phoet.de/interest/curriculum/curriculum_table.pdf.

Generating PDF form HTML without the hassle, thanks to DocRaptor!

Rails, getting started without the hassle

I just changed jobs and am now a Rails developer at “tolingo.com”:http://tolingo.com, which is an online translation broker. When I started out working on my new desk, I had to setup my iMac development environment. There are tons of articles of “how to compile/install/run stuff like MySQL”:http://hivelogic.com/articles/ruby-rails-leopard, to get you started on OS X, but I think all one really needs is “Homebrew”:http://mxcl.github.com/homebrew/ and “RVM”:http://rvm.beginrescueend.com/.

h2. Homebrew

Homebrew is a Ruby based packaging tool for Mac and once you start using it, you immediately hate yourself for having wasted time on “MacPorts”:http://www.macports.org/…

“Homebrew is the easiest and most flexible way to install the UNIX tools Apple didn’t include with OS X.”

This quote is from the official website and I guess they are absolutely right!

h3. Formula

Homebrew is build around formulas. They describe how a package should be loaded from the web and installed on your system. It also cares about package dependencies, paths and all the other ugly stuff:

require 'formula'

class Wget < Formula
  homepage 'http://www.gnu.org/wget/'
  url 'http://ftp.gnu.org/wget-1.12.tar.gz'
  md5 '308a5476fc096a8a525d07279a6f6aa3'

  def install
    system "./configure --prefix=#{prefix}"
    system 'make install'
  end
end

You can easily install packages from the shell with _brew_:

brew install wget

Homebrew puts all the packages into _'/usr/local'_, so that it won't interfer with other components of your system. To get your packages working, you need to include it into your _$PATH_. If you have any problems running something, Homebrew comes with the _doctor_ command, that scans for problems in your setup!

h3. Installation

Just download Homebrew to your system and update once a while:

# install homebrew via curl
sudo mkdir -p /usr/local && sudo chown -R $USER /usr/local && curl -Lsf http://bit.ly/9H4NXH | tar xvz -C/usr/local --strip 1

# update homebrew
brew update

h3. Git, MySQL, Sphinx and more

What else do you need? Just _search_ for it or get more infos with _info_!

These are the packages that I needed for development:

# install mysql and set it up
brew install mysql
mysql_install_db
# add mysqld as launch agent
cp /usr/local/Cellar/mysql/#{MYSQL_VERSION}/com.mysql.mysqld.plist ~/Library/LaunchAgents
launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

# install git
brew install git git-flow

# add git bash completion (find path to your git with 'brew info git')
ln -s /usr/local/Cellar/git/#{GIT_VERSION}/etc/bash_completion.d/git-completion.bash ~/.git-completion.bash
source .git-completion.bash

# install sphinx search-deamon
brew install sphinx

# aspell with all spellings
brew install aspell --all

# libxml and imagemagick for sprites
brew install libxml2 imagemagick

h2. RVM the Ruby Version Manager

RVM is a command line tool for managing your local Ruby environments, you can get some more information on the "RVM homepage":http://rvm.beginrescueend.com/ and in "earlier articles":http://blog.nofail.de/tag/rvm/.

Quick start with installing RVM to your machine:

# install rvm via curl !!! FOLLOW RVM INSTRUCTIONS !!!
bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )

# download and compile latest 1.8.7
rvm install 1.8.7

# create a .rvmrc file in your app's base directory
echo "rvm use 1.8.7@#{YOUR_APP} --create" > #{YOUR_APP}/.rvmrc
# execute it by cd-ing to your app's directory
cd #{YOUR_APP}

Now you can work on your app with a custom gem environment. Unless you are using "Bundler":http://gembundler.com/, this is probably what you want for installing and removing gems painlessly.

h3. Cucumber with Celerity

Behavior driven development with "Cucumber":http://cukes.info/ works nicely with "Celerity, a JRuby implementation of a headless browser using HtmlUnit":http://celerity.rubyforge.org/ and it's companion a Ruby wrapper called "Culerity":http://github.com/langalex/culerity. Culerity has recently been updated with some configuration points for registering your local JRuby environment:

# jruby config für culerity (from http://rvm.beginrescueend.com/integration/culerity/)
rvm install jruby
rvm use jruby@celerity --create
gem install celerity
rvm wrapper jruby@celerity celerity jruby
# add to .profile
export JRUBY_INVOCATION="$(readlink "$(which celerity_jruby)")"

If you are experiencing any weird _Broken Pipe_ errors (like me), have a look at "this issue":http://github.com/langalex/culerity/issues/#issue/29.

This is just an example of how you can setup your Rails development environment. Comments on this topic are appreciated!

DZone API and iPhone app

As “I already mentioned”:http://blog.nofail.de/2010/08/using-blocks-in-objective-c/, I am currently getting my hands dirty with Objective-C and iPhone application development.

The biggest problem with getting started was that I had no idea what application I could write for that device that “could become somewhat usable”:http://www.dzone.com/links/we_want_a_mobile_dzone.html. As I am a passionate tech reader, I consume a lot of articles posted on “DZone”:http://dzone.com. Usually I use a feed reader like “NetNewsWire”:http://netnewswireapp.com/ for that, which works very well for my MacBook but is nearly useless on the iPhone, because the DZone site is not very mobile friendly…

h2. Problems

Since there was no “DZone iPhone application”:http://github.com/phoet/dzone on the marked I started working on it. Parsing DZone feeds was easy, even though the buildin XML support on iOS sucks. There were “some nice libraries”:http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project that made my life easier.

h3. No deeplink

The DZone RSS feed does not provide a deeplink to the actual linked article, so one would still land on the DZone page… Since “DZone does not provide an API currently”:http://meta.dzone.com/questions/66/dzone-api, I started working on my own Rails application hosted on Heroku. Spidering the RSS, calling the page and extracting the link to the article is fragile, but it works (currently).

h3. No voting

One of my goals was to let the iPhone user vote for the article while reading it. The lack of an API forced me to do some more fragile login and posting stuff to the DZone page, but it works too (currently)!

You can read more about the API I created on the “actual page”:http://dzone-api.heroku.com/.

h2. iPhone app

The first version of the *”dzone mobile”* app has passed the iTunes store review process and is “available through the app store”:http://itunes.apple.com/app/dzone-mobile/id389511515. A version with some minor bugfixes is currently beeing reviewed. Have a look at updates and documentation “here”:http://blog.nofail.de/dzone/ or “here”:http://github.com/phoet/dzone.

h3. voting

You have got to provide your DZone login credentials if you want to use the voting feature. Go to the iPhone Settings > DZone and add your username and password. I want you to know that there is NO SSL, so your credentials will be submitted UNSECURE!

h3. more Features

If you are interested in pushing this further, you can “add bug reports or feature requests on GitHub”:http://github.com/phoet/dzone/issues.

!http://dl.dropbox.com/u/153067/dzone_screens.png(screenshots)!

DZone iPhone sugar!

Using the Redis addon on Heroku

I am always “playing around with new addons”:http://blog.nofail.de/2010/07/mongo-ruby-driver-mongoid-and-mongomapper/ offered by Heroku. My latest discovery was the “Redis addon”:http://addons.heroku.com/redistogo that is provided by “Redistogo”:http://redistogo.com/. The addon is probably in private beta (“docs”:http://docs-beta.heroku.com/redistogo are still on beta), but since they put up a link to it on their site, I managed to install it to my “personal website”:http://www.phoet.de that runs in the cloud.

“Redis”:http://code.google.com/p/redis/ is “an advanced key-value store” and has some features that make it a perfect match for a cache! I use caching extensively on my site and keep on “trying out new ways to do it”:http://blog.nofail.de/2010/02/simple-db-caching-for-heroku/ to circumvent Heroku’s readonly filesystem.

Like “Memcache”:http://memcached.org/, Redis provides the ability to set a time to live (ttl) on a key. This comes in handy, if you have data that expires in a short period of time, like 3rd party data from Twitter etc.

h2. Caching with Redis

Accessing Redis is very simple, since it is a text based protocol. The “command reference”:http://code.google.com/p/redis/wiki/CommandReference is straight forward and there is a “simple Ruby wrapper”:http://github.com/ezmobius/redis-rb/ available:

require "redis"
redis = Redis.new
redis.set "foo", "bar"
# => "OK"
redis.get "foo"
# => "bar"

The “redis-store gem”:http://github.com/jodosha/redis-store already provides a Rails 3 compatible Cache Store implementation, but I needed some more configuration points, especially the ttl.

That’s why I wrote my own “Rails 3 Redis Cache”:http://github.com/phoet/rails_redis_cache, also a great way to get used to the way of working with Redis and the Redistogo addon.

h2. Using Rails Redis Cache

There is some configuration needed for Rails to pick up the new cache store. If you want to use different or no caching for test, development and production, you should put the config in your environment files:

# config/environemnts/production.rb
config.action_controller.perform_caching = true
config.cache_store = ActiveSupport::Cache::RailsRedisCache.new(:url => ENV['REDISTOGO_URL'])

If there is a Redis server available in all environments, you can put it in your environment file:

# config/environment.rb
ActionController::Base.cache_store = ActiveSupport::Cache::RailsRedisCache.new(:url => ENV['REDISTOGO_URL'])

The caching parts are mostly in my controllers:

@tweets = cache("tweets", :expires_in => 30.seconds){ Twitter::Search.new(...) }

The store is using the “basic Rails cache store implementation”:http://github.com/rails/rails/blob/v3.0.0.beta1/activesupport/lib/active_support/cache.rb#L104 which is broken in the Rails 3.0.0.beta1 version that runs on Heroku, so “I added a monkey-patch”:http://github.com/phoet/basement/blob/master/config/rails_cache_fix.rb for that using edge Rails.

h2. Redis on localhost

Installing and running Redis on Mac OS X is really simple:

brew install redis
redis-server

There is also a commandline client available for direct access:

redis-cli
redis> set "foo" "bar"
OK
redis> get "foo"
"bar"

It’s key value stores, stupid!

Using blocks in Objective-C

One of my pious intentions for the year 2010 is to start writing some application for the Mac. Apart from the hype about iPhone development, I think that starting out in that area is especially appealing, as it reduces the size of the API one has to learn. Getting to know the iOS libraries is a lot easier than handling the endless amount of Cocoa frameworks.

One thing that I discovered recently is the “support of blocks”:http://developer.apple.com/mac/articles/cocoa/introblocksgcd.html, that has been introduced with OSX 10.6 and iOS 4.0.

h2. the environment matters

Consuming 3rd party data from the web is kind of a pain, especially compared to how easy it is in Ruby. So I was pleased to find “Seriously”:http://github.com/probablycorey/seriously, a framework for async calls and JSON/XML parsing. The Seriously examples made use of blocks:

NSString *url = @"http://api.twitter.com/1/users/show.json?screen_name=probablycorey";

[Seriously get:url handler:^(id body, NSHTTPURLResponse *response, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    }
    else {
        NSLog(@"Look, JSON is parsed into a dictionary!");
        NSLog(@"%@", [body objectForKey:@"profile_background_image_url"]);
    }
}];

Executing this example in my app code “raised an error”:http://www.mail-archive.com/[email protected]/msg47163.html, that I could not easily understand:

"__NSConcreteGlobalBlock", referenced from: ___block_holder_tmp_1.1207 in DZoneController.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

The problem was, that XCode set my execution environment to OSX 10.5 which does not have support for blocks. To fix this, one has to update the _MACOSX_DEPLOYMENT_TARGET_ build variable.

Right Clicking on the XCode project in the “Groups & Files” view will bring up a context menu with “Get Info” (or pressing CMD+I while project is selected). The info pane has a “General” tab that lets you select the “Base SDK for All Configurations”, which I set that to iOS 4.0.
An other option is to search for “MACOSX_DEPLOYMENT_TARGET” in the “Build” tab and changing that value accordingly. Make sure “Show” is set to “All Settings”.

At least some Objective-C sugar!