PDFKit (wkhtmltopdf)

Q1:ruby-on-rails - 将 footer-html 参数添加到 PDFKit (wkhtmltopdf) 时出现管道错误

template = File.read "#{Rails.root}/app/views/layouts/portfolio.haml"
@ci = self
pdf_html = Haml::Engine.new(template).render self

template_footer = File.read "#{Rails.root}/app/views/layouts/portfolio_footer.haml"
@ci = self
pdf_footer_html = Haml::Engine.new(template_footer).render self

pdfkit_instance = PDFKit.new(pdf_html, :orientation => 'Landscape', 'footer-html' => pdf_footer_html)

send_data(pdf.to_pdf, :filename => "generated.pdf", :type => 'application/pdf')

不幸的是,这会导致“Broken Pipe”错误:

pdfkit (0.5.2) lib/pdfkit/pdfkit.rb:65:in `write'
pdfkit (0.5.2) lib/pdfkit/pdfkit.rb:65:in `puts'
pdfkit (0.5.2) lib/pdfkit/pdfkit.rb:65:in `block in to_pdf'
pdfkit (0.5.2) lib/pdfkit/pdfkit.rb:64:in `popen'
pdfkit (0.5.2) lib/pdfkit/pdfkit.rb:64:in `to_pdf'
app/controllers/admin/collection_items_controller.rb:9:in `test_generate_pdf'

当我生成没有页脚的 PDF 时,它是成功的。如果我使用页脚 html 作为主要 html 源,它也会成功。使用像“footer-right”这样的纯文本选项之一也可以,但显然我想使用 html 选项。此行为在本地和部署到 Heroku 时都是一致的。

最佳答案

pdfkit_instance = PDFKit.new(pdf_html, :orientation => 'Landscape', :footer_html => pdf_footer_html)

关于ruby-on-rails - 将 footer-html 参数添加到 PDFKit (wkhtmltopdf) 时出现管道错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14327961/

Q2:Rails 3 and PDFKit, how can I convert an HTML file into a landscape orientation PDF?

In case this helps, I am using PDFKit and can render the pdf in landscape using:

respond_to do |format|
  format.html
  format.pdf {
    html = render_to_string(:layout => false , :action => "my_page.html.haml")
    kit = PDFKit.new(html, :orientation => 'Landscape')
    kit.stylesheets << "#{Rails.root}/public/stylesheets/views/pdf.css"
    send_data(kit.to_pdf, :filename => "some_name.pdf", :type => 'application/pdf')
    return # to avoid double render call
  }
end

I got the orientation part from here: https://github.com/pdfkit/pdfkit/issues/12

 

posted @ 2024-03-08 16:41  鞋带松了  阅读(8)  评论(0编辑  收藏  举报