Search code examples
rubyruby-on-rails-5wkhtmltopdfwicked-pdf

Using wicked_pdf, save as pdf including user supplied data


I have a template to be filled out by a user. My goal is to convert the template into a pdf (containing information supplied by the user) using wicked_pdf.

Also, none of the user input is hitting the model/database. I simply need to convert the template to a pdf containing the user's data, nothing more.

So far, I can convert the template to a pdf, save it and redirect back to my index view. However, none of the fields filled in by the user are saved; all fields that were containing input or values typed by the user are blank. It's simply saving a blank template. How can I convert a pdf and user supplied data to a pdf using wicked_pdf?

# controller save method --->
def save_grade_sheet
  @result = Result.find(params[:id])
  if current_user.admin?
    filename = "#{@result.user.last_name}_grades.pdf"      
    save_path = "#{Rails.root}/public/uploads/#{@result.user.last_name}/#{filename}"
    respond_to do |format|
      pdf = render_to_string pdf: filename, 
                            template: "/results/grade_sheet.html.erb", 
                            encoding: "UTF-8", 
                            disposition: "attachment", 
                            save_to_file: save_path, 
                            save_only: true
      File.open(save_path, "wb") do |file|
        file << pdf
      end
      flash[:notice] = "#{filename} successfully saved"
      format.html { redirect_to results_path }
    end
  else
    head :unauthorized
  end
end



# sample template code
<div>
  <div>
    <h2>PERSONNEL INFORMATION</h2>
    <p>Examinee's Name: <%= @result.user.first_name %> <%= @result.user.last_name %></p>
    <p>Examiner's Name: <input class="inline" type="text"></p>
  </div>
  <div>
    <p>Exam Type: <%= @result.test.upcase %></p>
    <p>Exam Version: <input class="inline" type="text"></p>
    <p>Exam Date: <%= @result.created_at.strftime("%m-%d-%y") %></p>
  </div>
  <%= button_to "Create Grade Sheet", save_grade_sheet_result_path(@result), data: { method: :post }, class: "btn btn-primary btn-lg"%>
</div>

Solution

  • I was sending everything to the correct route, but none of the data was actually being passed. Instead, I needed to wrap everything in a form_tag and use a submit_tag.

    <%= form_tag(save_grade_sheet_result_path(@result), method: :post) %>
      <div>
        <h2>PERSONNEL INFORMATION</h2>
        <p>Examiner's Name: <%= text_field_tag :examiner_name, @examiner_name, class: "inline" %></p>
        <p>Exam Version:<%= text_field_tag :exam_version, @exam_version, class: "inline" %></p>
      </div>
      <%= submit_tag "Create Grade Sheet", class: "btn btn-primary btn-lg" %>
    <% end %>
    

    And in my controller, I needed to grab the params passed in:

    def save_grade_sheet
      @result = Result.find(params[:id])
      @examiner_name = params[:examiner_name]
      @exam_version = params[:exam_version]
      if current_user.admin?
        # existing code
      end
    end