2

It's easy to figure out how to convert html into an image/pdf on an express server using libraries like html-pdf. But, I've been struggling for weeks trying to figure out how to take my locally-working code working in an AWS Lambda. The problem is, the library I'm using requires phantomjs to run, and it's very difficult to get phantomjs running in AWS lambda. You can see what I've tried here: How do you install phantomjs on AWS lambda?

This question isn't necessarily about that struggle. This question is higher level. I'm looking for some code that takes HTML and converts it to a PDF or image. This code doesn't have to use html-pdf or phantomjs. I'm looking for any working example that converts html to pdf/image and works in a lambda. It does not have to visit a webpage. I have the HTML in a string variable.

I'm looking for an answer that uses nodejs.

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356

1 Answers1

1

I've solved exactly the same problem using https://github.com/wkhtmltopdf/wkhtmltopdf library. There are several github project available that provide some additional wrappers to run wkhtmltopdf inside lambda but I don't think there is much value in using those. This is what I've done:

1) Start a docker container using Amazon linux, Centos is fine as well

2) Compile wkhtmltopdf yourself or install it using yum

3) Stop the container and copy wkhtmltopdf binary

4) Create a zip archive with the following structure

wkhtmltopdf.zip
└ bin/wkhtmltopdf

5) Create a lambda layer and upload the previously created archive

6) The wkhtmltopdf will become available in lambda $PATH

Run Example:

I'm using python but it won't be much different in nodejs since we are just executing a command

subprocess.run(['wkhtmltopdf', 'home.html', '/tmp/html.pdf'])
b.b3rn4rd
  • 8,494
  • 2
  • 45
  • 57