ruby-on-railswicked-pdf

PDF could not be generated on production using wicked_pdf


I am trying to generate pdf and render it in production server.In local host everything works fine for me. No error while I am generating PDF. But in production getting this error.

RuntimeError (Failed to execute: ["/home/app/vendor/bundle/ruby/2.7.0/gems/wkhtmltopdf-binary-0.12.6.6/bin/wkhtmltopdf", "--page-size", "A4", "file:////tmp/wicked_pdf20240308-1479705-1jl9481.html", "/tmp/wicked_pdf_generated_file20240308-1479705-112vd26.pdf"] Error: PDF could not be generated! Command Error: pid 1483292 exit 1 /home/app/.rbenv/versions/2.7.6/lib/ruby/2.7.0/bundler/spec_set.rb:86:in'block in materialize': Could not find rake-13.0.6 in any of the sources (Bundler::GemNotFound)

In Gemfile I have used

 gem 'rails', '~> 5.1.4'

 gem 'wicked_pdf', '~> 2.8'
 gem 'wkhtmltopdf-binary', '~> 0.12.6.6'

In Controller

 respond_to do |format|
        format.pdf do
          render pdf: 'certificate',
                 template: 'payments/ripicle_receipt.html.haml',
                 layout: 'payments/accounting_pdf.html.haml',
                 delete_temporary_files: true,
                 page_size: 'A4'

config/initializers/wicked_pdf.rb file. I don't have setting in this file

On server: ruby 2.7.6p219, Bundler version 2.1.4, ubuntu 22.4

I use Rails c, I tried generate pdf file. It's ok. But I request on browser, it has error

Update

I use `rails s' to starting server. It work. But i start server by nginx-passenger, it has this error


Solution

  • Current maintainer of both WickedPDF and wkhtmltopdf-binary-gem here.

    /home/app/vendor/bundle/ruby/2.7.0/gems/wkhtmltopdf-binary-0.12.6.6/bin/wkhtmltopdf is this Ruby script. It is invoking the top line !/usr/bin/env ruby to figure out which exact binary that is part of wkhtmltopdf_binary_gem to execute on your production platform.

    So, when your Rails app shells out to invoke wkhtmltopdf, the user it runs as often does not have the same Ruby environment as the application (your app runs in Bundler-managed environment, but a shell-script does not without extra setup, like installing and configuring rbenv for that linux user, and configuring it to run in non-interactive shells.

    One way to fix this is to make sure your server also has wkhtmltopdf_binary installed as a system gem, but it might be difficult to get the version installed on the system to be in sync with the one in your Gemfile.lock.

    What I would suggest you do is either code in the path to the actual binary that is needed (not the Ruby binstub), or unpack & copy the correct binary into your application's bin/ directory, and change your WickedPDF config like so:

    config/initializers/wicked_pdf.rb

    WickedPdf.configure do |config|
      if Rails.env.production?
        # Path to where the actual gem binary is extracted:
        config.exe_path = "#{ENV['GEM_HOME']}/gems/wkhtmltopdf-binary-#{Gem.loaded_specs['wkhtmltopdf-binary'].version}/bin/wkhtmltopdf_debian_10_amd64"
    
        # Or a copied binary in the /bin/ path:
        config.exe_path = Rails.root.join('bin/wkhtmltopdf_debian_10_amd64').to_s
      end
    end
    

    Or, you could install the system's package for wkhtmltopdf (like apt-get install wkhtmltopdf), and point exe_path to there instead of using the gem.