Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用cron作业rails 3在rake任务中生成pdf_Ruby On Rails_Rake Task_Wicked Pdf - Fatal编程技术网

Ruby on rails 如何使用cron作业rails 3在rake任务中生成pdf

Ruby on rails 如何使用cron作业rails 3在rake任务中生成pdf,ruby-on-rails,rake-task,wicked-pdf,Ruby On Rails,Rake Task,Wicked Pdf,嗨,我正在使用rails 3和wicked pdf gem在我的控制器中生成pdf。但是现在我想 在rake任务中生成pdf,该任务将通过cron作业运行。创建此pdf后,我将发送 它在电子邮件中 this is my normal controller method to generate pdf def generate_invoice_pdf begin @trunk_groups_orig = TrunkGroupSubscriber.all

嗨,我正在使用rails 3和wicked pdf gem在我的控制器中生成pdf。但是现在我想

在rake任务中生成pdf,该任务将通过cron作业运行。创建此pdf后,我将发送

它在电子邮件中

   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
但是如何在rake任务中生成pdf呢


非常感谢您的帮助

如果您正在使用
pdfkit
wicked\u pdf
,那么您可以尝试一下,看看它是否有效

更新代码

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,
classpdfinvoice
def生成发票
@trunk\u groups\u orig=TrunkGroupSubscriber.all
content=File.read(“#{Rails.root}/app/views/accountings/generate_invoice_pdf.erb”)
模板=ERB.new(内容)
#这将生成html内容
html\u content=template.result(绑定)
#现在您有了html内容
pdf=WickedPdf.new.pdf\u from\u字符串(html\u内容)
#然后保存到一个文件
save_path=Rails.root.join('pdf','filename.pdf'))
打开(保存路径,'wb')do |文件|

文件在我的情况下,我从本文中读到的内容我不理解content=file.read“#{Rails.root}/app/views/accountings/generate#u invoice_pdf.html.erb”在您的模型中,使它成为erb并读取它将立即读取erb,如何将变量传递到模板时出现问题?我有这个错误,任何地方都没有保存pdf没有这样的文件或目录-/home/kashif/rails\u app/Peeredge Customer Portal/pdfs/filename.pdfy您必须自定义并检查它邪恶的pdf文档如何从字符串渲染,rails.root中是否有pdf目录