[RUBY] Can't output PDF in production environment (EC2, Amazon Linux) with Rails

PDF output is not possible in the production environment (EC2, Amazon Linux)

I have added a PDF output function using the gems wicked_pdf and wkhtmltopdf-binary, but the following error occurs in the production environment although it works well in the development environment. Rails version is 4.2.

RuntimeError (Failed to execute:
["/var/www/~~~/shared/bundle/ruby/2.4.0/gems/wkhtmltopdf-binary-0.12.6.3/bin/wkhtmltopdf", "--encoding", "UTF-8", "--page-size", "A4", "file:////tmp/wicked_pdf20201007-11835-gppxfs.html", "/tmp/wicked_pdf_generated_file20201007-11835-j83wu8.pdf"]
Error: PDF could not be generated!
 Command Error: /var/www/~~~/shared/bundle/ruby/2.4.0/gems/wkhtmltopdf-binary-0.12.6.3/bin/wkhtmltopdf_centos_7_amd64: error while loading shared libraries: libpng15.so.15: cannot open shared object file: No such file or directory
):

It seems that there is no library called libpng15.so.15, but I searched the net for the same error and tried various things, but it did not solve.

Change Gem and solve

This is an article from 2016, but the following was helpful. https://qiita.com/s-mori/items/00aef46e6a10499f8254 https://qiita.com/yaboojp/items/526c9397070ca5d05256

wkhtmltopdf-binary doesn't seem to support Amazon Linux, It worked fine using wkhtmltopdf-binary-aml, which is compatible with Amazon Linux.

Gemfile before modification

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary-aml'

Modified Gemfile

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary-aml', git: 'https://github.com/insphire/wkhtmltopdf-binary-aml'

wicked_pdf before modification

config/initializers/wicked_pdf.rb


WickedPdf.config = {
  :exe_path => "#{Gem.loaded_specs['wkhtmltopdf-binary'].full_gem_path}/bin/wkhtmltopdf"
}

Modified wicked_pdf

config/initializers/wicked_pdf.rb


WickedPdf.config = {
  :exe_path => "#{Gem.loaded_specs['wkhtmltopdf-binary-aml'].full_gem_path}/bin/wkhtmltopdf"
}

I did bundle install so that the version of Bundler wouldn't change. $ bundle _1.16.1_ install

Support Japanese display

When deploying to the production environment, Japanese was not displayed. So install the IPA font on the production server.

cd /usr/share/fonts

$ yum install -y ipa-gothic-fonts ipa-mincho-fonts

Since the layout was broken by changing the font, I was able to complete the receipt function by adjusting the CSS etc.

Error in development environment

By changing to a Gem compatible with Amazon Linux, an error will occur in the development environment this time.

RuntimeError - PDF could not be generated!
 Command Error: /Users/~~~/vendor/bundle/ruby/2.4.0/bundler/gems/wkhtmltopdf-binary-aml-e5340ed88aa8/bin/wkhtmltopdf:15:in `exec': Bad CPU type in executable - /Users/~~~/vendor/bundle/ruby/2.4.0/bundler/gems/wkhtmltopdf-binary-aml-e5340ed88aa8/libexec/wkhtmltopdf-darwin-x86 (Errno::E086)

To do it well in both development and production environments, you can use ╩╗if Rails.env.production?Etc. to separatewicked_pdf.rb`. You can do this by switching the Gemfile for each environment as follows.

group :development do
 gem 'wkhtmltopdf-binary'
end
gem 'wkhtmltopdf-binary', group: :development

Recommended Posts

Can't output PDF in production environment (EC2, Amazon Linux) with Rails
[Rails] How to execute "rails db: create" etc. in production environment EC2
[Rails] Reset the database in the production environment
rails tutorial About account activation in production environment
Rails5.1 + puma SSL connection in local production environment
500 Internal Server Error occurs in Rails production environment
[Note] Build a Python3 environment with Docker in EC2
Japaneseize using i18n with Rails
[Docker] Rails 5.2 environment construction with docker
A story stuck with log output in Docker + Play framework environment
[Rails] How to reset the database in production environment (Capistrano version)
Rails + MySQL environment construction with Docker
Build environment with vue.js + rails + docker
Create a database in a production environment
[Grover] Generate PDF with Rails [2020 version]
Build Rails environment with Docker Compose
Output PDF and TIFF with Java 8
[Environment construction with Docker] Rails 6 & MySQL 8
[Rails 6] Customize Bootstrap in Rails + Bootstrap 5.0.0-alpha environment
[Rails] AWS EC2 instance environment construction
Settings that should be done when operating a production environment with Rails
[Rails] About the error that the image is not displayed in the production environment
Launching the production environment with docker + rails (ver5.2) and errors that occurred