2016-02-15 54 views
1

在我的Rails應用程序中,我試圖用PDF-Kit生成一個PDF文件。 我正在嘗試添加頁腳到PDF。每當我嘗試添加幫助程序方法:footer_html時,我都會收到錯誤消息,但是我可以在不使用PDF的情況下生成PDF。在控制檯上Rails-PDFKit Footer不能正常工作

錯誤是,

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") 

我也試過,

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

我的頁腳位於app/views/mlap_reports/footer.html。任何想法如何解決這個問題?

回答

0

最後,剛剛找到了解決方案。這是一個與PDFKit gem相關的錯誤,我已經通過下面的代碼繞過了它(直接調用wkhtmltopdf並提供頁腳作爲參數)。

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") 

如果您需要添加頁腳,您可以直接調用wkhtml。

0

我使用的臨時文件:

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