错误原因
[Errno 86] Bad CPU type in executable
表示你的Mac上安装的 wkhtmltopdf
二进制文件与你的CPU架构不匹配。
常见情况
- Apple Silicon Mac (M1/M2/M3):安装了为Intel x86_64架构编译的wkhtmltopdf
- Intel Mac:安装了为Apple Silicon (ARM64)架构编译的wkhtmltopdf
- 错误安装:可能安装了Linux版本的二进制文件
解决方案
1. 重新安装正确架构的wkhtmltopdf
使用Homebrew安装(推荐):
# 卸载现有版本
brew uninstall wkhtmltopdf
# 重新安装
brew install wkhtmltopdf
手动下载:
- 访问 wkhtmltopdf官网
- 下载适合你Mac架构的版本:
- Apple Silicon (M1/M2/M3):选择 ARM64/AArch64 版本
- Intel Mac:选择 x86_64 版本
2. 检查你的Mac架构
# 查看CPU架构
uname -m
# 或者
arch
3. 验证安装
# 检查wkhtmltopdf位置和架构
which wkhtmltopdf
file /usr/local/bin/wkhtmltopdf
# 测试是否能正常运行
wkhtmltopdf --version
4. 如果使用Python pdfkit库
确保重新安装wkhtmltopdf后,你的Python代码应该能正常工作:
import pdfkit
# 测试
pdfkit.from_string('Hello World!', 'test.pdf')
为什么Ubuntu能运行?
Ubuntu和Mac使用不同的操作系统和架构,Ubuntu上安装的是为Linux x86_64编译的版本,而Mac需要为macOS编译的版本。