Rails - PDFKit Footer not working

1k Views Asked by At

In my Rails application, I'm trying to generate a PDF file using PDF-Kit. I'm trying to add a footer to the PDF. I'm getting an error whenever I try to add the helper method :footer_html but, I could generate PDF without using it.

Error on the console is,

Error: Failed loading page http:/home/rmed179lt/workspace/ror/ROOT_PROJECT/views/mlap_reports/footer.html?page=1&section=&subsection=&frompage=1&subsubsection=&topage=1&webpage=foobar&time=8:50 PM&date=15/02/16 (sometimes it will work just to ignore this error with --ignore-load-errors) QPaintDevice: Cannot destroy paint device that is being painted

  kit = PDFKit.new(html, page_size: 'Letter', :footer_html => "/mlap_reports/footer.html")

I also tried,

kit = PDFKit.new(html, page_size: 'Letter', :footer_html => "/mlap_reports/footer")

My footer is located in app/views/mlap_reports/footer.html. Any Idea how to resolve this?

2

There are 2 best solutions below

0
Albert Paul On BEST ANSWER

Finally, just found a solution. It's a bug related to PDFKit gem, I've bypassed it by following code.(Called wkhtmltopdf directly and supplied footer as parameter).

  footer_html = ac.render_to_string("#{Rails.root}/app/views/mlap_reports/footer", locals: {name: "demo"})

  File.open("#{Rails.root}/public/footer.html", "w") { |file| file.write(footer_html) }

  body_path = "#{Rails.root}/public/body.html"
  footer_path = "#{Rails.root}/public/footer.html"

  status_code = system("wkhtmltopdf --page-size Letter  --footer-spacing -10  --footer-html #{footer_path} #{body_path} #{Rails.root}/public/checkthis.pdf")

You could simply call wkhtml directly if you've to add a footer.

0
Artem P On

I used temporary file:

footer_html = Tempfile.new(['footer_html', '.html'])
footer_html.write(render_to_string(partial: 'pdf/footer'))
footer_html.close
kit =
  PDFKit.new(render_to_string('pdf/show',
                              layout: false,
                              footer_html: footer_html.path)

pdf_inline = kit.to_pdf
footer_html.unlink
pdf_inline