UNPKG

html-to-pdf-converter

Version:

HTML to PDF converter with support for HEADERS, FOOTERS and page numbers

27 lines 1.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ramda_1 = require("ramda"); const path = require("path"); const fs = require("fs"); const mkPageBreakDiv = (content) => `<div style="page-break-after: always">${content}</div>`; exports.propagatePageNumbers = (html, startPageNum, endPageNumber, totalPagesCount) => { const [head, rest] = html.split(/<body>/i); if (!rest) { throw new Error('Malformed html!'); } const [body, foot] = rest.split(/<\/body>/i); if (!foot) { throw new Error('Malformed html!'); } const content = ramda_1.range(startPageNum, endPageNumber) .map(pageNum => body.replace(/{page}/g, pageNum.toString()).replace(/{pages}/g, totalPagesCount.toString())) .map(mkPageBreakDiv) .join(''); return `${head}<body>${content}</body>${foot}`; }; exports.saveBufferToFile = (buffer, filePath) => { const ws = fs.createWriteStream(path.join(__dirname, '..', 'output', filePath)); ws.write(buffer); ws.end(); }; //# sourceMappingURL=helpers.js.map