Wkhtmltopdf error when generating pdf with a large number of pages with header / footer

I use pdfkit (which uses wkhtmltopdf under the hood) to create PDF files in my rails application. Following the guide here , I got it mainly for basic cases of PDF files. I am now having a problem when trying to create a PDF file with a large number of pages that also have headers and footers. The error that I see from wkhtmltopdf in the console when trying to generate a PDF:

QEventDispatcherUNIXPrivate(): Unable to create thread pipe: Too many open files QEventDispatcherUNIXPrivate(): Can not continue without a thread pipe 

The minimum html example that can be used to recreate the error:

 <!-- 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 %> 

Please note that removing header / footer tags allows the PDF file to render perfectly.

Actual ruby ​​code for creating a PDF file:

 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 

A visit to this route of controllers will lead to the creation of a PDF file. And finally, I also have a controller for the header / footer, since these "partial" ones must be selected via the URL:

 class PdfController < ApplicationController def header render :layout => false end def footer render :layout => false end end 

The values ​​pdf_header_url and pdf_footer_url are literally the letters "h" and "f" for a minimal reproducible example.

Anyone familiar with wkhtmltopdf have any recommendations regarding the furthur debugging steps to solve this problem?

+6
source share
3 answers

Today I received an error message and I solved the problem with a very simple solution. The problem was that my header and footer were supposed to be full html documents with html, head and body tags. Also, see if you can get the html generated from your header and footer and check them out.

+7
source

wkhtmltopdf uses 2 file descriptors per page (one for the header and footer), which are needed to create your own variables on the page. You will need to edit nofile to set nofile (i.e. the maximum number of open files) to a sufficiently high number - some experimentation may be needed to find a value that works for you.

+3
source

Limit open files.

I ensured that my header and footer files were full HTML documents like Tom Hirschfeld , but I still got this error from too many open files.

After cleaning the networks, I found that you need to increase the limit on the number of files that one process can open. In my case, I created PDF files with hundreds of thousands of pages. It worked great without headers and footers, but when you turned on headers and footers, it seemed to get into this ceiling with an open file.

There are various ways to configure this setting based on your system, but here is what worked for me on a Ubuntu server:

Add the following to the end of /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 as wildcards (above) root soft nofile 65535 # are not applied to root. 

A good link for the ulimit team can be found here .

I hope some people are on the right track.

+3
source

Source: https://habr.com/ru/post/973949/


All Articles