mileszs / wicked_pdf

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

your ruby version error with rbenv #1075

Open Jonattb opened 9 months ago

Jonattb commented 9 months ago

I have an error with the ruby version. I use rbenv to install ruby and I have two versions, system and 3.0.3. When I do rbenv shell system and then ruby -v I get 2.5.1

the error I get is

FATAL -- : [75922001-8c3a-4d71-bb3b-50fa5a0d178a]
[75922001-8c3a-4d71-bb3b-50fa5a0d178a] RuntimeError (Failed to execute:
["/var/www/deliveryapp/code/vendor/bundle/ruby/3.0.0/bin/wkhtmltopdf", "--encoding", "UTF-8", "file:////tmp/wicked_pdf20230918-15909-zdrv5n.html", "/tmp/wicked_pdf_generated_file20230918-15909-kdgc0u.pdf"]
Error: PDF could not be generated!
 Command Error: pid 17826 exit 1
/home/deploy/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/bundler-2.4.19/lib/bundler/definition.rb:447:in `validate_ruby!': Your Ruby version is 2.5.1, but your Gemfile specified 3.0.3 (Bundler::RubyVersionMismatch)
        from /home/deploy/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/bundler-2.4.19/lib/bundler/definition.rb:422:in `validate_runtime!'
        from /home/deploy/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/bundler-2.4.19/lib/bundler.rb:156:in `setup'
        from /home/deploy/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/bundler-2.4.19/lib/bundler/setup.rb:23:in `block in <top (required)>'
        from /home/deploy/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/bundler-2.4.19/lib/bundler/ui/shell.rb:159:in `with_level'
        from /home/deploy/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/bundler-2.4.19/lib/bundler/ui/shell.rb:111:in `silence'
        from /home/deploy/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/bundler-2.4.19/lib/bundler/setup.rb:23:in `<top (required)>'
        from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
        from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'

I don't know why I get 2.5.1 if I use 3.0.3

wicked_pdf gem version (output of cat Gemfile.lock | grep wicked_pdf): 2.7.0

wkhtmltopdf version (output of wkhtmltopdf --version): 0.12.6 (with patched qt)

whtmltopdf provider gem and version if one is used:

platform/distribution and version (e.g. Windows 10 / Ubuntu 16.04 / Heroku cedar): Ubuntu 18.04.6 LTS

unixmonkey commented 9 months ago

When your app shells out to call wkhtmltopdf (or anything else), it's running as a user on that system, and that user might not have the rbenv environment loaded.

It executes the binstub under vendor/bundle/ruby/3.0.0/bin/wkhtmltopdf, but that's just a ruby file, which halts its own execution because it's running under a different version of Ruby than it was built for.

There's 2 paths we can take here:

The first (harder path) is to make it so that when your app spawns a new process, that that user's shell automatically loads the 3.0 rbenv environment, so that when it calls ruby, it's calling out the correct version.

The second (easier method) is to specify an exact path to the binary by adding it's path via configuration to your project like this: __config/initializers/wicked_pdf.rb__

WickedPdf.config = {
  exe_path: '/usr/local/bin/wkhtmltopdf',
  enable_local_file_access: true
}

You didn't post that you were using a provider gem for wkhtmltopdf, but if you were, this might look like:

exe_path: "#{ENV['GEM_HOME']}/gems/wkhtmltopdf-binary-#{Gem.loaded_specs['wkhtmltopdf-binary'].version}/bin/wkhtmltopdf_linux_amd64"