Git Product home page Git Product logo

htmltopdf's Introduction

HTML To PDF using AWS Lambda Functions and Layers with Serverless

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'

AWS Environment Variables

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

htmltopdf's People

Contributors

dependabot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.