Ruby on rails Rails 4.0 pdf在模型中的服务器上保存pdf

Ruby on rails Rails 4.0 pdf在模型中的服务器上保存pdf,ruby-on-rails,pdf,wicked-pdf,Ruby On Rails,Pdf,Wicked Pdf,我正在尝试将pdf保存在模型中,如下所示: def save_invoice pdf = WickedPdf.new.pdf_from_string( render_to_string(:pdf => "invoice",:template => 'documents/show.pdf.erb') ) save_path = Rails.root.join('pdfs','filename.pdf') File.open(save_pa

我正在尝试将pdf保存在模型中,如下所示:

def save_invoice
    pdf = WickedPdf.new.pdf_from_string(
        render_to_string(:pdf => "invoice",:template => 'documents/show.pdf.erb')
    )
    save_path = Rails.root.join('pdfs','filename.pdf')
    File.open(save_path, 'wb') do |file|
      file << pdf
    end
  end
class InvoicePdfGenerator
  def initialize(invoice)
    @invoice = invoice
    @view = ActionView::Base.new(ActionController::Base.view_paths, {})
    @view.extend(ApplicationHelper)
    @view.extend(Rails.application.routes.url_helpers)
    @save_path = Rails.root.join('pdfs','filename.pdf')
  end

  def save
    File.open(@save_path, 'wb') do |file|
      file << rendered_pdf
    end
  end

  private

  def rendered_pdf
    WickedPdf.new.pdf_from_string(
      rendered_view
    )
  end

  def rendered_view
    @view.render_to_string(
      :pdf => "invoice",
      :template => 'documents/show.pdf.erb',
      :locals => { '@invoice' => @invoice }
    )
  end
end

我当然有一个视图
payments/show.pdf.erb

Rails模型没有方法
渲染到字符串。
模型不负责渲染视图

如果您确实需要在模型中执行此操作,可以执行以下操作:

def save_invoice
  # instantiate an ActionView object
  view = ActionView::Base.new(ActionController::Base.view_paths, {})
  # include helpers and routes
  view.extend(ApplicationHelper)
  view.extend(Rails.application.routes.url_helpers)
  pdf = WickedPdf.new.pdf_from_string(
     view.render_to_string(
       :pdf => "invoice",
       :template => 'documents/show.pdf.erb',
       :locals => { '@invoice' => @invoice }
     )
  )
  save_path = Rails.root.join('pdfs','filename.pdf')
  File.open(save_path, 'wb') do |file|
    file << pdf
  end
end
这是我的工作:

class Transparency::Report
     def generate(id)
            action_controller = ActionController::Base.new
            action_controller.instance_variable_set("@minisite_publication", Minisite::Publication.find(id))


            pdf = WickedPdf.new.pdf_from_string(
              action_controller.render_to_string('minisite/publications/job.pdf.slim', layout: false)
            )

            pdf_path = "#{Rails.root}/tmp/#{self.name}-#{DateTime.now.to_i}.pdf"
            pdf = File.open(pdf_path, 'wb') do |file|
              file << pdf
            end
     end
end
类透明度::报告
def生成(id)
action\u controller=ActionController::Base.new
操作\u控制器。实例\u变量\u集(“@ministite\u publication”,ministite::publication.find(id))
pdf=WickedPdf.new.pdf\u from\u字符串(
操作控制器。渲染到字符串('ministe/publications/job.pdf.slim',布局:false)
)
pdf#u path=“#{Rails.root}/tmp/#{self.name}-#{DateTime.now.to_i}.pdf”
pdf=File.open(pdf_路径,'wb')do|文件|

MyRails(4.2)render_to_字符串中的文件被提取到ActionController::Rendering中。因此,我必须查看.extend(ActionController::Rendering)以获得该方法。我必须使用AbstractController::Rendering而不是ActionController。对于rails 4.2,我必须使用
render
而不是
render\u to\u string
来获取我的模板。我也不需要从
*::Rendering
扩展。
class InvoicePdfGenerator
  def initialize(invoice)
    @invoice = invoice
    @view = ActionView::Base.new(ActionController::Base.view_paths, {})
    @view.extend(ApplicationHelper)
    @view.extend(Rails.application.routes.url_helpers)
    @save_path = Rails.root.join('pdfs','filename.pdf')
  end

  def save
    File.open(@save_path, 'wb') do |file|
      file << rendered_pdf
    end
  end

  private

  def rendered_pdf
    WickedPdf.new.pdf_from_string(
      rendered_view
    )
  end

  def rendered_view
    @view.render_to_string(
      :pdf => "invoice",
      :template => 'documents/show.pdf.erb',
      :locals => { '@invoice' => @invoice }
    )
  end
end
def save_invoice
  InvoicePdfGenerator.new(self).save
end
class Transparency::Report
     def generate(id)
            action_controller = ActionController::Base.new
            action_controller.instance_variable_set("@minisite_publication", Minisite::Publication.find(id))


            pdf = WickedPdf.new.pdf_from_string(
              action_controller.render_to_string('minisite/publications/job.pdf.slim', layout: false)
            )

            pdf_path = "#{Rails.root}/tmp/#{self.name}-#{DateTime.now.to_i}.pdf"
            pdf = File.open(pdf_path, 'wb') do |file|
              file << pdf
            end
     end
end