@disruptive-learning/cfdi-to-pdf
Version:
Librería para crear un pdf basado en un XML CFDI o Retenciones
74 lines (73 loc) • 2.81 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const pdfmake_js_1 = __importDefault(require("pdfmake/build/pdfmake.js"));
const abstract_pdf_maker_builder_1 = __importDefault(require("#src/builders/abstract_pdf_maker_builder"));
const catalogs_source_1 = require("#src/catalogs/catalogs_source");
class PdfMakerBuilder extends abstract_pdf_maker_builder_1.default {
_overrideFonts;
_overrideVFS;
constructor(documentTranslator, documentOptions, catalogs, overrideFonts, overrideVFS, options) {
super();
this._documentTranslator = documentTranslator;
this._catalogs = catalogs ?? catalogs_source_1.catalogsSource;
this._overrideFonts = overrideFonts;
this._overrideVFS = overrideVFS;
this._documentOptions = {
...documentOptions,
defaultStyle: {
...this.defaultStyle(),
...documentOptions?.defaultStyle,
},
styles: {
...this.defaultDictionaryStyles(),
...documentOptions?.styles,
},
pageMargins: documentOptions?.pageMargins ?? this._defaultPageMargins,
};
this._options = options;
}
async buildRaw(data) {
const builderPdf = await this.buildPdf(data);
return new Promise((resolve, reject) => {
try {
builderPdf.getBuffer((pdfBuffer) => {
resolve(pdfBuffer.toString('binary'));
}, this._options);
}
catch (error) {
reject(error);
}
});
}
async buildBase64(data) {
const builderPdf = await this.buildPdf(data);
return new Promise((resolve, reject) => {
try {
builderPdf.getBase64((pdfBase64) => {
resolve(pdfBase64);
}, this._options);
}
catch (error) {
reject(error);
}
});
}
async buildPdf(data) {
const vfsFonts = await import('pdfmake/build/vfs_fonts.js');
const vfs = 'default' in vfsFonts ? vfsFonts.default : undefined;
if (vfs && !this._overrideVFS) {
this._overrideVFS = vfs.pdfMake.vfs;
}
const pdfTemplate = this._documentTranslator.translate(data, this._documentOptions, this._catalogs, this._primaryColor, this._bgGrayColor);
return pdfmake_js_1.default.createPdf(pdfTemplate, this.layouts(), this._overrideFonts, this._overrideVFS);
}
defaultStyle() {
return {
fontSize: 8,
};
}
}
exports.default = PdfMakerBuilder;