Skip to content

Instantly share code, notes, and snippets.

@wrburgess
Created September 24, 2012 22:54
Show Gist options
  • Save wrburgess/3778949 to your computer and use it in GitHub Desktop.
Save wrburgess/3778949 to your computer and use it in GitHub Desktop.
Setup wicked_pdf and wkhtmltopdf with Rails 3 and Heroku

##Refs

Install wicked_pdf

Add to Gemfile: gem 'wicked_pdf'

In terminal: bundle

For local, install wkhtmltopdf

In terminal: git checkout 6e2d550 /usr/local/Library/Formula/wkhtmltopdf.rb

In terminal: brew install wkhtmltopdf

Make sure you installed wkhtmltopdf 0.9.9

For Heroku, download wkhtmltopdf

Go to wkhtmltopdf

Download the wkhtmltopdf-0.9.9 Linux Static Binary (amd64) binary

Copy the binary to ``bin/wkhtmltopdf-amd64```

Install QT

In terminal: brew install qt

Set up the Config

Create a file: config/wicked_pdf.rb

if Rails.env.staging? || Rails.env.production?
  exe_path = Rails.root.join('bin', 'wkhtmltopdf-amd64').to_s
else
  exe_path = Rails.root.join('bin', 'wkhtmltopdf').to_s
  # exe_path = '/usr/local/bin/wkhtmltopdf'
end

Render PDF in Controller

  def show
    @model = Model params[:id]

    respond_to do |format|
      format.html
      format.csv do
        model_csv = ModelCSV.new(@lesson)
        send_data(model_csv.to_csv, filename: model_csv.generate_csv_filename)
      end
      format.pdf do
        render pdf: "#{@model.uuid}",
               template: 'admin/modelss/show.pdf.haml',
               disposition: 'attachment',
               :toc => { :depth => 2, :header_text => 'TEXT', disable_links: false }
      end
    end
  end

Create a PDF View

@obromios
Copy link

Thank you this was very useful. One minor clarification is that my local wkhtmltopdf binary was stored in a different place, so my config/initializers/wicked_pdf.rb file was a bit different. As well, the full config syntax is shown below.

if Rails.env.staging? || Rails.env.production?
  WickedPdf.config = {:exe_path => Rails.root.join('bin', 'wkhtmltopdf-amd64').to_s}
else
  WickedPdf.config = { :exe_path => '/usr/bin/wkhtmltopdf'}
end

@kathgironpe
Copy link

If you use Rails Config gem. It is much cleaner.

   wkhtmltopdf_path: "<%= Rails.root.join('bin', 'wkhtmltopdf-amd64').to_s %>"

@jancel
Copy link

jancel commented May 2, 2015

This is well outdated. Where can we get the current linux static binary? I'm looking around, looks like I need to extract it.

@eltiare
Copy link

eltiare commented May 8, 2015

@jancel You can get the static binaries here: http://wkhtmltopdf.org/downloads.html

@iyanski
Copy link

iyanski commented Jun 15, 2015

@wintondeshong
Copy link

Running wkhtmltopdf 0.12.2.1 on OS X 10.10.3 in a Ruby 2.2.2 Rails 4.2.2 project for local development via...

brew install caskroom/cask/brew-cask
brew cask install wkhtmltopdf

Then used the amd64 binary from https://github.com/h4cc/wkhtmltopdf-amd64 out of app's bin directory (as described above) on Heroku for staging and production environments.

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