Wicked pdf not rendering header/footer
Asked Answered
D

1

5

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.

Desiraedesire answered 11/1, 2017 at 4:4 Comment(0)
P
8

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

Pairs answered 11/1, 2017 at 8:6 Comment(2)
Thanks, it's made my day :)Desiraedesire
is this working with highcharts graph, in my case, graph is not displayed with qt patchedHiroko

© 2022 - 2024 — McMap. All rights reserved.