1

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 = "somebody@gmail.com"
    frommail = "somebody@gmail.com"
    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!

Reddirt
  • 5,913
  • 9
  • 48
  • 126

1 Answers1

1

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") )

Community
  • 1
  • 1
WhyEnBe
  • 295
  • 7
  • 22
  • My controller code is being used to select multiple costprojects along with any S3 attachments they have. The controller code works fine if I display the resulting pdf in the browser. – Reddirt Jun 16 '15 at 16:51