首先,对不起我的英语。我有一个使用Python的Azure函数,我需要生成一个html,使用wkhtmltopdf转换成pdf并通过电子邮件发送它。
#generate temporally pdf
config = pdfkit.configuration(wkhtmltopdf="binary/wkhtmltopdf")
pdfkit.from_string(pdf_content, 'report.pdf',configuration=config, options={})
#read pdf and transform to Bytes
with open('report.pdf', 'rb') as f:
data = f.read()
#encode bytes
encoded = base64.b64encode(data).decode()
#Send Email
EmailSendData.sendEmail(html_content,encoded,spanish_month)代码在本地开发中运行正常,但是当我部署函数并执行代码时,我会收到一个错误消息:
Result: Failure Exception: OSError: wkhtmltopdf reported an error: Loading pages (1/6) [> ] 0% [======> ] 10% [==============================> ] 50% [============================================================] 100% QPainter::begin(): Returned false Error: Unable to write to destination我认为报告错误是因为出于任何原因,写权限不可用。你能帮我解决这个问题吗?
提前谢谢。
发布于 2020-08-06 06:53:58
tempfile.gettempdir()方法返回一个临时文件夹,它在Linux上是您的应用程序可以使用来存储执行过程中由函数生成和使用的临时文件。
因此,使用/tmp/report.pdf作为文件目录保存临时文件。
with open('/tmp/report.pdf', 'rb') as f:
data = f.read()有关更多细节,您可以参考此文章。
发布于 2020-08-06 10:59:14
最终正确代码:
config = pdfkit.configuration(wkhtmltopdf="binary/wkhtmltopdf")
local_path = os.path.join(tempfile.gettempdir(), 'report.pdf')
logger.info(tempfile.gettempdir())
pdfkit.from_string(pdf_content, local_path,configuration=config, options={})https://stackoverflow.com/questions/63265669
复制相似问题