Custom template PDF generation problems

We are using the wkhtmltopdf tool to generate PDF invoices from HTML templates. In the common case, you needn't do any manipulations or additional settings. Sometimes, however, Redmine can't find the wkhtmltopdf module in OS and it should be assigned manually in the config/configuration.yml file.
default:
  wkhtmltopdf_exe_path: <path to wkhtmltopdf tool>

If you are using Windows for Redmine server wkhtmltopdf should be installed manually from https://wkhtmltopdf.org/downloads.html

If a custom template does not generate a valid pdf document please make sure that wkhtmltopdf is correctly configured.

In case of incorrect configuration, the resulting PDF will likely contain the following data:
Bad wkhtmltopdf's path: `/var/www` is not a directory.
Bundler will use `/tmp/bundler/home/www-data' as your home directory temporarily.
/usr/local/rvm/gems/ruby-2.4.1/bin/wkhtmltopdf

Access permissions problems

After update or install wkhtmltopdf or wkhtmltopdf-binary resulting PDF can contains the following error log:
Failed to execute:
["/usr/local/rvm/gems/ruby-2.4.1/bin/wkhtmltopdf", "-q", "--encoding", "UTF-8", "--page-size", "A4", "--margin-top", "20", "--margin-bottom", "20", "--margin-left", "20", "--margin-right", "20", "--footer-left", "INV/20200416-0028", "--footer-right", "[page]/[topage]", "file:////tmp/wicked_pdf20200416-30716-7gm5cg.html", "/tmp/wicked_pdf_generated_file20200416-30716-dxtg51.pdf"]
Error: PDF could not be generated!
 Command Error: /usr/local/rvm/gems/ruby-2.4.1/gems/wkhtmltopdf-binary-0.12.5.4/bin/wkhtmltopdf:39:in `initialize': Permission denied @ rb_sysopen - /usr/local/rvm/gems/ruby-2.4.1/gems/wkhtmltopdf-binary-0.12.5.4/bin/wkhtmltopdf_ubuntu_16.04_amd64 (Errno::EACCES)
    from /usr/local/rvm/gems/ruby-2.4.1/gems/wkhtmltopdf-binary-0.12.5.4/bin/wkhtmltopdf:39:in `open'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/wkhtmltopdf-binary-0.12.5.4/bin/wkhtmltopdf:39:in `<top (required)>'
    from /usr/local/rvm/gems/ruby-2.4.1/bin/wkhtmltopdf:22:in `load'
    from /usr/local/rvm/gems/ruby-2.4.1/bin/wkhtmltopdf:22:in `<main>'
    from /usr/local/rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `eval'
    from /usr/local/rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `<main>'

To fix that problem you need to grant write permissions to wkhtmltopdf-binary bin folder. Please run the following commands from redmine folder

# bundle show wkhtmltopdf-binary
=> /usr/local/rvm/gems/ruby-2.4.1/gems/wkhtmltopdf-binary-0.12.5.4 
# chmod -R 777 /usr/local/rvm/gems/ruby-2.4.1/gems/wkhtmltopdf-binary-0.12.5.4/bin/

Offical wkhtmltopdf_binary gem documentation

Configuration steps

  1. Find where your wkhtmltopdf is located
    $ which wkhtmltopdf
    => /usr/local/rvm/gems/ruby-2.4.1/bin/wkhtmltopdf
    
  2. Add wkhtmltopdf path to your redmine/config/configuration.yml
    default:
      wkhtmltopdf_exe_path: '/usr/local/rvm/gems/ruby-2.4.1/bin/wkhtmltopdf'
      ...
    
  3. Restart Redmine instance

Docker Container

If the problem appears to you on a Redmine Docker Container, then please add this missed library to the Docker image to fix the problem:

apt-get install libxrender1
Was this article helpful? Yes  No
454 from 493 found this helpful