I have following code executed in AWS Lambda
def handler(event, context):
s3 = boto3.resource('s3')
env = Environment(loader=FileSystemLoader(searchpath='templates'), trim_blocks=True, lstrip_blocks=True,
autoescape=select_autoescape(['html', 'xml']))
template = env.get_template('dag.template')
res = template.render(headers=headers, args=args)
with open('/tmp/w.html', 'w+') as fp:
fp.write(res)
config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
pdfkit.from_file('/tmp/w.html', '/tmp/out.pdf', configuration=config, options={"enable-local-file-access": True})
Above code runs through successfully in local docker container test but it failed when executed in AWS Lambda with error message
START RequestId: 4c1b6640-08c5-4f32-a935-bbd03d946385 Version: $LATEST
[ERROR] OSError: wkhtmltopdf exited with non-zero code 1. error:
QPainter::begin(): Returned false
Exit with code 1, due to unknown error.
Traceback (most recent call last):
File "/var/task/wbr.py", line 62, in handler
pdfkit.from_file('/tmp/wbr.html', '/opt/out.pdf', configuration=config, options={"enable-local-file-access": True})
File "/var/task/pdfkit/api.py", line 51, in from_file
return r.to_pdf(output_path)
File "/var/task/pdfkit/pdfkit.py", line 201, in to_pdf
self.handle_error(exit_code, stderr)
File "/var/task/pdfkit/pdfkit.py", line 158, in handle_error
raise IOError("wkhtmltopdf exited with non-zero code {0}. error:\n{1}".format(exit_code, error_msg))
END RequestId: 4c1b6640-08c5-4f32-a935-bbd03d946385
REPORT RequestId: 4c1b6640-08c5-4f32-a935-bbd03d946385 Duration: 580.91 ms Billed Duration: 944 ms Memory Size: 2046 MB Max Memory Used: 101 MB Init Duration: 362.31 ms
My lambda memory is 2046MB and storage is 2046MB. I am using Python3.8
Thank you
It's hard to know what a 3rd-party library is doing, but this line looks suspicious:
pdfkit.from_file('/tmp/wbr.html', '/opt/out.pdf', configuration=config, options={"enable-local-file-access": True})
It would appear that it is attempting to output a file to the /opt/
directory.
AWS Lambda blocks writes access to the disk, only allowing writing to the /tmp/
directory. If the code attempted to write to /opt/out.pdf
, it would be blocked. This might cause the error you are experiencing.
You would need to debug the library to discover what it is attempting to use /opt/out.pdf
instead of /tmp/out.pdf
that you specified in your code.
In looking at wkhtmltopdf, 0.12.6, Warning: Blocked access to file - Stack Overflow, it might be possible to specify a path that it can use.
The manual page Ubuntu Manpage: wkhtmltoimage - html to image converter says: --allow <path>
: Allow the file or files from the specified folder to be loaded (repeatable) (But that might not help.)