python - 是否可以在 Python 中的 Azure Function Linux 消耗计划中保存临时文件?
问题描述
首先对不起我的英语。我有一个使用 Python 的 Azure Function Linux 消耗计划,我需要生成一个 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
我认为报告该错误是因为出于任何原因写权限不可用。你能帮我解决这个问题吗?
提前致谢。
解决方案
该tempfile.gettempdir()
方法返回一个临时文件夹,在 Linux 上是/tmp
. 您的应用程序可以使用此目录来存储函数在执行期间生成和使用的临时文件。
所以/tmp/report.pdf
用作文件目录来保存临时文件。
with open('/tmp/report.pdf', 'rb') as f:
data = f.read()
更多细节,你可以参考这篇文章。
推荐阅读
- typescript - 如何在 TypeScript 中获取方法名称?
- kotlin - CoroutineExceptionHandler “吞下”异常,但不调用 handleException() 函数
- c# - 负载大于 1500 字节的远程请求被阻止
- mysql - 使用带有“IN 子句”和“=”的查询
- c# - 如果向相反方向移动太快,玩家无法集中在车道上的问题
- java - putValue(MNEMONIC_KEY, KeyEvent.VK_S) 是做什么的?
- powershell - Powershell 获取记录 Txt DNS (Windows 7)
- r - 样条模型的 glm.nb 的正常执行和 glm.nb 的 geom_smooth 执行之间的区别
- java - 大 O 不明白代码是如何 O(log n)
- sql - Snowflake DB 无法识别 CTE 递归查询中使用的“OPTION (MAXRECURSION 1000)”