Rails 4 wicked_pdf saving pdf on server in model
Asked Answered
R

2

6

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.

Responsory answered 30/7, 2014 at 8:37 Comment(0)
V
21

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
Vellum answered 30/7, 2014 at 14:2 Comment(3)
In my rails (4.2) render_to_string was extracted into ActionController::Rendering. So i had to view.extend(ActionController::Rendering) in order to get that method.Throwback
I had to use AbstractController::Rendering rather than ActionController.Cockboat
For rails 4.2, I had to use render instead of render_to_string for this to pick up my templates. I did not need to extend from *::Rendering either.Bodine
P
1

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
Pridgen answered 23/12, 2015 at 23:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.