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 @   鞋带松了  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示