'How I can use python pdfkit and wkhtmltopdf in aws lambda?

I would like to convert html to pdf using pdfkit, but when i try to run in lambda i got the errors

No wkhtmltopdf executable found: ""
If this file exists please check that this process can read it or you can pass path to it manually in method call, check README. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
Traceback (most recent call last):
  File "/opt/python/lib/python3.8/site-packages/pdfkit/configuration.py", line 35, in __init__
    with open(self.wkhtmltopdf) as f:
FileNotFoundError: [Errno 2] No such file or directory: ''

My code:

import pdfkit
pdfkit.from_string(html_content, "testing.pdf")

the code is runing in my local py file, but not working in aws lambda. Any idea to solve this issue? or any other lib suggestions???

Thank You



Solution 1:[1]

You need a add wkhtmltopdf layer to your lambda function. You can get it from here https://wkhtmltopdf.org/downloads.html#stable And in your python code add the config to pdfkit like below

PATH_WKHTMLTOPDF = '/opt/bin/wkhtmltopdf'
PDFKIT_CONFIG = pdfkit.configuration(wkhtmltopdf=PATH_WKHTMLTOPDF)
pdfkit.from_string('somehtml',configuration=PDFKIT_CONFIG)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Azhar Syed