简体   繁体   中英

How to use pdfkit in AWS Lambda?

I'm using AWS Lambda to generate pdf file using a ninja2 template. I am trying to use pdfkit to convert my HTML into pdf. I realize pdfkit has an internal dependency - wkhtmltopdf which needs to be used as a binary or installed via a package manager. I am not sure how to make this work on AWS Lambda?

With my current template and python code using pdfkit, I am getting the following error -

{
  "errorMessage": "No wkhtmltopdf executable found: \"b''\"\nIf this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf",
  "errorType": "OSError",
.....
.....
}

Any ideas on how can I make pdfkit work on lambda?

Any suggestions for wkhtmltopdf replacements?

Thanks

I've made a simple demo on using PDFKit using Serverless framework using layer. Checkout https://medium.com/@crespo.wang/create-pdf-using-pdfkit-on-serverless-aws-lambda-with-layer-721ca86724b2

AWS Lambda has concept of Layers which allows you to upload your custom dependencies as a zip and then it will be available as it is installed on the box. For more information see here :

https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

In your case you could upload the binaries for wkhtmltopdf as layer and while creating lambda function, you could provide the layer to use.

Download the wkhtmltopdf binaries required for aws lambda from : https://wkhtmltopdf.org/downloads.html .

Add the zip file as layer to lambda and set the pdf kit config pointing to executable path in the zip file.(/opt/bin/wkhtmltopdf)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM