wicked_pdf无法在ActionMailer中加载页眉或页脚 [英] wicked_pdf not loading header or footer in ActionMailer

查看:113
本文介绍了wicked_pdf无法在ActionMailer中加载页眉或页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码生成pdf:

I'm trying to generate a pdf with the following code:

    archivo = render_to_string(
      :pdf => "formulario_de_registro.pdf",
      :template => 'pdf/profile_download.pdf.haml',
      :layout   => 'pdf/body.pdf.haml',
      :margin => { :bottom => 40, :top => 30 },
      :header => {
        :html => { :template => 'layouts/pdf/header.pdf.haml'},
        :font_size => 13},
      :footer => {
        :html => { :template => 'layouts/pdf/footer.pdf.haml'},
        :line => true}
    )
    # from here is just for debugging purposes
    save_path = Rails.root.join('tmp','prueba.pdf')
    File.open(save_path, 'wb') do |file|
      file << archivo
    end

如果我在控制器的操作中运行此代码,效果很好,但是我的mailer类中的相同代码仅呈现HTML,而不呈现PDF。然后,如果我调用 WickedPdf.new.pdf_from_string(archivo)会生成一个PDF,但是没有页眉或页脚,这是正确的,因为在生成的HTML中既不包含二者页眉或页脚。有什么我想念的吗?请帮忙。
无论如何,我使用的是:

If I run this code inside of a controller's action, works great, but the same code in my mailer class just render a HTML, not a PDF. Then if I call WickedPdf.new.pdf_from_string(archivo) generates a PDF, but without the header or footer, which is right, because in the generated HTML doesn't include both header or footer. Is there something that I'm missing? Please help. In any case, I'm using:


  • wkhtmltopdf 0.11.0 rc1

  • rails(3.2.3)

  • wicked_pdf(0.7.9)

谢谢!

推荐答案

WickedPdf在ActionMailer中没有alias_method_chain:render_to_string,就像对ActionController一样。

WickedPdf doesn't alias_method_chain :render_to_string in ActionMailer like it does for ActionController.

首先,将wicked_pdf更新到版本0.8.0或git master。这将允许您将其包含在actionmailer类中:

First, update wicked_pdf to version 0.8.0 or git master. This will allow you to include it on an actionmailer class:

然后您可以通过手动包含PdfHelper模块并像这样直接调用方法来解决此问题:

Then you get around this by manually including the PdfHelper module and just calling the method directly like so:

# Mailer
class Notifications < ActionMailer::Base
  include PdfHelper

  def send_email
    archivo = render_to_string_with_wicked_pdf(
      :pdf => "formulario_de_registro.pdf",
      :footer => { :html => { :template => 'layouts/pdf/footer.pdf.haml' } }
      # etc...
    )
    attachments['formulario_de_registro.pdf'] = archivo
    mail :to => 'person@example.com'
  end
end

这篇关于wicked_pdf无法在ActionMailer中加载页眉或页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆