Reputation: 31
So maybe this is a bad question but I'm trying to create a pdf generator that dynamically puts a name from the database into the pdf via an html view.
Here is my download function for my controller.
class PdfGeneratorController < ApplicationController
def download
@actual_locale = I18n.locale
I18n.locale = :en
@user = User.find(params[:userid])
@firstname = @user.first_name
@lastname = @user.last_name
html = PdfGeneratorController.new.render_to_string(
{
template: "/pdf_generator/download",
layout: "user_courses/certificate_layout",
}
)
pdf = Grover.new(html, wait_until: 'load').to_pdf
send_data pdf, type: "application/pdf", disposition: 'attachment', filename: 'Certificate.pdf'
I18n.locale = @actual_locale
end
end
If I place a byebug just after where @user, @firstname, and @lastname are declared they all come back with the correct data. Within my view which is named download.html.erb I user @firstname and @lastname like so.
<h2 class="certificate--title"> Demo Certification</h2>
<h4>is proudly presented to</h4>
<h2 class="certificate--acquirer-name"><%= @firstname %> <%= @lastname%> </h2>
However when the pdf is generated there is no name in it. Finally I've tested with a byebug, and @firstname and @lastname come back as nil in the view. Any ideas what I might be doing wrong?
Upvotes: 0
Views: 89
Reputation: 21160
The issue is that you are creating a new controller instance, whereas you should just use the current instance.
html = PdfGeneratorController.new.render_to_string(
Should be:
html = render_to_string(
Creation of a controller instance is typically done by the Rails framework and should not be done by yourself.
Upvotes: 2