@webilix/nestjs-helper
Version:
Helper library for NestJS
285 lines • 14.7 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExportWordService = void 0;
const common_1 = require("@nestjs/common");
const docx = __importStar(require("docx"));
const fs = __importStar(require("fs"));
const helper_library_1 = require("@webilix/helper-library");
const jalali_date_time_1 = require("@webilix/jalali-date-time");
const export_type_1 = require("../export.type");
let ExportWordService = class ExportWordService {
constructor(config) {
this.config = config;
this.noBorder = {
top: { style: docx.BorderStyle.NONE },
left: { style: docx.BorderStyle.NONE },
right: { style: docx.BorderStyle.NONE },
bottom: { style: docx.BorderStyle.NONE },
};
this.getTextRun = (text, config) => new docx.TextRun({
text,
color: (config === null || config === void 0 ? void 0 : config.color) || this.config.textColor,
font: (config === null || config === void 0 ? void 0 : config.font) || this.config.fontFA,
size: (config === null || config === void 0 ? void 0 : config.size) || 20,
bold: (config === null || config === void 0 ? void 0 : config.bold) || false,
boldComplexScript: (config === null || config === void 0 ? void 0 : config.bold) || false,
break: (config === null || config === void 0 ? void 0 : config.newLine) ? 1 : 0,
});
this.getParagraph = (alignment, children) => new docx.Paragraph({ alignment, bidirectional: true, children });
this.getTH = (text) => new docx.TableCell({
shading: { color: this.config.backgroundColor, type: docx.ShadingType.SOLID },
verticalAlign: docx.VerticalAlign.CENTER,
borders: {
top: { style: docx.BorderStyle.SINGLE, size: 1, color: this.config.backgroundColor },
right: { style: docx.BorderStyle.SINGLE, size: 1, color: this.config.backgroundColor },
bottom: { style: docx.BorderStyle.SINGLE, size: 1, color: this.config.backgroundColor },
left: { style: docx.BorderStyle.SINGLE, size: 1, color: this.config.backgroundColor },
},
margins: { top: 100, right: 100, bottom: 100, left: 100 },
children: [
this.getParagraph('left', [this.getTextRun(text, { bold: true, color: this.config.foregroundColor })]),
],
});
this.getTD = (text, column) => {
const english = export_type_1.ExportColumnEnum[column.type].docx.english(column);
return new docx.TableCell({
verticalAlign: docx.VerticalAlign.TOP,
borders: {
top: { style: docx.BorderStyle.SINGLE, size: 1, color: this.config.backgroundColor },
right: { style: docx.BorderStyle.SINGLE, size: 1, color: this.config.backgroundColor },
bottom: { style: docx.BorderStyle.SINGLE, size: 1, color: this.config.backgroundColor },
left: { style: docx.BorderStyle.SINGLE, size: 1, color: this.config.backgroundColor },
},
margins: { top: 50, right: 100, bottom: 50, left: 100 },
children: [
this.getParagraph(english ? 'right' : 'left', text.split('\n').map((line, index) => this.getTextRun(line, {
size: 18,
newLine: index !== 0,
font: english ? this.config.fontEN : this.config.fontFA,
}))),
],
});
};
this.getLogo = (image) => helper_library_1.Helper.IS.empty(image)
? undefined
: new docx.TableCell({
verticalAlign: docx.VerticalAlign.CENTER,
borders: Object.assign({}, this.noBorder),
margins: { top: 0, right: 0, bottom: 100, left: 0 },
children: [
this.getParagraph('center', [
new docx.ImageRun({
type: 'png',
data: fs.readFileSync(image || ''),
transformation: { width: 50, height: 50 },
}),
]),
],
});
this.getDescription = (description) => !description
? undefined
: this.getParagraph('left', [
this.getTextRun(description, {
bold: true,
color: this.config.backgroundColor,
}),
]);
this.getHeader = (headers = []) => headers.length === 0
? undefined
: new docx.Table({
visuallyRightToLeft: true,
width: { size: 100, type: docx.WidthType.PERCENTAGE },
rows: headers.map((h) => new docx.TableRow({
children: [
new docx.TableCell({
borders: Object.assign({}, this.noBorder),
margins: { top: 0, right: 0, bottom: 0, left: 0 },
children: [
this.getParagraph('left', [
this.getTextRun(`${h.title}: `, {
bold: true,
color: this.config.backgroundColor,
}),
this.getTextRun(h.value, {
color: this.config.backgroundColor,
font: h.english ? this.config.fontEN : this.config.fontFA,
}),
]),
],
}),
],
})),
});
this.getWidth = (table) => {
const size = table.columns.map((c) => c.title.length);
table.columns.map((c, index) => {
table.rows.forEach((r) => {
const length = export_type_1.ExportColumnEnum[c.type].width(r[c.id], c);
if (length > size[index])
size[index] = length;
});
});
const sum = size.reduce((sum, s) => sum + s, 0);
const width = size.map((s) => 5 + Math.floor((s / sum) * 95));
while (width.reduce((sum, w) => sum + w, 0) > 100)
width[width.indexOf(Math.max(...width))]--;
while (width.reduce((sum, w) => sum + w, 0) < 100)
width[width.indexOf(Math.min(...width))]++;
return width;
};
}
export(path, table, options) {
return new Promise(async (resolve, reject) => {
try {
const styles = {
default: {
document: {
paragraph: { spacing: { before: 0, after: 0 } },
run: { font: this.config.fontFA, rightToLeft: true, size: 20 },
},
},
};
const th = new docx.TableRow({
children: table.columns.map((c) => this.getTH(c.title)),
tableHeader: true,
});
const td = table.rows.map((r) => {
return new docx.TableRow({
children: table.columns.map((c) => this.getTD(export_type_1.ExportColumnEnum[c.type].docx.value(r[c.id], c), c)),
cantSplit: true,
});
});
const header = new docx.Header({
children: [
new docx.Table({
visuallyRightToLeft: true,
width: { size: 100, type: docx.WidthType.PERCENTAGE },
columnWidths: this.config.logo ? [7, 70, 23] : [70, 30],
rows: [
new docx.TableRow({
children: [
this.getLogo(this.config.logo),
new docx.TableCell({
verticalAlign: docx.VerticalAlign.CENTER,
borders: Object.assign({}, this.noBorder),
margins: { top: 0, right: 0, bottom: 100, left: 0 },
children: [
this.getParagraph('left', [
this.getTextRun(table.title, {
size: 28,
bold: true,
color: this.config.backgroundColor,
}),
]),
this.getDescription(table.description),
this.getHeader(table.headers),
].filter((c) => c !== undefined),
}),
new docx.TableCell({
verticalAlign: docx.VerticalAlign.BOTTOM,
borders: Object.assign({}, this.noBorder),
margins: { top: 0, right: 0, bottom: 100, left: 0 },
children: (options === null || options === void 0 ? void 0 : options.hideDate)
? []
: [
this.getParagraph('right', [
this.getTextRun((0, jalali_date_time_1.JalaliDateTime)().toTitle(new Date(), {
format: 'W، d N Y',
})),
]),
],
}),
].filter((c) => c !== undefined),
}),
],
}),
],
});
const footer = new docx.Footer({
children: [
this.getParagraph('right', [
new docx.TextRun({
children: [docx.PageNumber.CURRENT, ' / ', docx.PageNumber.TOTAL_PAGES],
}),
]),
],
});
const doc = new docx.Document({
styles,
sections: [
{
properties: { page: { size: { orientation: docx.PageOrientation.LANDSCAPE } } },
headers: { default: header },
footers: { default: footer },
children: [
new docx.Table({
visuallyRightToLeft: true,
width: { size: 100, type: docx.WidthType.PERCENTAGE },
columnWidths: this.getWidth(table),
rows: [th, ...td],
}),
],
},
],
});
fs.writeFileSync(path, Buffer.from(await docx.Packer.toBuffer(doc)));
resolve();
}
catch (e) {
reject();
}
});
}
};
exports.ExportWordService = ExportWordService;
exports.ExportWordService = ExportWordService = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)('EXPORT_CONFIG')),
__metadata("design:paramtypes", [Object])
], ExportWordService);
//# sourceMappingURL=word.service.js.map