Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Microservice based on Node.js and Puppeteer for converting a HTML markup to PDF

Notifications You must be signed in to change notification settings

RoTerentev/docker-html-to-pdf

Repository files navigation

Microservice based on Node.js and Puppeteer for converting a HTML markup to PDF

Prerequisites

  1. Installed Docker (https://docs.docker.com/)
  2. Cloned repository

Build image

On your host machine, go to the root of cloned project and run:

docker build --target prod -t <PREFER_IMAGE_NAME>:<TAG> ./

e.g.

docker build --target prod -t html2pdf:0.0.1 ./

Docker must create a new image: html2pdf:0.0.1

Run container

To run a new container, you need to run:

docker run -d -p <HOST_PORT>:<SERVICE_PORT> <PREFER_IMAGE_NAME>:<TAG>

e.g.

docker run -d -p 8765:8765 html2pdf:0.0.1

Default SERVICE_PORT value is 8765, for change this you can use docker environment variable

docker run -d --env PORT=9999 -p 3030:9999 html2pdf:0.0.1

For more details about the Docker see the docs: https://docs.docker.com/reference/

Usage

Example

Node.js

html-to-pdf-converter.js

const fs = require('fs');
const request = require('http').request;

module.exports = (destFilePath, markup, puppeteerPDFOpts) => new Promise((resolve, reject) => {
  let body = {
    markup: markup.html,
  };

  if (markup.header) body.header = markup.header;
  if (markup.footer) body.footer = markup.footer;
  if (puppeteerPDFOpts) body.pdfOpts = puppeteerPDFOpts;

  body = JSON.stringify(body);

  const reqOpts = {
    port: 8765,
    path: '/v1/html',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json; charset=utf-8',
      'Content-Length': Buffer.byteLength(body),
    },
  };

  const req = request(reqOpts, (res) => {
    const pdfStream = fs.createWriteStream(destFilePath);

    pdfStream
      .on('finish', () => {
        resolve(destFilePath);
      })
      .on('error', reject);

    res.pipe(pdfStream);
  });

  req.on('error', reject);
  req.write(body);
  req.end();
});

About

Microservice based on Node.js and Puppeteer for converting a HTML markup to PDF

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published