Sign inSign up

vinorcola/pdf-generator

By vinorcola

•Updated about 4 years ago

An HTTP service for generating PDF documents from HTML content.

Image
0

847

vinorcola/pdf-generator repository overview

⁠PDF generator

An HTTP service for generating PDF documents from HTML content.

⁠Introduction

This service is meant to be a PDF document generator from HTML.

It runs an HTTP server (using Node.js & express) and provide a POST / API end-point that accept the HTML content. It uses a Chromium browser (using Microsoft Playwright library) to generate a PDF document.

This service is provided as a Docker image and is meant to be integrated into a Docker stack for an application that needs to generate PDF documents.

⁠Usage

The usage is pretty simple: you send an HTTP request with the HTML content in the body and receive a PDF document as response.

First, you have to integrate the Docker image into your Docker stack:

version: "3"

services:
    # Whatever services your app already have

    pdfGenerator:
        image: "vinorcola/pdf-generator:latest"

The PDF generator service will be available at http://pdfGenerator from other services that share the private network.

In order to generate a PDF document, you will need to send an HTTP request with the following JSON content:

POST / HTTP/1.1
Content-Type: application/JSON

{"content":"<html><head><style>h1{color:red}</style></head><body><h1>Hello world!</h1></body></html>"}

The service will return the PDF document as an attachment file.

⁠Example with Symfony

An example of simple controller using Symfony.

/**
 * @Route("/download", methods={"GET"}, name="download")
 */
public function download(HttpClientInterface $client): Response
{
    $pdfResponse = $client->request(
        'POST',
        'http://pdfGenerator',
        [
            'json' => [
                'content' => '<html><style>h1{color:red}</style><body><h1>Hello world</h1></body></html>',
            ],
        ],
    );

    if ($pdfResponse->getStatusCode() >= 400 && $pdfResponse->getStatusCode() < 500) {
        throw new LogicException('Invalid request supply to PDF generator. Get status code ' . $pdfResponse->getStatusCode() . '.');
    }
    if ($pdfResponse->getStatusCode() >= 500) {
        throw new ServiceUnavailableHttpException('PDF Generator service is unavailable. Get status code ' . $pdfResponse->getStatusCode() . '.');
    }

    $response = new Response($pdfResponse->getContent());
    $response->headers->set('Content-Disposition', $response->headers->makeDisposition(
        ResponseHeaderBag::DISPOSITION_ATTACHMENT,
        'file.pdf',
    ));
    $response->headers->set('Content-Type', $pdfResponse->getHeaders()['content-type']);

    return $response;
}

⁠Security

No specific security are set for this service. It is meant to be integrated into a Docker stack into a private network. If you make the service available from a public port, anyone will be able to use it. Accordingly to that choice, the communication with this service is done using plain HTTP and not HTTPS.

Furthermore, note that the javascript you may provide in the HTML content will not be executed. This service is aimed to generate static documents from a trusted plain HTML content generated by an application, and for security reason, the javascript execution is disabled inside the browser used to generate the documents.

Tag summary

Content type

Image

Digest

sha256:e59dae62f…

Size

220.2 MB

Last updated

about 4 years ago

docker pull vinorcola/pdf-generator