UNPKG

@coolgk/pdf

Version:

html to PDF module. create PDF files from html string or file.

96 lines (94 loc) 4.33 kB
/*! * @package @coolgk/utils * @version 3.1.4 * @link https://github.com/coolgk/node-utils * @license MIT * @author Daniel Gong <daniel.k.gong@gmail.com> * * Copyright (c) 2017 Daniel Gong <daniel.k.gong@gmail.com>. All rights reserved. * Licensed under the MIT License. */ "use strict"; /*! * Copyright (c) 2017 Daniel Gong <daniel.k.gong@gmail.com>. All rights reserved. * Licensed under the MIT License. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const phantom = require("phantom"); const tmp = require("@coolgk/tmp"); var Orientation; (function (Orientation) { Orientation["Portrait"] = "portrait"; Orientation["Landscape"] = "landscape"; })(Orientation = exports.Orientation || (exports.Orientation = {})); var Format; (function (Format) { Format["A3"] = "A3"; Format["A4"] = "A4"; Format["A5"] = "A5"; Format["Legal"] = "Legal"; Format["Letter"] = "Letter"; Format["Tabloid"] = "Tabloid"; })(Format = exports.Format || (exports.Format = {})); class Pdf { constructor(options = {}) { this._phantom = options.phantom || phantom; this._tmp = options.tmp || tmp; this._tmpConfig = options.tmpConfig || {}; } createFromHtmlFile(htmlFilePath, { pdfFilePath = '', delay = 0, margin = 0, orientation = Orientation.Portrait, format = Format.A4, header = '', footer = '', dpi = 96 } = {}) { return __awaiter(this, void 0, void 0, function* () { if (!pdfFilePath) { pdfFilePath = (yield this._tmp.generateFile(Object.assign({}, this._tmpConfig, { keep: true, postfix: '.pdf' }))).path; } const phantomInstance = yield this._phantom.create(); const page = yield phantomInstance.createPage(); yield page.open(htmlFilePath); yield page.property('dpi', dpi); yield page.property('paperSize', { format, orientation, margin, header: { height: typeof header === 'string' ? 0 : header.height || 0, contents: phantomInstance.callback(this._getHeaderFooter(typeof header === 'string' ? header : header.contents + '')) }, footer: { height: typeof footer === 'string' ? 0 : footer.height || 0, contents: phantomInstance.callback(this._getHeaderFooter(typeof footer === 'string' ? footer : footer.contents + '')) } }); yield page.render(pdfFilePath, { format: 'pdf' }); yield page.close(); phantomInstance.exit(); return pdfFilePath; }); } createFromHtmlString(htmlString, options = {}) { return __awaiter(this, void 0, void 0, function* () { const { path, cleanupCallback } = yield this._tmp.generateFile(Object.assign({}, this._tmpConfig, { keep: true, postfix: '.html' })); const fileStream = fs_1.createWriteStream(path); return new Promise((resolve, reject) => fileStream.write(htmlString) ? resolve() : fileStream.once('drain', resolve)).then(() => __awaiter(this, void 0, void 0, function* () { const pdfFilePath = yield this.createFromHtmlFile(path, options); cleanupCallback(); return pdfFilePath; })); }); } _getHeaderFooter(html = '') { return new Function('pageNumber', 'numberOfPages', "return '" + (html.replace(/'/g, "\\'") || '') + "'.replace(/\\${pageNumber}/g, pageNumber).replace(/\\${numberOfPages}/g, numberOfPages)"); } } exports.Pdf = Pdf; exports.default = Pdf;