在AWS Lambda上使用Node.js 10.x版本时,如果你遇到了“wkhtmltopdf 发生了未知的应用程序错误”的问题,可以尝试以下解决方法:
确保你的wkhtmltopdf二进制文件与Lambda运行时环境兼容,并且正确地打包在Lambda函数中。
确保你的Lambda函数代码正确调用了wkhtmltopdf。
下面是一个示例代码,展示了如何在Node.js 10.x的AWS Lambda函数中调用wkhtmltopdf:
const child_process = require('child_process');
exports.handler = async (event) => {
const htmlToPdfCommand = '/path/to/wkhtmltopdf'; // 替换为你的wkhtmltopdf路径
const inputFile = '/path/to/input.html'; // 替换为你的输入HTML文件路径
const outputFile = '/path/to/output.pdf'; // 替换为你的输出PDF文件路径
const command = `${htmlToPdfCommand} ${inputFile} ${outputFile}`;
try {
child_process.execSync(command);
console.log('wkhtmltopdf executed successfully');
return 'Success';
} catch (error) {
console.error('wkhtmltopdf execution failed:', error);
throw error;
}
};
请确保替换代码中的路径为你实际的wkhtmltopdf路径、输入HTML文件路径和输出PDF文件路径。
如果问题仍然存在,你可以通过查看Lambda函数的日志来获取更多详细信息,以帮助你进一步排查问题。