mileszs / wicked_pdf

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

your ruby version error with rbenv #1075

Open Jonattb opened 1 year ago

Jonattb commented 1 year 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 1 year 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"
andyrue commented 4 months ago

I'm having this challenge too. I attempted the recommended approach of specifying the exact path to wkhtmltopdf, but I'm still getting the ruby version mismatch error. I'm using the wkhtmltopdf-binary gem version 0.12.6.7. Things worked at some point in my environment, but I'm not sure when it broke, or what caused it to break. I don't know if people have just been ignoring the error and not telling me about it, or if it just recently broke. Any help is greatly appreciated!

My specified ruby version is 3.0.4, and my system version is 2.7.0.

My WickedPdf initializer looks like this.

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

and the error

["/var/www/rubyapps/my-app/releases/20240729134726/vendor/bundle/ruby/3.0.0/gems/wkhtmltopdf-binary-0.12.6.7/bin/wkhtmltopdf", "--enable-local-file-access", "--orientation", "Landscape", "file:////tmp/wicked_pdf20240729-950697-n6d7kk.html", "/tmp/wicked_pdf_generated_file20240729-950697-e86w3a.pdf"]
Error: PDF could not be generated!
/home/deploy/.rbenv/versions/3.0.4/lib/ruby/3.0.0/bundler/definition.rb:425:in `validate_ruby!': Your Ruby version is 2.7.0, but your Gemfile specified 3.0.4 (Bundler::RubyVersionMismatch)
unixmonkey commented 4 months ago

My WickedPdf initializer looks like this.


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

@andyrue I can see here you are just pointing to bin/wkhtmltopdf, which is the Ruby binstub, which has to load the version of Ruby outside of your app.

The important part of what I said above is pointing to the exact binary needed for your platform, like bin/wkhtmltopdf_linux_amd64. This will call the linux binary itself, without needing Ruby in that system shell at all.

andyrue commented 4 months ago

Thank you, I did try pointing it to the ubuntu_20.04 file, but didn't realize I needed to uncompress it first. Uncompressing it and making it executable has me running again. However, I noticed when I deploy a new version of the app, I have to uncompress it again. Feels like I'm doing something wrong if I need to do that every time. Thank you again for your help.