2

I want to generate pdf using rake task running through whenever cron job rails.When i running this rake task by this command on console it is generating pdf fine

       bundle exec rake auto_invoice_email

if i give this command

       which wkhtmltopdf

then i have this path "/usr/bin/wkhtmltopdf"

But when i run it through cron job then i have this error

       rake aborted!
       Failed to execute:
       "/usr/bin/wkhtmltopdf" -q        - - 
       Error: PDF could not be generated!
       Tasks: TOP => auto_invoice_email
       (See full trace by running task with --trace)

how and where i can specify path for wkhtmltopdf for cron job

Any help?

here is rake task

 require 'rubygems'
  require 'wicked_pdf'
 require 'erb'
 require 'date'
 task :auto_invoice_email => :environment do
 #$PATH = '/usr/bin/wkhtmltopdf'
  include ApplicationHelper

  users = User.find_subscribers
    unless users.blank?
     users.each do |user|
     @user = user
    content = File.read "#{Rails.root}/app/views/accountings/generate_invoice_pdf_rake.html.erb"
    template = ERB.new(content)
     html_content = template.result(binding)
     pdf= WickedPdf.new.pdf_from_string(html_content, :wkhtmltopdf => '/usr/bin/wkhtmltopdf')
     name = "#{Rails.root}/public/#{user.subscriber.downcase+" "+Time.now.to_s}.pdf"
     File.open(name, 'wb') do |file|
       file << pdf
     end
   end
end

end

Thanks

Unixmonkey
  • 18,485
  • 7
  • 55
  • 78
Kashiftufail
  • 10,815
  • 11
  • 45
  • 79
  • Not knowing anything about rails, have you installed the gems? I don't know if they are required here or even related to rakes, but see http://stackoverflow.com/q/8626594/694325 – Joel Peltonen Sep 18 '12 at 07:35
  • Can you copy the Rake task here? – Kashyap Sep 18 '12 at 10:39
  • If that is not possible, have a look here: [cron rails Railscast](http://railscasts.com/episodes/164-cron-in-ruby?view=asciicast) by Ryan Bates. It's a bit old but should do the job. – Kashyap Sep 18 '12 at 10:44
  • @Kashyap i have adit question now – Kashiftufail Sep 18 '12 at 11:00
  • And can you paste the trace as well? run with `rake auto_invoice_email --trace` – Kashyap Sep 18 '12 at 17:44
  • Did you put wicked_pdf configuration in rails initializer ? Like on https://github.com/mileszs/wicked_pdf/wiki/Configuration ? – Casual Coder Sep 20 '12 at 13:12
  • Try adding `WickedPdf.config = { :exe_path => '/usr/bin/wkhtmltopdf' }` instead of passing it inline to `WickedPdf.new`. – Arman H Jul 14 '13 at 18:30
  • 1
    I have encountered similar issue recently, and the problem wasn't the path to `wkhtmltopdf` but the contents of the document. Specifically, the PDF couldn't find images using relative URLs. Re-writing asset paths with absolute URLs worked. – Arman H Aug 10 '13 at 20:59

0 Answers0