Giter VIP home page Giter VIP logo

html-to-pdf-service's Introduction

Export HTML to PDF Service

This is a simple Docker container that runs a JSON API service that allows HTML to be converted to PDF or PNG/JPG images. This service accomplishes this by using a Chrome headless browser to ensure full rendering capabilities on par with Google Chrome.

Security Note: This is intended to run as a micro service - do not directly expose to the public internet

Usage

docker run  -p 2305:2305 bedrockio/export-html

Or:

git clone [email protected]:bedrockio/export-html.git
cd export-html
yarn install
yarn start

Generating a PDF

curl \
-d '{"html": "<h1>Hello World</h1>"}' \
-H "Content-Type: application/json" \
--output hello.pdf \
-XPOST "http://localhost:2305/1/pdf"

Now open hello.pdf

The default format is "Letter" (US) but it can be set to other paper formats like so:

curl \
-d '{"html": "<h1>Hello World</h1>", "export": {"format": "A4"}}' \
-H "Content-Type: application/json" \
--output hello-a4.pdf \
-XPOST "http://localhost:2305/1/pdf"

Generating a PNG

curl \
-d '{"html": "<h1>Hello World</h1>", "export": {"type": "png"}}' \
-H "Content-Type: application/json" \
--output hello.png \
-XPOST "http://localhost:2305/1/screenshot"

Now open hello.png

Advanced Options

Each API call allows Puppeteer options via body.export

Kubernetes Deployment Notes

This module runs a full browser and each request will open a virtual broeser tab. Many concurrent requests can increase memory usage signicantly.

Here's an example of a Kubernetes deployment that limits resources (this is used in production for generating invoices):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: export-html-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      name: export-html
  template:
    metadata:
      labels:
        name: export-html
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                    - key: name
                      operator: In
                      values:
                        - export-html
                topologyKey: kubernetes.io/hostname
      containers:
        - image: bedrockio/export-html
          imagePullPolicy: Always
          name: export-html
          resources:
            requests:
              memory: "1000Mi"
              cpu: "500m"
            limits:
              memory: "3000Mi"
              cpu: "2500m"
          env:
            - name: NODE_ENV
              value: "production"
            - name: ENV_NAME
              value: "production"
          ports:
            - name: http-server
              containerPort: 2305
          volumeMounts:
            - name: export-html-cache
              mountPath: /workdir/data
      volumes:
        - name: export-html-cache
          emptyDir: {}

An optional SENTRY_DSN environment variable can be configured to enable Sentry.io error tracking.

Credits

This service is based on the excellent Puppeteer module and is maintained by Kaare Larsen.

html-to-pdf-service's People

Contributors

dominiek avatar kaareal 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.