Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 wicked_pdf saving pdf on server in model

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.

like image 950
Starspire Avatar asked Jul 30 '14 08:07

Starspire


2 Answers

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
like image 106
Unixmonkey Avatar answered Nov 23 '22 19:11

Unixmonkey


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
like image 28
user2672859 Avatar answered Nov 23 '22 19:11

user2672859