Skip to content

OliverBailey/htmlToLambda

Repository files navigation

HTML To PDF using AWS Lambda Functions and Layers with Serverless

Once run locally once, you don't need to run it again. The API endpoint will stay the same. You only ever need to deploy if you're making tweaks or changes to the any of the layers, or htmlToPdf directories. You can re-initialise it with

sudo serverless deploy --aws-profile profile_name --region eu-west-2

html-pdf is used to convert html to pdf

Prerequisite

  1. Nodejs
  2. Install Serverless globally
npm i -g serverless

Getting Started

Install all dependencies

cd path/to/the/repo
npm install

To run Locally

cd htmlToPdf/
sls offline start

Make sure the following lines are commented to run locally

htmlToPdf/handler.js

pdf.create(html, {
    format: "Letter",
    orientation: "portrait",
    // phantomPath: '/opt/phantomjs_linux-x86_64'
}).toBuffer((err, buffer) => {
    if (err) {
        reject(err)
    } else {
        resolve(buffer)
    }
});
htmlToPdf/serverless.yml

htmlToPdf:
    handler: handler.htmlToPdf
    # layers:
    #   - ${cf:executables-layer-${self:provider.stage}.HtmlToPdfLayerExport}
    events:
      - http:
          path: api/htmltopdf
          method: get
          cors: true
          integration: lambda

Setting Environment

The following environment variables must be set before the function. If not set the pdf will not be generated or else the pdf will contain black dots.

process.env.PATH = `${process.env.PATH}:/opt`
process.env.FONTCONFIG_PATH = '/opt'
process.env.LD_LIBRARY_PATH = '/opt'

These are env vars set within. No need to add as an Env var to the S3 bucket.

AWS Environment Variables

This only needs to be set for the S3_BUCKET

Deployment

Step 1. First deploy the layer.

cd layers/
sls deploy

Step 2. Deploy the main service that will be converting html to pdf

cd htmlToPdf/
sls deploy

Example

  1. Plain HTML
let html = `<h1>This is a example to convert html to pdf<h1>`
let file = await exportHtmlToPdf(html)

sample.pdf

  1. With Template Engine(hbs)
import handlebars from 'handlebars'


let html = `<h1>This is a example to convert html to pdf</h1><br /><b>{{template}}</b>`

html = handlebars.compile(html)({ template: 'HBS' })

let file = await exportHtmlToPdf(html)

sample(hbs).pdf

Template Engines

To generate HTML with dynamic data there are some popular template engines like hbs, pug, Mustache, ejs, etc.

For list of template engines visit Template Engines

More Fonts

For more fonts add .ttf font files in executable folder and redeploy the layer and the redeploy the other service so the function is pointing to the latest Lambda Layer

Reference

  1. https://github.com/naeemshaikh27/phantom-lambda-fontconfig-pack

SLS-deploy script help

sudo serverless deploy --aws-profile profile_name --region eu-west-2

serverless.yml contains the deploymentBucket name

About

htmlToLambda github repo.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published