datainout
Version:
Import and reports data
104 lines (102 loc) • 3.69 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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/reporters/exporters/Pdf.exporter.ts
var Pdf_exporter_exports = {};
__export(Pdf_exporter_exports, {
PdfExporter: () => PdfExporter
});
module.exports = __toCommonJS(Pdf_exporter_exports);
var ejs = __toESM(require("ejs"), 1);
var fs = __toESM(require("fs"), 1);
var puppeteer = __toESM(require("puppeteer"), 1);
var PdfExporter = class {
constructor(templatePath) {
this.templatePath = templatePath;
}
write(reportPath, data, opts) {
return __async(this, null, function* () {
const { browser, content } = yield this.genTemplate(data, opts);
yield browser.close();
fs.writeFile(reportPath, content, () => console.log("Write file pdf successfully"));
});
}
toBuffer(data, opts) {
return __async(this, null, function* () {
const { browser, content } = yield this.genTemplate(data, opts);
yield browser.close();
return content;
});
}
genTemplate(data, opts) {
return __async(this, null, function* () {
var _a;
const html = ejs.render((_a = this.templatePath) != null ? _a : "", data);
const browser = yield puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox", "--disable-gpu", "--disable-dev-shm-usage", "--no-zygote"]
});
const page = yield browser.newPage();
yield page.setContent(html, { waitUntil: "networkidle0" });
return {
content: yield page.pdf({
format: "A4",
printBackground: true,
margin: { top: "20px", bottom: "20px" }
}),
browser
};
});
}
streamTo(...args) {
throw new Error("Method not implemented.");
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PdfExporter
});