1

I currently have a rails app(4.2.1) that uses wicked_pdf and carrierwave functionality. The user currently clicks a button that renders a Family Assessment view to PDF, the user then downloads the PDF to their computer and navigates to an Assessment form where they then submit the PDF and it uploads successfully to AWS.

This all works fine however what I would like to do is "simplify" this process. When the user clicks a button the PDF would then be created and passed off to carrierwave for the upload to AWS using carrierwave/fog. I have a view now with a link to a controller which calls the FamilyAssesment.upload_assessment method.

I have been experimenting with different approaches but just can't seem to understand exactly what I need to do to get this to work.

Button on view

<%= link_to "Upload", family_assessment_pdf_upload_url(id: "#{f.object.case.id}"), class: 'button radius default tiny pdf-button' %>

family_assessments_controller.rb

class FamilyAssessmentsController < ApplicationController

  def family_assessment_pdf_upload
    c = Case.find(params[:id])
    family_assessment = c.family_assessment
    assessment = c.assessments.where(title: 'Families First Assessment').first

    FamilyAssessment.upload_assessment(assessment, family_assessment)

    #If work is done and url saved to assessment.assessment do some redirect action here 

  end

end

family_assessment.rb

class FamilyAssessment < ActiveRecord::Base

  def self.upload_assessment(assessment, family_assessment)
    family_assessment = family_assessment
    assessment = assessment 

    #I think this is how I would create the PDF in preparation for hand off to carrierwave

    pdf = WickedPdf.new.pdf_from_string(
      render_to_string(:pdf => "family_assessment",:template => 'family_assessments/family_assessment.pdf.erb', :locals => {:family_assessment => family_assessment})
    )

    #Pass pdf to carrierwave and save url in assessment.assessment

  end

end

1 Answers1

3

It looks like you can save the PDF to a temp file then upload that temp file.

class FamilyAssessment < ActiveRecord::Base

  def self.upload_assessment(assessment, family_assessment)
    family_assessment = family_assessment
    assessment = assessment 

    #I think this is how I would create the PDF in preparation for hand off to carrierwave

    pdf = WickedPdf.new.pdf_from_string(
      render_to_string(:pdf => "family_assessment",:template => 'family_assessments/family_assessment.pdf.erb', :locals => {:family_assessment => family_assessment})
    )

    #Pass pdf to carrierwave and save url in assessment.assessment
    # Write it to tempfile
    tempfile = Tempfile.new("#{Rails.root}/tmp/assessment_#{self.id}.pdf")
    tempfile.binmode
    tempfile.write pdf_file
    tempfile.close

    # Attach that tempfile to the invoice
    unless pdf_file.blank?
      self.uploads.clear
      self.uploads.create(fileinfo: File.open(tempfile.path), job_id: self.job.id)
      tempfile.unlink
    end

  end

end

A alot of this answer comes from Getting PDF from WickedPDF for attachment via Carrierwave

nzajt
  • 1,937
  • 1
  • 15
  • 16
  • I get something like `No such file or directory @ rb_sysopen - /var/folders/_0/f6fqddzx6sx64kkpf0l540sr0000gn/T/{Rails.root}/tmp/invoice_78.pdf20170502-46739-59eta1`. Would you know how to solve this? – Code-MonKy May 02 '17 at 09:19
  • @Code-MonKy -- he has a couple of typos. that line should be `"#{Rails.root}/tmp/..."`. Notice the missing `#` – toobulkeh Jun 14 '17 at 16:52
  • I fixed the aforementioned typo. – nzajt Jun 19 '17 at 18:14
  • @Code-MonKy use: tempfile = Tempfile.new("test001.pdf","#{Rails.root}/tmp") for specific directory – FredyK Sep 14 '18 at 10:25