Question

Hi i am using rails 3 and wicked pdf gem to generate pdf in my controllers.But now i want to

generate pdf in rake task which will run through cron jobs.After creating this PDF i will send

it in email.

   this is my normal controller method to generate pdf


    def generate_invoice_pdf
      begin
        @trunk_groups_orig = TrunkGroupSubscriber.all
        render :pdf => "GenerateInvoice", :layout => false, :template => "/accountings/generate_invoice_pdf"
     rescue => client
      puts client.inspect
     end
    end

But how i can generate pdf in rake task

Many many thanks for any help

Was it helpful?

Solution

If you are using pdfkit or wicked_pdf then you can try this,see if it works.

Updating code

class PdfInvoice
 def generate_invoice
  @trunk_groups_orig = TrunkGroupSubscriber.all
   content = File.read('#{Rails.root}/app/views/accountings/generate_invoice_pdf.erb')
   template = ERB.new(content) 
   # THis will generate html content 
   html_content = template.result(binding) 
   # now you have html content
   pdf= WickedPdf.new.pdf_from_string(html_content)
   # then save to a file
  save_path = Rails.root.join('pdfs','filename.pdf')
  File.open(save_path, 'wb') do |file|
   file << pdf
  end
 end 
end

PdfInvoice.new.generate_pdf
#You can customize method based on your requirement,
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top