mileszs / wicked_pdf

PDF generator (from HTML) plugin for Ruby on Rails
http://www.mileszs.com/wicked-pdf-plugin
MIT License
3.53k stars 644 forks source link

Bundler::GemNotFound Issue when running on production #890

Open ahaseebkhan opened 4 years ago

ahaseebkhan commented 4 years ago

Wickedpdf-Binary not working in production

On production we use the vendor directory for gems. Now when trying to generate a pdf we get the following error:

RuntimeError (Failed to execute:
["/var/www/project/app_name/vendor/bundle/ruby/2.3.0/gems/wkhtmltopdf-binary-0.12.5.4/bin/wkhtmltopdf", "--page-size", "A4", "file:////tmp/wicked_pdf20200308-32667-xdi037.html", "/tmp/wicked_pdf_generated_file20200308-32667-1yc7wxj.pdf"]
Error: PDF could not be generated!
 Command Error: /home/ubuntu/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/spec_set.rb:91:in `block in materialize': Could not find json-1.8.6 in any of the sources (Bundler::GemNotFound)
    from /home/ubuntu/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/spec_set.rb:85:in `map!'
    from /home/ubuntu/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/spec_set.rb:85:in `materialize'
    from /home/ubuntu/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/definition.rb:170:in `specs'
    from /home/ubuntu/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/definition.rb:237:in `specs_for'
    from /home/ubuntu/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/definition.rb:226:in `requested_specs'
    from /home/ubuntu/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/runtime.rb:108:in `block in definition_method'
    from /home/ubuntu/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/runtime.rb:20:in `setup'
    from /home/ubuntu/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler.rb:107:in `setup'
    from /home/ubuntu/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/setup.rb:20:in `<top (required)>'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
):
  app/controllers/user_answers_controller.rb:106:in `block (2 levels) in user_detail_pdf'
  app/controllers/user_answers_controller.rb:103:in `user_detail_pdf'

I've tried everything from changing the 'wkhtmltopdf-binary version and defining the path for the binary directly in the exe_path inside the initializer. It all gives the same error.

Now this works perfectly on local. I don't know why its looking for the JSON gem or if its something to do with the gem directories.

Also changed the default exe_path from /vendor/bundle/ruby/2.3.0/bin/wkhtmltopdf to /vendor/bundle/ruby/2.3.0/gems/wkhtmltopdf-binary-0.12.5.4/bin/wkhtmltopdf but that too doesn't work.

My issue seems similar to this one https://github.com/mileszs/wicked_pdf/issues/737 but nothing from this really helped in my case.

System Specs

Ubuntu-"16.04.3 LTS (Xenial Xerus)" Running with Rails 4 on Apache

unixmonkey commented 4 years ago

I wonder if the JSON dependency has to do with wkhtmltopdf-binary trying to un-gzip the compressed binary the first time it's run.

Try navigating to the bin directory of the gem and see if the binary you need has the .gz file extension. If it does, uncompress it with:

gunzip wkhtmltopdf-binary-0.12.5.4/bin/wkhtmltopdf_ubuntu_16.04_amd64.gz

And try again. If you still have trouble, try pointing exe_path to the actual binary file like:

WickedPdf.config = {
  Rails.root.join('vendor/bundle/ruby/2.3.0/gems/wkhtmltopdf-binary-0.12.5.4/bin/wkhtmltopdf_ubuntu_16.04_amd64')
}

instead of the ruby wrapper at bin/wkhtmltopdf. That wrapper file is Ruby, and if it's being executed under a different context than your Rails app, it may not have the dependencies it needs.

You could also just copy the wkhtmltopdf binaries you need for both development and production out of the gem and vendor those in your project.

Let me know how it goes!

ahaseebkhan commented 4 years ago

@unixmonkey thank you for pointing this out. I used sudo apt-get install xvfb libfontconfig wkhtmltopdf package and moved the binary to my project bin directory and ran it with xvfb. That too was causing the an issue with some fonts.

I followed the above steps and uncompressed the binary from the GEM and pointed my exe path to it. Now its finally working as expected. Still not sure why the ruby wrapper was causing this error though.

Thanks for helping out.