npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fibonalabs/pdf-generator

v0.0.1

Published

Converting HTML To PDF using AWS Lambda Functions and Layers with Serverless Framework

Downloads

1

Readme

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