webpage-to-pdf
Version:
Utility to export multiple webpages into single PDF
174 lines (171 loc) • 5.35 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
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 __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
import fs from "fs";
import { Cluster } from "puppeteer-cluster";
// src/pdf-creator.ts
import { PDFDocument } from "pdf-lib";
var PDFCreator = class {
constructor() {
this.isReady = false;
this.nextPdfBytesIndex = 0;
}
init() {
return __async(this, null, function* () {
this.pdfDoc = yield 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 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 Cluster.launch({
concurrency: args.shareCookies ? Cluster.CONCURRENCY_PAGE : 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 fs.promises.writeFile(path, pdfBytes);
yield this.cluster.close();
this.isSessionActive = false;
});
}
};
var src_default = WebpageToPdf;
export {
Status,
src_default as default
};
//# sourceMappingURL=index.mjs.map