生成带有页眉/页脚的页面的pdf时出现wkhtmltopdf错误

dar*_*rko 7 ruby qt ruby-on-rails wkhtmltopdf

我正在使用pdfkit(在引擎盖下使用wkhtmltopdf)在我的rails应用程序中生成PDF.按照这里的指南,我已经得到它主要用于PDF的基本情况.在尝试生成包含大量页面的PDF时,我现在遇到了一个问题,这些页面也有页眉/页脚.尝试生成PDF时,我在控制台中从wkhtmltopdf看到的错误是:

QEventDispatcherUNIXPrivate(): Unable to create thread pipe: Too many open files
QEventDispatcherUNIXPrivate(): Can not continue without a thread pipe
Run Code Online (Sandbox Code Playgroud)

可用于重新创建错误的html的最小示例:

<!-- content of pdf_header_url is the character "h" -->
<meta content="<%= pdf_header_url %>" name="pdfkit-header-html"/>
<!-- content of pdf_footer_url is the character "f" -->
<meta content="<%= pdf_footer_url %>" name="pdfkit-footer_html"/>
<% [*1..3].each do |j|%>
  <h1><%= j %></h1>
  <ul>
    <% [*1..1000].each do |i|%>
      <li><%= i %></li>
    <% end %>
  </ul>
<% end %>
Run Code Online (Sandbox Code Playgroud)

请注意,删除页眉/页脚标记允许pdf呈现正常.

生成PDF的实际ruby代码是:

def view_report
  html = render_to_string(:template => 'pdf/pdf_body.html', :layout => false)
  kit = PDFKit.new(html)
  pdf = kit.to_pdf
  send_data pdf, :type => 'application/pdf', :disposition => 'inline', :filename => 'foo.pdf'
end
Run Code Online (Sandbox Code Playgroud)

访问此控制器路由将生成PDF.最后,我还有一个页眉/页脚控制器,因为那些"部分"需要通过url获取:

class PdfController < ApplicationController

  def header
    render :layout => false
  end

  def footer
    render :layout => false
  end

end
Run Code Online (Sandbox Code Playgroud)

pdf_header_url和pdf_footer_url的值实际上只是"h"和"f",以获得最小的可再现示例.

有没有熟悉wkhtmltopdf的人有任何关于更深入调试步骤的建议来解决这个问题?

小智 8

我今天得到了同样的错误信息,我用一个非常简单的解决方案解决了这个问题.问题是我的页眉和页脚需要是带有html,head和body标签的完整html文档.另外,看看你是否可以获得生成的页眉和页脚的html输出并验证它们.


Jos*_*ter 5

打开文件限制。

正如Tom Hirschfeld所建议的那样,我确保我的页眉和页脚文件是完整的HTML文档,但是我仍然遇到太多打开文件的错误。

搜寻互联网后,我发现您需要提高允许单个进程打开的文件数量的限制。就我而言,我正在生成包含数百到数千页的PDF。在没有页眉和页脚的情况下,它可以正常工作,但是在合并页眉和页脚时,似乎达到了打开文件的上限。

有多种方法可以根据您所运行的系统调整此设置,但这是在Ubuntu服务器上对我有用的方法:

将以下内容添加到的末尾/etc/security/limits.conf

# Sets the open file maximum here.
# Generating large PDFs hits the default ceiling (1024) quickly. 
*    hard nofile 65535
*    soft nofile 65535
root hard nofile 65535 # Need these two lines because the wildcards
root soft nofile 65535 # (the * above) are not applied to the root user.
Run Code Online (Sandbox Code Playgroud)

ulimit此处可以找到该命令的良好参考。

我希望这能使某些人走上正确的道路。