首页 文章

邪恶的pdf没有渲染页眉/页脚

提问于
浏览
4

我在我的rails项目中使用wicked_pdfhtml 生成 pdf . 它渲染 template 但我无法打印 header/footer 部分 . 下面是我的代码,只打印 template 部分

render pdf: "pdf_name",
               layout: 'application',
               template: 'reports/show',
               formats: [:html],
               margin: { top: 10, bottom: 10, left: 10, right: 10 },
               disable_javascript: true,
               show_as_html: params[:debug],
               header: {
                 html: { template: 'shared/header' },
                 spacing: 10
               },
               footer: {
                 html: { template: 'shared/footer' },
                 spacing: 30,
                 line: true
               }

如果我尝试使用 WickedPdf.new.pdf_from_string render_to_string 它显示 Failed to load PDF document 错误甚至简单 WickedPdf.new.pdf_from_string('<html><body><h1>Hello There!</h1></body></html>') 不起作用 .

我曾尝试使用 render_to_string_with_wicked_pdf, render_with_wicked_pdf 但没有成功 . 我的Rails版本是 3.2.19wicked_pdf 宝石版本是 1.1.0wkhtmltopdf 版本是 wkhtmltopdf-0.13.0-alpha-7b36694_linux-trusty-amd64.deb . 我也尝试使用 wkhtmltopdf 版本 0.12.0 .

我的问题是

  • 为什么我上面的实现不起作用?

  • 为什么 pdf_from_string 不起作用?

任何形式的帮助表示赞赏 . 谢谢 .

1 回答

  • 5

    使用带有修补qt构建的 wkhtmltopdf 版本,该版本适用于页脚和页眉 .

    $ wget http://download.gna.org/wkhtmltopdf/0.12/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
    $ tar xf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
    $ sudo mv wkhtmltox /usr/local/share/
    $ sudo update-alternatives --install /usr/local/bin/wkhtmltopdf wkhtmltopdf /usr/local/share/wkhtmltox/bin/wkhtmltopdf 1005
    

    检查版本:

    $ wkhtmltopdf --version
    

    它应该说:

    wkhtmltopdf 0.12.4 (with patched qt)
    

    测试一下:

    $ wkhtmltopdf --footer-center 'footer' http://www.google.com google.pdf
    

    你也可以安装wkhtmltoimage:

    $ sudo update-alternatives --install /usr/local/bin/wkhtmltoimage wkhtmltoimage /usr/local/share/wkhtmltox/bin/wkhtmltoimage 1005
    

    你可以使用这样的脚本:wkhtmltox installer

相关问题