5

我使用 mac osx 并尝试通过 wickedpdf 将我的 html 文件转换为 pdf 文件。我想在我的 pdf 文件的每一页都放一个字符串,但我有一个关于没有呈现的标题的问题。

我的 wickedpdf 方法是,

format.pdf do
        render :pdf => '#{@examination.name}.pdf',
               :disposition => 'inline',
               :layout => 'examination_session_pdf.html.erb',
               :no_background => true,
               :header =>{:html =>{:template=>'shared/pdf/header.pdf.erb'}}
      end

并且头文件只包含“hello”字符串或什么都不包含。然而,每次我看到这个错误,

can't convert nil into String

问题行是“:header =>{:html =>{:template=>'shared/pdf/header.pdf.erb'”。此外,我看不到任何有关在控制台上呈现标题页的日志。

我该如何解决?

4

2 回答 2

6

我今天早些时候遇到了完全相同的问题!

这就是我为让它工作而做的事情

    format.pdf 做
        渲染 :pdf => "#{@inv.invno}.pdf",
               :template => "inv/show.pdf",
               :布局=>'pdf',
               :header => { :content => render_to_string({:template => 'inv/header.pdf.erb'})},
               :footer => { :content => render_to_string({:template => 'inv/footer.pdf.erb'})},
               :margin => { :top => 38, :bottom => 35}
        结尾

你会看到我实际上使用了 render_to_string,然后通过 :content 将结果粘贴到页眉或页脚中。这对我来说非常有效。

您可以忽略 :margin 部分,因为我只是使用它来很好地分隔内容,因为页眉和页脚都包含图形。

希望这可以帮助!

于 2013-01-14T21:00:12.573 回答
3

我的解决方案已在此问题上注明。

确保你有

<!DOCTYPE html>

在头模板文件的顶部。

于 2017-02-28T15:02:26.663 回答