You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 22.04下使用wkhtmltopdf-binary生成PDF失败求助

问题描述

执行项目测试时,使用wicked_pdf生成PDF报错,无法完成生成。已尝试重新安装wicked_pdf和wkhtmltopdf-binary gem,问题仍未解决。

执行命令及错误信息

Failed to execute:  
["/home/o200/.asdf/installs/ruby/2.7.3/bin/wkhtmltopdf", "--margin-top", "5", "--margin-bottom", "10", "--margin-left", "10", "--margin-right", "10", "--footer-html", "file:////tmp/wicked_footer_pdf20240903-22703-1y5cw9p.html", "file:////tmp/wicked_pdf20240903-22703-yzpxrd.html", "/tmp/wicked_pdf_generated_file20240903-22703-62f1ed.pdf"]        

Error: PDF could not be generated!         

Command Error: /home/o200/.asdf/installs/ruby/2.7.3/lib/ruby/gems/2.7.0/gems/wkhtmltopdf-binary-0.12.6/bin/wkhtmltopdf:48:in <top (required)>': Invalid platform, must be running on Ubuntu 16.04/18.04/20.04 CentOS 6/7/8, Debian 9/10, or intel-based Cocoa macOS (missing binary: /home/o200/.asdf/installs/ruby/2.7.3/lib/ruby/gems/2.7.0/gems/wkhtmltopdf-binary-0.12.6/bin/wkhtmltopdf_ubuntu_22.04_amd64).

(RuntimeError)
from /home/o200/.asdf/installs/ruby/2.7.3/bin/wkhtmltopdf:23:in load'         
from /home/o200/.asdf/installs/ruby/2.7.3/bin/wkhtmltopdf:23:in <main>`

使用的代码示例

content = "<html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html> "
options = {}
WickedPdf.new.pdf_from_string(content, options)

系统信息

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.4 LTS
Release:    22.04
Codename:   jammy

解决方案

原因分析

wkhtmltopdf-binary gem 0.12.6版本内置的二进制文件仅兼容到Ubuntu 20.04,缺少对应Ubuntu 22.04的二进制包,导致平台校验失败。

具体解决方法

方法1:升级gem到支持Ubuntu 22.04的版本

wkhtmltopdf-binary 0.12.6.6及以上版本已添加对Ubuntu 22.04的支持,修改Gemfile:

gem 'wkhtmltopdf-binary', '>= 0.12.6.6'

执行安装命令:

bundle install

方法2:使用系统级wkhtmltopdf

  1. 卸载gem版的wkhtmltopdf-binary:
gem uninstall wkhtmltopdf-binary
  1. 安装系统级工具:
sudo apt update
sudo apt install wkhtmltopdf
  1. 配置WickedPdf使用系统二进制文件:
    • Rails项目:在config/initializers/wicked_pdf.rb中添加
      WickedPdf.config = {
        exe_path: '/usr/bin/wkhtmltopdf'
      }
      
    • 非Rails项目:初始化时指定路径
      options = { exe_path: '/usr/bin/wkhtmltopdf' }
      WickedPdf.new.pdf_from_string(content, options)
      

方法3:手动添加对应二进制包(不推荐)

从wkhtmltopdf官方下载Ubuntu 22.04的二进制文件,放到gem的bin目录下并重命名为wkhtmltopdf_ubuntu_22.04_amd64,但后续gem更新会覆盖该文件,仅作为临时方案。


内容的提问来源于stack exchange,提问作者Alexander Abramov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 18:07:41