UNPKG

webpage-to-pdf

Version:

Utility to export multiple webpages into single PDF

206 lines (203 loc) 7.01 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // src/index.ts var src_exports = {}; __export(src_exports, { Status: () => Status, default: () => src_default }); module.exports = __toCommonJS(src_exports); var import_fs = __toESM(require("fs")); var import_puppeteer_cluster = require("puppeteer-cluster"); // src/pdf-creator.ts var import_pdf_lib = require("pdf-lib"); var PDFCreator = class { constructor() { this.isReady = false; this.nextPdfBytesIndex = 0; } init() { return __async(this, null, function* () { this.pdfDoc = yield import_pdf_lib.PDFDocument.create(); this.pdfBytesMap = /* @__PURE__ */ new Map(); this.isReady = true; this.nextPdfBytesIndex = 0; }); } addPdfBytesAtIndex(pdfBytes, index) { return __async(this, null, function* () { this.checkReady(); this.pdfBytesMap.set(index, pdfBytes); while (true) { let nextPdfBytes = this.pdfBytesMap.get(this.nextPdfBytesIndex); if (nextPdfBytes == void 0) { break; } yield this.addPdfBytesToDoc(nextPdfBytes); this.pdfBytesMap.delete(this.nextPdfBytesIndex); this.nextPdfBytesIndex++; } }); } addPdfBytes(pdfBytes) { return __async(this, null, function* () { this.checkReady(); this.addPdfBytesToDoc(pdfBytes); }); } getPdf() { return __async(this, null, function* () { this.checkReady(); return this.pdfDoc.save(); }); } addPdfBytesToDoc(pdfBytes) { return __async(this, null, function* () { const doc = yield import_pdf_lib.PDFDocument.load(pdfBytes); for (var i = 0; i < doc.getPageCount(); i++) { const [docPage] = yield this.pdfDoc.copyPages(doc, [i]); this.pdfDoc.addPage(docPage); } }); } checkReady() { if (!this.isReady) { throw new Error("PDF Creator is not initialized yet. Call init() before calling me."); } } }; var pdf_creator_default = PDFCreator; // src/index.ts var Status = /* @__PURE__ */ ((Status2) => { Status2[Status2["SUCCESS"] = 0] = "SUCCESS"; Status2[Status2["SESSION_ALREADY_ACTIVE"] = 1] = "SESSION_ALREADY_ACTIVE"; Status2[Status2["NOT_INITIALIZED"] = 2] = "NOT_INITIALIZED"; Status2[Status2["CONERSION_IN_PROGRESS"] = 3] = "CONERSION_IN_PROGRESS"; return Status2; })(Status || {}); var DEFAULT_OPTIONS = { parallelRequests: 5, shareCookies: false, options: {} }; var WebpageToPdf = class { constructor() { this.isSessionActive = false; this.isConversionInProgress = false; this.numPages = 0; } init() { return __async(this, arguments, function* (wtpArguments = DEFAULT_OPTIONS) { let args = __spreadValues(__spreadValues({}, DEFAULT_OPTIONS), wtpArguments); if (this.isSessionActive) { return 1 /* SESSION_ALREADY_ACTIVE */; } this.pdfCreator = new pdf_creator_default(); yield this.pdfCreator.init(); this.numPages = 0; this.isConversionInProgress = false; this.cluster = yield import_puppeteer_cluster.Cluster.launch({ concurrency: args.shareCookies ? import_puppeteer_cluster.Cluster.CONCURRENCY_PAGE : import_puppeteer_cluster.Cluster.CONCURRENCY_CONTEXT, maxConcurrency: args.parallelRequests, puppeteerOptions: args.options }); yield this.cluster.task((_0) => __async(this, [_0], function* ({ page, data: taskData }) { yield page.setExtraHTTPHeaders(taskData.headers); yield page.goto(taskData.url); const pdfBytes = yield page.pdf(); yield this.pdfCreator.addPdfBytesAtIndex(pdfBytes, taskData.index); })); this.isSessionActive = true; return 0 /* SUCCESS */; }); } convertPageToPdf(url, headers = {}) { if (!this.isSessionActive) { return 2 /* NOT_INITIALIZED */; } if (this.isConversionInProgress) { return 3 /* CONERSION_IN_PROGRESS */; } this.cluster.queue({ url, index: this.numPages++, headers }); return 0 /* SUCCESS */; } saveAllPagesToPdf(path) { return __async(this, null, function* () { yield this.cluster.idle(); const pdfBytes = yield this.pdfCreator.getPdf(); yield import_fs.default.promises.writeFile(path, pdfBytes); yield this.cluster.close(); this.isSessionActive = false; }); } }; var src_default = WebpageToPdf; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Status }); //# sourceMappingURL=index.js.map