Skip to content

Commit b3fb7ad

Browse files
authored
feat(PDF): Adding customFont option for PDF exporter. (#16892)
1 parent 03ad637 commit b3fb7ad

4 files changed

Lines changed: 530 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,36 @@ All notable changes for each version of this project will be documented in this
66

77
### New Features
88

9+
- `IgxPdfExporterService`
10+
- Added `customFont` property to `IgxPdfExporterOptions` for Unicode character support in PDF exports. By default, the PDF exporter uses Helvetica, which only supports basic Latin characters. When exporting data containing non-Latin characters (Cyrillic, Chinese, Japanese, Arabic, Hebrew, special symbols, etc.), you can now provide a custom TrueType font (TTF) with the required character glyphs.
11+
12+
```typescript
13+
import { IgxPdfExporterService, IgxPdfExporterOptions } from 'igniteui-angular/grids/core';
14+
import { NOTO_SANS_REGULAR, NOTO_SANS_BOLD } from './fonts/noto-sans';
15+
16+
constructor(private pdfExporter: IgxPdfExporterService) {}
17+
18+
exportWithUnicodeSupport() {
19+
const options = new IgxPdfExporterOptions('GridExport');
20+
options.customFont = {
21+
name: 'NotoSans',
22+
data: NOTO_SANS_REGULAR, // Base64-encoded TTF font data
23+
bold: {
24+
name: 'NotoSans-Bold',
25+
data: NOTO_SANS_BOLD // Optional: Base64-encoded bold TTF font data
26+
}
27+
};
28+
29+
this.pdfExporter.export(this.grid, options);
30+
}
31+
```
32+
33+
Key features:
34+
- Supports any TrueType font (TTF) provided as Base64-encoded data
35+
- Optional bold font variant for header styling
36+
- Automatic fallback to Helvetica if custom font loading fails
37+
- Works with all grid types (IgxGrid, IgxTreeGrid, IgxHierarchicalGrid, IgxPivotGrid)
38+
939
- `IgxTooltipTarget`
1040
- Added new properties:
1141
- `showTriggers` - Which event triggers will show the tooltip. Expects a comma-separated string of different event triggers. Defaults to `pointerenter`.

projects/igniteui-angular/grids/core/src/services/pdf/pdf-exporter-options.ts

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,159 @@ export class IgxPdfExporterOptions extends IgxExporterOptionsBase {
4848
*/
4949
public fontSize = 10;
5050

51+
/**
52+
* Custom font configuration for Unicode character support in PDF exports.
53+
*
54+
* By default, the PDF exporter uses the built-in Helvetica font, which only supports
55+
* basic Latin characters. If your data contains non-Latin characters (such as Cyrillic,
56+
* Chinese, Japanese, Arabic, Hebrew, or special symbols), you must provide a custom
57+
* TrueType font (TTF) that includes the required character glyphs.
58+
*
59+
* The font data must be provided as a Base64-encoded string of the TTF file contents.
60+
* You can optionally provide a separate bold variant for header styling.
61+
*
62+
* @remarks
63+
* To convert a TTF file to Base64, you can use Node.js:
64+
* ```javascript
65+
* const fs = require('fs');
66+
* const fontData = fs.readFileSync('path/to/font.ttf');
67+
* const base64 = fontData.toString('base64');
68+
* fs.writeFileSync('font-base64.ts', `export const MY_FONT = '${base64}';`);
69+
* ```
70+
*
71+
* Or use an online Base64 encoder tool to convert your TTF file.
72+
*
73+
* @example
74+
* Basic usage with a single font (used for both normal and bold text):
75+
* ```typescript
76+
* import { NOTO_SANS_REGULAR } from './fonts/noto-sans';
77+
*
78+
* const options = new IgxPdfExporterOptions('Export');
79+
* options.customFont = {
80+
* name: 'NotoSans',
81+
* data: NOTO_SANS_REGULAR
82+
* };
83+
* this.pdfExporter.export(this.grid, options);
84+
* ```
85+
*
86+
* @example
87+
* Usage with separate normal and bold font variants:
88+
* ```typescript
89+
* import { NOTO_SANS_REGULAR, NOTO_SANS_BOLD } from './fonts/noto-sans';
90+
*
91+
* const options = new IgxPdfExporterOptions('Export');
92+
* options.customFont = {
93+
* name: 'NotoSans',
94+
* data: NOTO_SANS_REGULAR,
95+
* bold: {
96+
* name: 'NotoSans-Bold',
97+
* data: NOTO_SANS_BOLD
98+
* }
99+
* };
100+
* this.pdfExporter.export(this.grid, options);
101+
* ```
102+
*
103+
* @example
104+
* Recommended fonts for Unicode support:
105+
* - Noto Sans: Covers most Unicode scripts (https://fonts.google.com/noto)
106+
* - Arial Unicode MS: Comprehensive Unicode coverage
107+
* - Source Han Sans: Excellent CJK (Chinese, Japanese, Korean) support
108+
*
109+
* @memberof IgxPdfExporterOptions
110+
*/
111+
public customFont?: PdfUnicodeFont;
112+
51113
constructor(fileName: string) {
52114
super(fileName, '.pdf');
53115
}
54116
}
117+
118+
/**
119+
* Font configuration interface for PDF export with Unicode character support.
120+
*
121+
* This interface defines the structure for providing custom TrueType fonts (TTF)
122+
* to the PDF exporter. Custom fonts are required when exporting data that contains
123+
* non-Latin characters, as the default Helvetica font only supports basic Latin characters.
124+
*
125+
* @remarks
126+
* The font data must be Base64-encoded TTF file contents. Both the normal and optional
127+
* bold variants should be from the same font family for consistent styling.
128+
*
129+
* If the bold variant is not provided, the normal font will be used for both
130+
* regular text and headers (which are typically rendered in bold).
131+
*
132+
* @example
133+
* Minimal configuration:
134+
* ```typescript
135+
* const font: PdfUnicodeFont = {
136+
* name: 'MyFont',
137+
* data: 'AAEAAAATAQAABAAwR0...' // Base64-encoded TTF data
138+
* };
139+
* ```
140+
*
141+
* @example
142+
* Full configuration with bold variant:
143+
* ```typescript
144+
* const font: PdfUnicodeFont = {
145+
* name: 'MyFont-Regular',
146+
* data: 'AAEAAAATAQAABAAwR0...', // Base64-encoded regular TTF
147+
* bold: {
148+
* name: 'MyFont-Bold',
149+
* data: 'BBFAAAAUBQAACAAxS1...' // Base64-encoded bold TTF
150+
* }
151+
* };
152+
* ```
153+
*/
154+
export interface PdfUnicodeFont {
155+
/**
156+
* Base64-encoded font data from a TrueType Font (TTF) file.
157+
*
158+
* This should be the complete TTF file contents encoded as a Base64 string.
159+
* The font must include glyphs for all characters that may appear in your grid data.
160+
*
161+
* @remarks
162+
* To convert a TTF file to Base64 in Node.js:
163+
* ```javascript
164+
* const base64Data = require('fs').readFileSync('font.ttf').toString('base64');
165+
* ```
166+
*/
167+
data: string;
168+
169+
/**
170+
* The font family name to register with the PDF library.
171+
*
172+
* This name is used internally by jsPDF to reference the font. It should be
173+
* a simple identifier without spaces (e.g., 'NotoSans', 'ArialUnicode').
174+
*
175+
* @remarks
176+
* The name does not need to match the actual font's internal name, but using
177+
* a descriptive name helps with debugging and maintenance.
178+
*/
179+
name: string;
180+
181+
/**
182+
* Optional bold variant of the font for styling headers and emphasized text.
183+
*
184+
* If provided, this font will be used for table headers and any other text
185+
* that should appear in bold. If not provided, the normal font specified
186+
* by `data` and `name` will be used for all text, including headers.
187+
*
188+
* @remarks
189+
* For best visual results, use the bold variant from the same font family
190+
* as the regular font.
191+
*/
192+
bold?: {
193+
/**
194+
* Base64-encoded font data from a bold TrueType Font (TTF) file.
195+
*/
196+
data: string;
197+
198+
/**
199+
* The font family name for the bold variant.
200+
*
201+
* This should be different from the regular font name to avoid conflicts
202+
* (e.g., 'NotoSans-Bold' vs 'NotoSans').
203+
*/
204+
name: string;
205+
};
206+
}

0 commit comments

Comments
 (0)