Rails 电子邮件转 pdf

Rails email to pdf

我正在尝试使用 rails 4 应用程序中的 wicked_pdf gem 将电子邮件呈现为 pdf 文件。

这样的操作可以吗?我试过了

ApplicationController.new.render_to_string :text => email.body, :format => :pdf, :template => 'wicked_pdf/template', :layout => 'wicked_pdf'

但这会产生一个空字符串。请注意,即使不应使用模板,我还是根据渲染需要创建它们。

email.body 可以用任何字符串替换得到相同的结果。

两个文件都是空的,只有 <%= yield %>.

来自 Wicked PDF 文档:

The wkhtmltopdf binary is run outside of your Rails application; therefore, your normal layouts will not work. If you plan to use any CSS, Javascript, or image files, you must modify your layout so that you provide an absolute reference to these files.

这就是我通常渲染邪恶 PDF 文件的方式。

    pdf = WickedPdf.new.pdf_from_string(
      ActionController::Base.new().render_to_string(:template => YourTemplate, :locals => {:something => @something})
    ) 

希望对您有所帮助。

我从rii的回答中推断出正确的解决方案:

如果您已有字符串或 html 文本,只需调用

即可将其呈现为 pdf
pdf = WickedPdf.new.pdf_from_string(email.body)

无需调用任何渲染器。所以你不需要任何模板或任何东西。