Skip to content

Commit 02c31b8

Browse files
Copilotkdinev
andcommitted
Add text truncation for multi-column header text in PDF export
Implemented text truncation with ellipsis for column headers when text is wider than the available header space. This applies to both: 1. Multi-level column headers (column groups) in drawMultiLevelHeaders 2. Simple column headers in drawTableHeaders The truncation logic: - Calculates max text width as column width minus 10px padding (5px each side) - If header text exceeds available space, truncates character by character - Adds ellipsis (...) to indicate truncation - Similar to existing cell value truncation logic Also added test case to verify truncation works with nested column groups on smaller page size (a5) where truncation is more likely to occur. This prevents header text from overflowing cell boundaries and overlapping with adjacent headers, maintaining proper PDF layout. Co-authored-by: kdinev <[email protected]>
1 parent 1381a69 commit 02c31b8

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

projects/igniteui-angular/src/lib/services/pdf/pdf-exporter-grid.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,29 @@ describe('PDF Grid Exporter', () => {
363363

364364
exporter.export(grid, options);
365365
});
366+
367+
it('should truncate long header text with ellipsis in multi-column headers', (done) => {
368+
TestBed.configureTestingModule({
369+
imports: [
370+
NoopAnimationsModule,
371+
NestedColumnGroupsGridComponent
372+
]
373+
}).compileComponents();
374+
375+
const fix = TestBed.createComponent(NestedColumnGroupsGridComponent);
376+
fix.detectChanges();
377+
378+
const grid = fix.componentInstance.grid;
379+
380+
exporter.exportEnded.pipe(first()).subscribe((args) => {
381+
expect(ExportUtilities.saveBlobToFile).toHaveBeenCalledTimes(1);
382+
// The PDF should be created successfully even with long header text
383+
expect(args.pdf).toBeDefined();
384+
done();
385+
});
386+
387+
// Use smaller page size to force truncation
388+
options.pageSize = 'a5';
389+
exporter.export(grid, options);
390+
});
366391
});

projects/igniteui-angular/src/lib/services/pdf/pdf-exporter.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,18 @@ export class IgxPdfExporterService extends IgxBaseExporter {
279279
pdf.rect(xPosition, yPosition, width, headerHeight);
280280
}
281281

282-
// Center text in cell
283-
const headerText = col.header || col.field || '';
282+
// Center text in cell with truncation if needed
283+
let headerText = col.header || col.field || '';
284+
const maxTextWidth = width - 10; // Leave 5px padding on each side
285+
286+
// Truncate text if it's too long
287+
if (pdf.getTextWidth(headerText) > maxTextWidth) {
288+
while (pdf.getTextWidth(headerText + '...') > maxTextWidth && headerText.length > 0) {
289+
headerText = headerText.substring(0, headerText.length - 1);
290+
}
291+
headerText += '...';
292+
}
293+
284294
const textWidth = pdf.getTextWidth(headerText);
285295
const textX = xPosition + (width - textWidth) / 2;
286296
const textY = yPosition + headerHeight / 2 + options.fontSize / 3;
@@ -314,12 +324,21 @@ export class IgxPdfExporterService extends IgxBaseExporter {
314324

315325
columns.forEach((col, index) => {
316326
const xPosition = xStart + (index * columnWidth);
317-
const headerText = col.header || col.field;
327+
let headerText = col.header || col.field;
318328

319329
if (options.showTableBorders) {
320330
pdf.rect(xPosition, yPosition, columnWidth, headerHeight);
321331
}
322332

333+
// Truncate text if it's too long
334+
const maxTextWidth = columnWidth - 10; // Leave 5px padding on each side
335+
if (pdf.getTextWidth(headerText) > maxTextWidth) {
336+
while (pdf.getTextWidth(headerText + '...') > maxTextWidth && headerText.length > 0) {
337+
headerText = headerText.substring(0, headerText.length - 1);
338+
}
339+
headerText += '...';
340+
}
341+
323342
// Center text in cell
324343
const textWidth = pdf.getTextWidth(headerText);
325344
const textX = xPosition + (columnWidth - textWidth) / 2;

0 commit comments

Comments
 (0)