Search code examples
ruby-on-railsruby-on-rails-3wkhtmltopdfruby-1.9pdfkit

Rails - PDFKit Footer not working


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?


Solution

  • 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.