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

Python Lambda中pdfkit结合Chart.js转PDF报wkhtmltopdf code -11错误

问题:Lambda中pdfkit转换含Chart.js的HTML为PDF失败,报错wkhtmltopdf exited with code -11

在AWS Lambda的Python环境中使用pdfkit工具将HTML转为PDF时,无图表的HTML可正常生成PDF,但包含Chart.js图表的HTML转换失败,报错wkhtmltopdf exited with non-zero code -11。

相关代码与配置

HTML代码

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<div style="width:250px;height:250px;">
    <canvas id="segment-chart"></canvas>
</div>
<script>
var ctx = document.getElementById("segment-chart");
var label = ["Mid", "Top", "Bottom"];
var series = [20, 40, 10];
var myChart = new Chart(ctx, {
    type: "doughnut",
    data: {
        labels: label,
        datasets: [{
            data: series,
            backgroundColor: ["#f38b4a", "#56d798", "#ff8397", "#6970d5"],
            borderColor: ["#6970d5", "#6970d5", "#6970d5", "#6970d5"],
            borderWidth: 1
        }]
    },
    options: {
        legend: {
            position: "right",
            align: "middle",
            labels: {
                usePointStyle: true,
                boxWidth: 6,
            },
        },
        animation: {
            duration: 0,
        },
        hover: {
            animationDuration: 0,
        },
        responsiveAnimationDuration: 0
    }
});
</script>

Python代码片段

...
try:
    pdfkit.from_file("/tmp/input.html", "/tmp/output.pdf", options=options)
except Exception as e:
    print("failed while converting to pdf. exception = " + str(e))
    exit(0)
...

Lambda配置

Timeout - 30 Seconds
Memory - 512 MB
Ephemeral storage - 512 MB

报错信息

failed while converting to pdf. exception = wkhtmltopdf exited with non-zero code -11. error:
Loading pages (1/6)
[>                                                           ] 0%
[======>                                                     ] 10%
[==========>                                                 ] 18%
[==================>                                         ] 30%
[===================>                                        ] 32%
[========================>                                   ] 41%
[==========================>                                 ] 44%
[===========================>                                ] 46%
END RequestId: 1xxxxxx xxx xxx
REPORT RequestId: xxxxxx xxx xxx    Duration: 1971.80 ms    
Billed Duration: 1972 ms    Memory Size: 512 MB Max   
Memory Used: 146 MB Init Duration: 435.24 ms    

解决办法

1. 修复wkhtmltopdf兼容性与依赖问题

  • 使用适配Lambda环境的wkhtmltopdf二进制包:选择针对Amazon Linux 2编译、支持无头渲染的版本,替换现有环境中的二进制文件,初始化时执行chmod +x /path/to/wkhtmltopdf赋予执行权限。
  • 补充图形依赖库:通过Lambda层预打包wkhtmltopdf所需的系统依赖(如libX11、libXext、libfontconfig等),避免环境缺失导致渲染崩溃。

2. 确保Chart.js渲染完成后再生成PDF

  • 添加JS延迟:在pdfkit的配置中增加javascript-delay参数,等待Chart.js完成渲染,示例配置:
options = {
    'javascript-delay': '3000',  # 等待3秒
    'no-stop-slow-scripts': '',
    'quiet': ''
}
  • 确认静态渲染配置:确保Chart.js的所有动画、过渡配置都已关闭(当前配置已满足,可再次检查是否有遗漏)。

3. 调整Lambda资源配置

  • 提升内存至1024MB:Lambda的CPU资源随内存比例分配,更高内存能提供更强的渲染性能,避免因资源不足导致的段错误(code -11)。
  • 检查临时存储:确保/tmp目录有足够空间存放HTML、PDF及可能的本地依赖文件。

4. 本地化Chart.js资源

将Chart.js文件下载到Lambda的/tmp目录,修改HTML中的script标签为本地路径,避免CDN加载失败或延迟导致的渲染问题:

<script src="/tmp/Chart.min.js"></script>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 04:06:26