Is possible to save a temporaly file in a Azure Function Linux Consuption Plan in Python?

Viewed 3938

first of all sorry for my English. I have an Azure Function Linux Consuption Plan using Python and I need to generate an html, transform to pdf using wkhtmltopdf and send it by email.

 #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)

Code is running ok in my local development but when I deploy the function and execute the code I am getting an error saying:

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

I think that error is reported because for any reason write permission is not available. Can you help me to solve this problem?

Thanks in advance.

2 Answers

The tempfile.gettempdir() method returns a temporary folder, which on Linux is /tmp. Your application can use this directory to store temporary files generated and used by your functions during execution.

So use /tmp/report.pdf as the file directory to save temporary file.

with open('/tmp/report.pdf', 'rb') as f:
    data = f.read()

For more details, you could refer to this article.

Final correct code:

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={})
Related