使用WickedPDF在ActionMailer中生成和发送PDF

时间:2015-05-12 12:42:31

标签: ruby-on-rails pdf wicked-pdf

我正在使用rails 4.2并在动作管理器中使用以下代码生成pdf:

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"

它工作正常并呈现abc.html.erb。但它忽略了margin-tags和头文件...如果我在header.html.erb中输入一个错误就是大喊 - 所以必须找到并处理它。

我也尝试过这个但同样的问题:

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

我读到了ActionMailer的一些问题,但无法解决它们,因为我使用的是rails 4:

wicked_pdf not loading header or footer in ActionMailer

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

1 个答案:

答案 0 :(得分:1)

好笑,以为我尝试了unixMonkey推荐的包含。现在我再次尝试使用以下代码...感谢您将我推向正确的方向。

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