1

i am using rails 4.2 and am generating pdfs in actionmailer with following code:

attachments["abc.pdf"] = WickedPdf.new.pdf_from_string(
  render_to_string(template: "pdf_templates/abc.html", header: {
    content: render_to_string(layout: "header.html")
   }, margin: {
   top: 50, left: 50
   })
  )
  mail to: @user.email, subject: "bla blubb"

Its working fine and rendering the abc.html.erb. but it ignores margin-tags and the header file... if i put an error into header.html.erb rails is shouting - so it must be found and processed.

i also tried this but same issue:

pdf = render template: "pdf_templates/abc.html", footer: {spacing: 20, left: "ABDCDSAFASDF"}
attachments["abc.pdf"] = WickedPdf.new.pdf_from_string(pdf)

i read about some problems with ActionMailer but cant solve them, cause i am using rails 4:

wicked_pdf not loading header or footer in ActionMailer

Rails3 - wicked_pdf gem, footer issue when calling from action mailer model

Community
  • 1
  • 1
thisisole
  • 115
  • 11
  • Did you try including `PdfHelper` and using `render_to_string_with_wicked_pdf` instead of `render_to_string` like in the first SO post you linked to? – Unixmonkey May 13 '15 at 14:04

1 Answers1

1

Funny, thought that i tried the include which unixMonkey recommend. Now I tried again and it works with following code... Thank you for pushing me into the right direction.

class WelcomeMailer < ApplicationMailer
      include PdfHelper
      helper(MailerHelper)

      def new_customer(user)
         @user = user

         test = render_to_string_with_wicked_pdf(
          pdf: "test.pdf",
          template: "pdf_templates/abc.html",
          header: {html: {template: "pdf_templates/header.html"}}
         )

         attachments["abc.pdf"] = test
         mail to: @user.email, subject: "blaa blubb"
      end
    end
thisisole
  • 115
  • 11