yasir kk
yasir kk

Reputation: 171

HTML to PDF creation in AWS Lambda using Python

I am trying to create a pdf file that contains images, tables from HTML data in AWS lambda using python. I searched a lot on google and I didn't find any super cool solution. I tried some libraries in local(FPDF, pdfKit) and but it doesn't work on AWS. Is there any simple tool to create pdf and upload it to S3 bucket. Thanks in advance.

Upvotes: 0

Views: 4158

Answers (2)

vlmrmano
vlmrmano

Reputation: 11

pdfkit library works with aws lambda. pdfkit internally needs the wkhtmltopdf binaries installed, you can add them as lambda layer. You can download files from https://wkhtmltopdf.org/downloads.html.

Once you add the lambda layers you can set the config path as following:

config = pdfkit.configuration(wkhtmltopdf="/opt/bin/wkhtmltopdf")

pdfkit.from_string/from_file(input, <temp path of pdf file on lambda>, configuration=config)

You can uplod the file generated in your lambda temp location to S3 bucket using upload_file(). You can refer https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.upload_file on how to upload to s3 bucket.

Upvotes: 1

Repakula Srushith
Repakula Srushith

Reputation: 464

you can use reportlab PDF python module. It is good for all the things you have asked for. You can add images, create tables etc. There are a lot of styling options available as well. You can find more about it here: https://www.reportlab.com/docs/reportlab-userguide.pdf

I am using this is in my production and works pretty well for my use case where I have to create an invoice. You can create the invoice in the /tmp directory and then upload this to S3

Upvotes: 3

Related Questions