Rails 3 email a wicked pdf issue

147 views Asked by At

I have gem 'wicked_pdf' and gem 'combine_pdf' in a Rails 3.2 app. It works great if I'm displaying the pdf in browser.

I would like to email the pdf. My current code sends the email, but the pdf is just garbage:

enter image description here

This is the controller code:

  def pdfemail
    @costprojects = Costproject.find(params[:costproject_ids])
    respond_to do |format|
      format.html
      format.pdf do
        pdf = CombinePDF.new
        @costprojects.each do |costproject|
          @costproject = costproject
          pdf2 = render_to_string pdf: "Costproject.pdf", template: "costprojects/viewproject", encoding: "UTF-8"
          pdf << CombinePDF.parse(pdf2)
          costproject.attachments.each do |attachment|
            pdf << CombinePDF.parse( Net::HTTP.get( URI.parse( attachment.attach.url ) ) )
          end
        end
        pdf = pdf.to_pdf
        SendReport.send_report(pdf).deliver
        redirect_to :back
        flash[:notice] = 'Email containing pdf has been sent to you!'
      end
    end
  end

And this is my mailer:

  def send_report(pdf)
    tomail = "[email protected]"
    frommail = "[email protected]"
    attachments['Report.pdf'] = pdf
    mail(
        :to => tomail,
        :from => frommail,
        :subject => "Report pdf")
  end

Again - the formatting of the pdf works fine when I display in Browser.

Thanks for the help!

1

There are 1 answers

1
WhyEnBe On BEST ANSWER

See this question.

Basically you need to change it to something like:

attachments['Report.pdf'] = WickedPdf.new.pdf_from_string( render_to_string(:pdf => "report",:template => "costprojects/viewproject") )