Rails PDFKit command failed
Asked Answered
G

4

10

I'm trying to use PDFKit as middleware in a rails 3 app.

I can use wkhtmltopdf from the command line just fine, but my app keeps throwing me this error

command failed: "/Users/bobby/.rvm/gems/ruby-1.9.2-p0/bin/wkhtmltopdf" "--page-size" "Letter" "--margin-top" "0.75in" "--margin-right" "0.75in" "--margin-bottom" "0.75in" "--margin-left" "0.75in" "--encoding" "UTF-8" "--print-media-type" "--quiet" "-" "-"

If i run this in terminal, it waits for my input, so I type some HTML, then press Ctrl-d and it spits what appears to be some PDF... but no luck in rails.

Here's what I have:

application.rb

require File.expand_path('../boot', __FILE__)

require 'rails/all'
require 'pdfkit'
Bundler.require(:default, Rails.env) if defined?(Bundler)

module Mpr
  class Application < Rails::Application

    YEARS_ARRAY =  (2006..2012).map {|y| [y,y]}.unshift(["Year",nil])
    MONTHS_ARRAY = (1..12).map{|m| [ Date::MONTHNAMES[m], m]}.unshift(["All months",nil])
    config.middleware.use "PDFKit::Middleware", :print_media_type => true
    PDFKit.configure do |config|
      config.wkhtmltopdf = '/Users/bobby/.rvm/gems/ruby-1.9.2-p0/bin/wkhtmltopdf'
    end
  end
end

in my controller (first line)

respond_to :html, :pdf

I think I've gone through all threads on SO, Github and Google, but no luck.

Can anyone help or point me in the right direction?

thanks, P.

Galbanum answered 13/1, 2011 at 17:1 Comment(0)
S
3

Someone solved the problem and kindly posted their solution.

Staid answered 6/7, 2011 at 9:1 Comment(0)
G
7

Run and copy the path

which wkhtmltopdf

Create config/initializers/pdfkit.rb:

 PDFKit.configure do |config|
   config.wkhtmltopdf = '/path/to/wkhtmltopdf'
 end
Grammer answered 11/4, 2011 at 11:39 Comment(1)
This answer has fixed a huge headache for me twice now.Pretense
S
3

Someone solved the problem and kindly posted their solution.

Staid answered 6/7, 2011 at 9:1 Comment(0)
D
1

Why does the error start with

/Users/bobby/... 
yet your config starts with
/Users/pierrelapree/... 

Edit:

Another difference between your code and the example in the README: https://github.com/pdfkit/PDFKit is that they show config.middleware.use taking a class or module argument, not a string.

Try changing this

config.middleware.use "PDFKit::Middleware", :print_media_type => true

to this

config.middleware.use PDFKit::Middleware, :print_media_type => true
Dru answered 15/2, 2011 at 2:49 Comment(2)
My bad, I checked and they are the same. I've corrected the questionGalbanum
Still looks different in the question? Anyway, another suggestion above.Dru
O
1

The wkhtmltopdf that comes as a gem is quite old. Uninstall this gem and try to the following wkhtmltopdf binary file. Download, unzip and move it to /usr/local/bin/. It should help.

Otherwhere answered 11/4, 2011 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.