PDF Rendering Service

A microservice for rendering PDFs from HTML using the Chromium browser.

Features

  • Good for any kind of content like receipts, invoices, reports.

  • gRPC interface including health checks.

  • Batch rendering.

  • Return or upload render results to an S3 endpoint.

  • Uses Chromium to render PDF.

  • Supports various fonts out of the box and adding custom fonts.

Example

<!DOCTYPE html>
<html>
<body>

<p style="font-family:Liberation">This is a paragraph - Liberation.</p>
<p style="font-family:Liberation Sans">This is a paragraph - Liberation Sans.</p>
<p style="font-family:Noto Serif">This is a paragraph - Noto Serif.</p>
<p style="font-family:Noto Sans">This is a paragraph - Noto Sans.</p>
<p style="font-family:Arial">This is a paragraph - Arial (without font being installed).</p>
<p style="font-family:Comic Sans MS">This is a paragraph - Comic Sans MS (without font being installed).</p>
<p style="font-family:Times New Roman">This is a paragraph - Times New Roman (without font being installed).</p>

</body>
</html>

renders like this in PDF: fonts.png.

Usage

Running as Container

docker run -d -p 50051:50051 -n pdf-rendering-srv ghcr.io/restorecommerce/pdf-rendering-srv

Example calls

These examples use the grpcurl CLI tool to make gRPC calls.

From URL

Produces a PDF file called out.pdf

grpcurl -plaintext -d '{
  "individual": {
    "data": [
      {
        "data": {
          "source": {
            "url": "https://en.wikipedia.org/wiki/WebKit"
          }
        }
      }
    ]
  }
}' 127.0.0.1:50051 io.restorecommerce.pdf_rendering.PdfRenderingService.Render | jq -r '.individual.RenderingResponse[0].payload.pdf.data' | base64 -d > out.pdf

From HTML

Produces a PDF file called out.pdf

grpcurl -plaintext -d '{
  "individual": {
    "data": [
      {
        "data": {
          "source": {
            "html": "Hello World"
          }
        }
      }
    ]
  }
}' 127.0.0.1:50051 io.restorecommerce.pdf_rendering.PdfRenderingService.Render | jq -r '.individual.RenderingResponse[0].payload.pdf.data' | base64 -d > out.pdf

Combine Multiple PDFs

Produces a PDF file called combined.pdf

grpcurl -plaintext -d '{
  "combined": {
    "data": [
      {
        "source": {
          "url": "https://en.wikipedia.org/wiki/WebKit"
        }
      },
      {
        "source": {
          "html": "Hello World"
        }
      }
    ]
  }
}' 127.0.0.1:50051 io.restorecommerce.pdf_rendering.PdfRenderingService.Render | jq -r '.combined.payload.pdf.data' | base64 -d > combined.pdf

Upload directly to S3

Produces a PDF file and uploads it to the pdf bucket at the sample.pdf key.

grpcurl -plaintext -d '{
  "individual": {
    "data": [
      {
        "data": {
          "source": {
            "html": "Hello World"
          }
        },
        "output": {
          "metaData": {
            "title": "Replacement Title",
            "creator": "Replacement Creator",
            "producer": "Replacement Producer"
          },
          "uploadOptions": {
            "bucket": "pdf",
            "key": "sample.pdf"
          }
        }
      }
    ]
  }
}' 127.0.0.1:50051 io.restorecommerce.pdf_rendering.PdfRenderingService.Render | jq

Customization

Installing Additional Fonts

See the Dockerfile how fonts are installed in Alpine Linux.

Configuration

All configuration options and their defaults are available in ./cfg/config.json.

API

This microservice exposes the following gRPC endpoints:

Info

Return data about the used chromium instance.

io.restorecommerce.pdf_rendering.PdfRenderingService.Info

Render

Render provided request into a PDF.

io.restorecommerce.pdf_rendering.PdfRenderingService.Render

Field Type Label Description

individual

io.restorecommerce.pdf_rendering.IndividualRequest

optional

Individual render request

combined

io.restorecommerce.pdf_rendering.CombinedRequest

optional

Combined render request

subject

io.restorecommerce.auth.Subject

optional

Subject details

For details of the meaning of these options check the PDFOptions interface of Puppeteer.