Skip to content

Repository files navigation

PDF-Generator-Service

An API service for generating PDFs from the given html.

Docker

cheesecake87/pdf-generator-service:latest

or

cheesecake87/pdf-generator-service-gfonts:latest

⚠️Warning: The -gfonts image is VERY large (~3GB)

How it works

This service uses Flask + granian (a Rust HTTP server) and WeasyPrint or the Chromium web browser to generate PDFs from the given html.

Chromium is set up to print in A4 pages, and Weasyprint is a little more flexible and allows you to pass in the page size via css.

The service listens on port 9898 by default, but can be changed by setting the environment variable PDFGS_PORT. Although it is recommended to use dockers --publish flag, somthing like:

docker run --publish 127.0.0.1:9898:9898/tcp cheesecake87/pdf-generator-service:latest

You would then sit this behind a reverse proxy. Or use it directly from other services.

Usage

Form-Data

Weasyprint

curl -X POST -F "html=<h1>Hello, World!</h1>" http://localhost:9898/pdf

or

Chromium

curl -X POST -F "html=<h1>Hello, World!</h1>" http://localhost:9898/chromium/pdf

Both routes will return a PDF file as Content-Type application/pdf

JSON

Weasyprint

curl -X POST -H "Content-Type: application/json" -d '{"html": "<h1>Hello, World!</h1>"}' http://localhost:9898/pdf

or

Chromium

curl -X POST -H "Content-Type: application/json" -d '{"html": "<h1>Hello, World!</h1>"}' http://localhost:9898/chromium/pdf

Both routes will return:

{
  "pdf": "<base64_encoded_pdf>"
}

Tips ℹ️

When passing in html with css, it's best to use the style tag in the head:

<head>
    ...
    <style>
        .styles-here {
            ...
        }
    </style>
</head>

You can adjust the page settings, and add fonts by doing:

<style>
    @page {
        size: A4 !important;
        padding: 0 !important;
        margin: 2rem !important;
    }

    @font-face {
        font-family: 'OpenSans';
        src: url('https://<url_to_font>/OpenSans.ttf') format('truetype');
    }

    ...
</style>

With Fonts

The cheesecake87/pdf-generator-service-gfonts:latest image includes all the fonts from Google Fonts.

To access these fonts use the following css:

<style>
    @page {
        size: A4 !important;
        padding: 0 !important;
        margin: 2rem !important;
    }

    @font-face {
        font-family: 'Font Name Here';
        src: url('file:///fonts/MonsieurLaDoulaise-Regular.ttf') format('truetype');
    }

    p {
        font-family: 'Font Name Here';
    }

    ...
</style>

A list of all the available fonts can be found in the FONTS.md file.

Security

If you set the environment variable PDFGS_X_API_KEY, this will set the /pdf and /chromium/pdf routes to look for the header X-API-KEY and check if it matches the value of PDFGS_X_API_KEY.

docker run --publish 127.0.0.1:9898:9898/tcp -e PDFGS_X_API_KEY=<your_api_key> cheesecake87/pdf-generator-service:latest

Usage

curl -X POST -H "X-API-KEY: <your_api_key>" -H "Content-Type: application/json" -d '{"html": "<html_string>"}' http://localhost:9898/pdf

ENV Variables List

  • PDFGS_PORT - The port to listen on.
  • PDFGS_X_API_KEY - The API key value to use to secure the /pdf route.
  • PDFGS_IN_TESTING - If set to true, the service will enable the /test and /test-api-key routes.

Orbiting Documentation

Please see the WeasyPrint documentation for more information on how the passed html works.

https://doc.courtbouillon.org/weasyprint/stable/common_use_cases.html

About

An API service for generating PDFs from given html

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages