The aws-lambda-pdf project is designed to generate PDFs from HTML content using AWS Lambda and Chromium. The generated PDF files are stored in an S3 bucket, and the application returns a signed URL for accessing the PDF file.
The project consists of the following main components:
- API Gateway: Provides an HTTP endpoint to trigger the Lambda function.
- Lambda Function: Generates PDF from HTML and stores it in an S3 bucket.
- S3 Bucket: Stores the generated PDF files with a lifecycle policy to delete files after 24 hours.
- IAM Role: Provides necessary permissions for the Lambda function to interact with S3.
- AWS CLI
- AWS SAM CLI
- Node.js
- npm
git clone https://github.com/antoniowd/aws-lambda-pdf.git
cd aws-lambda-pdf
Navigate to the functions/html directory and install the required Node.js dependencies.
cd functions/html
npm install
Make sure you have the AWS SAM CLI installed and configured with your AWS credentials.
sam build
sam deploy --guided
Follow the prompts during the guided deployment to set parameters and review the changes before deploying.
aws apigateway get-api-keys --name-query MyApiKey --include-values --region YOUR_REGION
The deployment will generate an API Key. You need to get it from the AWS Console or use the above command:
After deployment, you will get the API endpoint URL. Use the endpoint to generate a PDF from HTML.
Example using curl
curl -X POST -H "x-api-key: YOUR_API_KEY" \
-d '{"html":"<h1>Hello World!</h1>", "fileName": "myDoc.pdf", "inlinePdf: true }' \
"https://your-api-id.execute-api.your-region.amazonaws.com/prod/pdf/html"
Replace YOUR_API_KEY with your API key and add in the body of the request the HTML content you want to convert to PDF.
- html (required): A string containing the HTML content to be converted into the PDF. For example,
<h1>Hello World!</h1>
will create a PDF with a heading that says "Hello World!". - fileName (optional, default: sample-document.pdf): The desired name of the generated PDF file. In the example, the PDF will be saved as myDoc.pdf.
- inlinePdf (optional, defaul: false): A boolean value indicating how the PDF should be displayed. If set to true, the PDF will be displayed inline (embedded within the page). If set to false or omitted, the PDF will prompt for download or open in a new tab depending on the browser's default behavior.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request for any changes.