Rails 4 wicked_pdf saving pdf on server in model

依然范特西╮ 提交于 2019-12-21 12:58:06

问题


I'm trying to save pdf in model like this:

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

I did in in payment.rb model after I save my Payment object.

Get an error:

undefined method `render_to_string' for <Payment object>

Earlier did it in controller without problem

def show
    @user = @payment.user
    #sprawdza czy faktura nalezy do danego uzytkownika
    # [nie mozna podejrzec po wpisaniu id dowolnej faktury]
    if current_user != @user
      flash[:error] = I18n.t 'errors.invoice_forbidden'
      redirect_to '/' and return
    end
    respond_to do |format|
      format.html do
        render :layout => false
      end
      format.pdf do
        render :pdf => "invoice",:template => "payments/show"
      end
    end
  end

I have a view payments/show.pdf.erb of course.


回答1:


Rails models doesn't have the method render_to_string. It is not the responsibility of the model to render views.

If you absolutely need to do it in the model, you can do this:

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

Instead of polluting my model with all this, I might create a service object something like this:

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

Then in the model you could do this:

def save_invoice
  InvoicePdfGenerator.new(self).save
end



回答2:


This work for me:

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


来源:https://stackoverflow.com/questions/25032224/rails-4-wicked-pdf-saving-pdf-on-server-in-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!