UNPKG

html-pdf-chrome

Version:

HTML to PDF and image converter via Chrome/Chromium

88 lines 2.22 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.CreateResult = void 0; const fs = require("fs"); const stream_1 = require("stream"); /** * Allows exporting of PDF or image data to multiple formats. * * @export * @class CreateResult */ class CreateResult { /** * Writes the given data Buffer to the specified file location. * * @private * @static * @param {string} filename the file name to write to. * @param {Buffer} data the data to write. * @returns {Promise<void>} * * @memberof CreateResult */ static async writeFile(filename, data) { return new Promise((resolve, reject) => { fs.writeFile(filename, data, (err) => { err ? reject(err) : resolve(); }); }); } /** * Creates an instance of CreateResult. * @param {string} data base64 data * @param {Protocol.Network.Response} response the main page network response, if any. * * @memberof CreateResult */ constructor(data, response) { this.data = data; this.response = response; } /** * Get the base64 data. * * @returns {string} base64 data. * * @memberof CreateResult */ toBase64() { return this.data; } /** * Get a Buffer of the data. * * @returns {Buffer} data. * * @memberof CreateResult */ toBuffer() { return Buffer.from(this.data, 'base64'); } /** * Get a Stream (Readable) of the data. * * @returns {Readable} Stream of data. * * @memberof CreateResult */ toStream() { const stream = new stream_1.Readable(); stream.push(this.data, 'base64'); stream.push(null); return stream; } /** * Saves the result to a file. * * @param {string} filename the filename. * @returns {Promise<void>} resolves upon successful create. * * @memberof CreateResult */ async toFile(filename) { await CreateResult.writeFile(filename, this.toBuffer()); } } exports.CreateResult = CreateResult; //# sourceMappingURL=CreateResult.js.map