Search code examples
ruby-on-railsrubywicked-pdf

PDF files not generating for more than 200 pages with footer using wicked_pdf


Gemfile

gem 'wicked_pdf' 
gem 'wkhtmltopdf-binary', '~> 0.12.5'

Controller:

def pdf_list
    @lists = Domain.select(:domain_name, :ip_address, :priority)
    respond_to do |format|
      format.pdf do
        render pdf: "pdf",
               margin: {:top => 20, :bottom => 18},
               page_size: 'A4',
               template: 'domains/list.pdf.erb',
               disposition: 'inline',
              footer: {html: {template: 'domains/footer.pdf.erb'}}
      end
    end
  end   

I have generated the html file to pdf file using wicked_pdf in rails.

Without rendering footer i can able to generate pdf for more than 300 pages.

If i am including footer with content pdf file is not generating for more than 200 pages using wicked_pdf.

Errors:

RuntimeError in DomainListsController#pdf_list Failed to execute: ["/usr/local/bin/wkhtmltopdf", "--page-size", "A4", "--margin-top", "20", "--margin-bottom", "18", "--footer-html", "file:////var/folders/v6/_516wg4d5rsdrhlb5sn82fbc0000gp/T/wicked_footer_pdf20191206-1975-o5qou2.html", "file:////var/folders/v6/_516wg4d5rsdrhlb5sn82fbc0000gp/T/wicked_pdf20191206-1975-16o6tkk.html", "/var/folders/v6/_516wg4d5rsdrhlb5sn82fbc0000gp/T/wicked_pdf_generated_file20191206-1975-1u66u16.pdf"] Error: PDF could not be generated! Command Error: Loading pages (1/6) [> ] 0% [======> ] 10% [==============================> ] 50% [==============================> ] 50%

In log files:

Error: Failed to load file:////var/folders/v6/_516wg4d5rsdrhlb5sn82fbc0000gp/T/wicked_footer_pdf20191206-1975-o5qou2.html?page=274&section=FULL ASSET REPORT&sitepage=274&title=FULL ASSETS REPORT&subsection=&frompage=1&subsubsection=&isodate=2019-12-06&topage=275&doctitle=FULL ASSETS REPORT&sitepages=275&webpage=file:////var/folders/v6/_516wg4d5rsdrhlb5sn82fbc0000gp/T/wicked_pdf20191206-1975-16o6tkk.html&time=11:07 AM&date=06/12/19, with network status code 201 and http status code 0 - Error opening //var/folders/v6/_516wg4d5rsdrhlb5sn82fbc0000gp/T/wicked_footer_pdf20191206-1975-o5qou2.html: Too many open files Error: Failed loading page file:////var/folders/v6/_516wg4d5rsdrhlb5sn82fbc0000gp/T/wicked_footer_pdf20191206-1975-o5qou2.html?page=274&section=FULL ASSET REPORT&sitepage=274&title=FULL ASSETS REPORT&subsection=&frompage=1&subsubsection=&isodate=2019-12-06&topage=275&doctitle=FULL ASSETS REPORT&sitepages=275&webpage=file:////var/folders/v6/_516wg4d5rsdrhlb5sn82fbc0000gp/T/wicked_pdf20191206-1975-16o6tkk.html&time=11:07 AM&date=06/12/19 (sometimes it will work just to ignore this error with --load-error-handling ignore)


Solution

  • The error message gives us a clue:

    Too many open files

    It is most likely that wicked_pdf is creating the complete document from several file fragments. On Linux systems there is a soft and hard limit to how many files can be open at the same time, and it appears this limit is reached. Hence the error.

    To view the current soft limit set by your system, you can use ulimit -n:

    > ulimit -n
    1024
    

    To view the available headroom for increasing the soft limit, you can check the hard limit:

    > ulimit -H -n
    1048576
    

    To increase the soft limit for the current shell, you can do:

    > ulimit -n 5000
    > ulimit -n
    5000
    

    Now run your Rails server from this shell, and try again.

    However my suggestion is to raise an issue with wicked_pdf, as this seems like an implementation problem, that could probably be addressed in the code of the gem.

    For more information on file limits see here:
    https://ultra-technology.org/linux_for_beginners/too-many-open-files-in-linux/