UNPKG

pdf-document-maker

Version:
102 lines 5.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PdfDocumentMaker = void 0; const fs = require("fs"); const handlebars_1 = require("handlebars"); const lodash_1 = require("lodash"); const puppeteer_1 = require("puppeteer"); const puppeteer_report_1 = require("puppeteer-report"); const upath_1 = require("upath"); const date_fns_1 = require("date-fns"); class PdfDocumentMaker { constructor() { this._currentLocale = 'en'; this.defaultOptions = { printBackground: true, format: 'A4', scale: 1, margin: { top: 0, left: 0, right: 0, bottom: 0, } }; this.helpers = { date: (date, dateFormat) => date ? (0, date_fns_1.format)(date, dateFormat) : '', amount: (amount, format) => { var _a; const splitFormat = format.split('.'); const fractionSettings = ((_a = splitFormat[1]) === null || _a === void 0 ? void 0 : _a.split('-')) || []; const numberFormatter = new Intl.NumberFormat(this._currentLocale, { minimumIntegerDigits: Number(splitFormat[0]) || 1, minimumFractionDigits: fractionSettings[0] !== undefined ? Number(fractionSettings[0]) : 2, maximumFractionDigits: fractionSettings[1] !== undefined ? Number(fractionSettings[1]) : 3, }); const parsedAmount = amount == undefined || !amount ? 0 : amount; return !parsedAmount && parsedAmount !== 0 ? '' : `${numberFormatter.format(+parsedAmount)}`; }, lowercase: (str) => str.toLowerCase(), uppercase: (str) => str.toUpperCase(), escape: (str) => str === null || str === undefined ? '-' : str, }; } setLocale(locale) { this._currentLocale = locale; } addHelpers(newHelpers) { this.helpers = (0, lodash_1.merge)(this.helpers, newHelpers); } async getHtmlData(filePath, templateData, options = {}) { let templatePath = (0, upath_1.normalizeSafe)(filePath); if (!(0, upath_1.isAbsolute)(templatePath)) { const callingFileDir = (0, upath_1.dirname)(require.main ? require.main.filename : __filename); templatePath = (0, upath_1.resolve)(callingFileDir, templatePath); } const dataBuffer = await fs.promises.readFile(templatePath); let templateString = dataBuffer.toString(); if (options.partials) { for (const key of Object.keys(options.partials)) { const includeKey = `##INCLUDE:${key}##`; if (templateString.indexOf(key) >= 0) { const partialTemplate = await this.getHtmlData(options.partials[key], templateData); templateString = templateString.replace(new RegExp(includeKey, 'g'), partialTemplate); } } } const template = handlebars_1.default.compile(templateString); const currentHelpers = (0, lodash_1.merge)(this.helpers, options.helpers || {}); return template(templateData, { helpers: currentHelpers }); } async getPdf(filePath, templateData, pdfOptions = {}, browserArgs = {}) { const browser = await puppeteer_1.default.launch({ headless: 'new', ...browserArgs }); const html = await this.getHtmlData(filePath, templateData, pdfOptions); const currentPdfOptions = { ...this.defaultOptions, ...pdfOptions, }; const page = await browser.newPage(); await page.setContent(html, { waitUntil: 'networkidle0' }); await this._handlePageFiller(page, currentPdfOptions); const currentPdf = await (0, puppeteer_report_1.pdfPage)(page, currentPdfOptions); await browser.close(); return currentPdf; } async _handlePageFiller(page, pdfOptions) { var _a, _b, _c, _d, _e, _f; const currentPageHeight = (await (await page.$('main')).boundingBox()).height; const currentHeaderHeight = ((_b = (await ((_a = (await page.$('#header'))) === null || _a === void 0 ? void 0 : _a.boundingBox()))) === null || _b === void 0 ? void 0 : _b.height) || 0; const currentFooterHeight = ((_d = (await ((_c = (await page.$('#footer'))) === null || _c === void 0 ? void 0 : _c.boundingBox()))) === null || _d === void 0 ? void 0 : _d.height) || 0; const repeatableHeightElement = pdfOptions.repeatableElementHeight || 20; const availableSpaceInPage = PdfDocumentMaker.PAGE_HEIGHT_IN_PIXELS - (((_e = pdfOptions.margin) === null || _e === void 0 ? void 0 : _e.top) || 0) - (((_f = pdfOptions.margin) === null || _f === void 0 ? void 0 : _f.bottom) || 0) - currentFooterHeight - currentHeaderHeight; const fillerHeight = currentPageHeight > availableSpaceInPage ? (currentPageHeight + (Math.floor(currentPageHeight % availableSpaceInPage) * repeatableHeightElement)) % (availableSpaceInPage) : availableSpaceInPage - (currentPageHeight); if (fillerHeight - 12 > 0) { await page.addStyleTag({ content: '.pageFiller { height: ' + (fillerHeight - 12) + 'px !important;}' }); } } } exports.PdfDocumentMaker = PdfDocumentMaker; PdfDocumentMaker.PAGE_HEIGHT_IN_PIXELS = 1142; //# sourceMappingURL=pdf-document-maker.js.map