fix: reports pdf template

This commit is contained in:
Ahmed Bouhuolia
2025-12-12 23:38:48 +02:00
parent 736f2c4109
commit 3cfb5cdde8
23 changed files with 173 additions and 42 deletions

View File

@@ -3,29 +3,30 @@ import { ITableColumn, ITableData, ITableRow } from '../types/Table.types';
import { FinancialTableStructure } from './FinancialTableStructure';
import { tableClassNames } from '../utils';
import { Injectable } from '@nestjs/common';
import { TemplateInjectable } from '../../TemplateInjectable/TemplateInjectable.service';
import { ChromiumlyTenancy } from '../../ChromiumlyTenancy/ChromiumlyTenancy.service';
import { renderFinancialSheetTemplateHtml } from '@bigcapital/pdf-templates';
@Injectable()
export class TableSheetPdf {
/**
* @param {TemplateInjectable} templateInjectable - The template injectable service.
* @param {ChromiumlyTenancy} chromiumlyTenancy - The chromiumly tenancy service.
*/
constructor(
private readonly templateInjectable: TemplateInjectable,
private readonly chromiumlyTenancy: ChromiumlyTenancy,
) {}
) { }
/**
* Converts the table data into a PDF format.
* @param {ITableData} table - The table data to be converted.
* @param {string} organizationName - The organization name.
* @param {string} sheetName - The name of the sheet.
* @param {string} sheetDate - The date of the sheet.
* @param {string} customCSS - Optional custom CSS to inject.
* @returns A promise that resolves with the PDF conversion result.
*/
public async convertToPdf(
table: ITableData,
organizationName: string,
sheetName: string,
sheetDate: string,
customCSS?: string,
@@ -33,19 +34,26 @@ export class TableSheetPdf {
// Prepare columns and rows for PDF conversion
const columns = this.tablePdfColumns(table.columns);
const rows = this.tablePdfRows(table.rows);
const landscape = columns.length > 4;
// Generate HTML content from the template
const htmlContent = await this.templateInjectable.render(
'financial-sheet',
{
table: { rows, columns },
sheetName,
sheetDate,
customCSS,
// Generate HTML content from the React template
const htmlContent = renderFinancialSheetTemplateHtml({
organizationName,
sheetName,
sheetDate,
table: {
columns: columns.map((col) => ({
key: col.key,
label: col.label,
style: (col as any).style, // style may be added during transformation
})),
rows: rows.map((row) => ({
cells: row.cells,
classNames: (row as any).classNames,
})),
},
);
customCSS,
});
// Convert the HTML content to PDF
return this.chromiumlyTenancy.convertHtmlContent(htmlContent, {
margins: { top: 0, bottom: 0, left: 0, right: 0 },
@@ -74,7 +82,6 @@ export class TableSheetPdf {
const flatNestedTree = curriedFlatNestedTree(R.__, {
nestedPrefix: '<span style="padding-left: 15px;"></span>',
});
// @ts-ignore
return R.compose(tableClassNames, flatNestedTree)(rows);
};