- Installed Docker (https://docs.docker.com/)
- Cloned repository
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
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/
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();
});