是否可以在 Python 的 Azure Functions Linux 消费计划中保存临时文件?
Is possible to save a temporaly file in a Azure Function Linux Consuption Plan in Python?
首先对不起我的英语。我有一个使用 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()
方法 returns 一个临时文件夹,在 Linux 上是 /tmp
。您的应用程序可以使用此目录来存储临时文件 由您的函数在执行期间生成和使用。
所以使用/tmp/report.pdf
作为保存临时文件的文件目录
with open('/tmp/report.pdf', 'rb') as f:
data = f.read()
更多细节,你可以参考这个article。
最终正确代码:
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={})
首先对不起我的英语。我有一个使用 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()
方法 returns 一个临时文件夹,在 Linux 上是 /tmp
。您的应用程序可以使用此目录来存储临时文件 由您的函数在执行期间生成和使用。
所以使用/tmp/report.pdf
作为保存临时文件的文件目录
with open('/tmp/report.pdf', 'rb') as f:
data = f.read()
更多细节,你可以参考这个article。
最终正确代码:
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={})