What is Google Firebase? Everything you Need to Know in 2023
Reading Time: 15 mins What is Google Firebase? Everything you need to know in 2023 By James Virgo App developers have mixed feelings when it comes to any technology.
7th November 2024
If you’re a developer and have a need to generate PDF documents from HTML, one tool you might consider is wkhtmltopdf. This open-source library allows you to convert HTML files to PDFs with a high degree of control over the resulting document. In this blog post, we’ll look at how to use wkhtmltopdf in a JavaScript application running on AWS Lambda.
One of the challenges of using wkhtmltopdf on AWS Lambda is that it is not available as a native package in the AWS Lambda environment. However, we can work around this by creating a custom runtime that includes the library.
Here’s how to do it:
Create a new folder on your local machine and navigate to it in the terminal.
Download the wkhtmltopdf static binary for your operating system from the wkhtmltopdf downloads page.
Unzip the downloaded file and copy the binary to your project folder.
Create a file named Dockerfile in your project folder with the following contents:
FROM amazonlinux:2
COPY wkhtmltopdf /usr/local/bin/wkhtmltopdf CMD ["wkhtmltopdf"]
This Dockerfile creates a custom runtime image based on Amazon Linux 2, copies the wkhtmltopdf binary to the /usr/local/bin directory, and sets it as the default command.
Build the Docker image by running the following command:
docker build -t wkhtmltopdf .
Push the image to Amazon Elastic Container Registry (ECR). You can do this by following the instructions in the AWS documentation.
Create a new AWS Lambda function and select the custom runtime you just created in the runtime dropdown.
Now that you have wkhtmltopdf set up on your Lambda function, you can use it to convert HTML to PDF. Here’s an example of how to do it using the child_process module:
const childProcess = require('child_process');
exports.handler = async (event) => {
const html = '<h1>Hello, World!</h1>';
const pdf = childProcess.spawnSync('wkhtmltopdf', ['-', '-'], {
input: html,
encoding: 'binary'
});
const response = {
statusCode: 200,
headers: {
'Content-Type': 'application/pdf'
},
isBase64Encoded: true,
body: pdf.stdout.toString('base64')
};
return response;
};
In this example, we use the spawnSync function from the child_process module to run the wkhtmltopdf binary with the – and – arguments.
You can find the open source project of our pdf generator here: https://github.com/dittofi/dittofi-html-to-pdf.
What is Google Firebase? Everything you Need to Know in 2023
Reading Time: 15 mins What is Google Firebase? Everything you need to know in 2023 By James Virgo App developers have mixed feelings when it comes to any technology.
Why you should use Google Go (2023)
Reading Time: 5 mins Why you should use Google Go (2023) By James Virgo Google Go has become the go to app development language for startups, SMEs & enterprises
Relationships in SQL – complete guide with examples
Reading Time: 10 mins Relationships in SQL – complete guide with examples By James Virgo In this article we take a look at the different types of database relation
Enter your email below and gain the tools, knowledge, and support needed to build, launch, and scale your own car rental marketplace.
⸺ Receive the latest news
Get notified about new articles and events