Wicked pdf not rendering header/footer

元气小坏坏 提交于 2019-12-07 06:28:50

问题


I am using wicked_pdf for generating pdf from html on my rails project. It is rendering template but I could not manage to print header/footer sections. Below is my code that print only template section

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
               }

If I try to use WickedPdf.new.pdf_from_string render_to_string it show Failed to load PDF document error even simple WickedPdf.new.pdf_from_string('<html><body><h1>Hello There!</h1></body></html>') does not work.

I have tried to use render_to_string_with_wicked_pdf, render_with_wicked_pdf but did not success. My Rails version is 3.2.19, wicked_pdf gem version is 1.1.0 and wkhtmltopdf version is wkhtmltopdf-0.13.0-alpha-7b36694_linux-trusty-amd64.deb. I also tried to use wkhtmltopdf version 0.12.0.

My questions are

  1. Why my above implementation does not work?
  2. Why pdf_from_string does not work?

Any kind of help is appreciated. Thanks.


回答1:


Use a version of wkhtmltopdf built with patched qt, which properly works with footer and header.

$ 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

check the version:

$ wkhtmltopdf --version

it should say:

wkhtmltopdf 0.12.4 (with patched qt)

test it:

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

you can also install the wkhtmltoimage:

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

you can use a script like this: wkhtmltox installer



来源:https://stackoverflow.com/questions/41582504/wicked-pdf-not-rendering-header-footer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!