|
| 1 | +import { ChangeDetectionStrategy, Component, signal, computed, inject, viewChild, OnInit } from '@angular/core'; |
| 2 | +import { IgxColumnComponent, IgxPdfExporterService, IgxPdfExporterOptions } from 'igniteui-angular/grids/core'; |
| 3 | +import { IgxGridComponent } from 'igniteui-angular/grids/grid'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Demonstrates PDF export with a custom Unicode font. |
| 7 | + * |
| 8 | + * The sample ships with Noto Sans (Latin/Cyrillic/Greek) loaded from |
| 9 | + * assets/fonts/noto-sans.json. Users can also upload their own .ttf font — |
| 10 | + * for example Noto Sans CJK for Japanese/Chinese/Korean support. |
| 11 | + * |
| 12 | + * All Noto fonts are licensed under the SIL Open Font License 1.1 |
| 13 | + * (see assets/fonts/OFL.txt). |
| 14 | + */ |
| 15 | +@Component({ |
| 16 | + selector: 'app-export-pdf-custom-font', |
| 17 | + templateUrl: './export-pdf-custom-font.component.html', |
| 18 | + styleUrls: ['./export-pdf-custom-font.component.scss'], |
| 19 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 20 | + imports: [IgxGridComponent, IgxColumnComponent] |
| 21 | +}) |
| 22 | +export class ExportPdfCustomFontComponent implements OnInit { |
| 23 | + private pdfExporter = inject(IgxPdfExporterService); |
| 24 | + |
| 25 | + protected readonly grid = viewChild.required<IgxGridComponent>('grid'); |
| 26 | + |
| 27 | + protected readonly isExporting = signal(false); |
| 28 | + protected readonly builtInFontLoaded = signal(false); |
| 29 | + protected readonly builtInFontLoading = signal(false); |
| 30 | + protected readonly uploadedFontName = signal(''); |
| 31 | + protected readonly exportStatus = signal(''); |
| 32 | + |
| 33 | + // Built-in Noto Sans (Latin / Cyrillic / Greek) |
| 34 | + private builtInFontData: string | null = null; |
| 35 | + private builtInBoldFontData: string | null = null; |
| 36 | + |
| 37 | + // User-uploaded font (e.g. Noto Sans CJK for Japanese) |
| 38 | + private uploadedFontData = signal<string | null>(null); |
| 39 | + |
| 40 | + protected readonly canExportBuiltIn = computed(() => this.builtInFontLoaded() && !this.isExporting()); |
| 41 | + protected readonly canExportUploaded = computed(() => !!this.uploadedFontData() && !this.isExporting()); |
| 42 | + |
| 43 | + protected readonly data = signal([ |
| 44 | + { Name: 'Александър Иванов', City: 'София', Product: '商品A', Amount: 1500 }, |
| 45 | + { Name: '田中太郎', City: '東京', Product: '商品B', Amount: 2300 }, |
| 46 | + { Name: 'Élise Müller', City: 'München', Product: '商品D', Amount: 3200 }, |
| 47 | + { Name: '王小明', City: '北京', Product: '商品E', Amount: 1750 }, |
| 48 | + { Name: 'Ирина Петрова', City: 'Санкт-Петербург', Product: '製品 F', Amount: 2890 } |
| 49 | + ]); |
| 50 | + |
| 51 | + public ngOnInit(): void { |
| 52 | + this.loadBuiltInFont(); |
| 53 | + } |
| 54 | + |
| 55 | + /** Loads the built-in Noto Sans font from application assets. */ |
| 56 | + private async loadBuiltInFont(): Promise<void> { |
| 57 | + this.builtInFontLoading.set(true); |
| 58 | + this.exportStatus.set('Loading built-in Noto Sans font…'); |
| 59 | + |
| 60 | + try { |
| 61 | + const response = await fetch('assets/fonts/noto-sans.json'); |
| 62 | + if (!response.ok) { |
| 63 | + throw new Error(`HTTP ${response.status}`); |
| 64 | + } |
| 65 | + const fontJson: { normal: string; bold: string } = await response.json(); |
| 66 | + |
| 67 | + this.builtInFontData = fontJson.normal; |
| 68 | + this.builtInBoldFontData = fontJson.bold; |
| 69 | + this.builtInFontLoaded.set(true); |
| 70 | + this.exportStatus.set('Noto Sans loaded — ready to export. Upload a CJK font for Japanese/Chinese/Korean support.'); |
| 71 | + } catch (error) { |
| 72 | + console.error('Failed to load built-in font:', error); |
| 73 | + this.exportStatus.set('Failed to load built-in Noto Sans font.'); |
| 74 | + } finally { |
| 75 | + this.builtInFontLoading.set(false); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** Handles the user uploading a custom .ttf font file. */ |
| 80 | + protected onFontFileSelected(event: Event): void { |
| 81 | + const input = event.target as HTMLInputElement; |
| 82 | + if (!input.files?.[0]) { |
| 83 | + return; |
| 84 | + } |
| 85 | + const file = input.files[0]; |
| 86 | + this.uploadedFontName.set(file.name); |
| 87 | + this.exportStatus.set(`Reading "${file.name}"…`); |
| 88 | + |
| 89 | + this.readFontFile(file).then(base64 => { |
| 90 | + this.uploadedFontData.set(base64); |
| 91 | + this.exportStatus.set(`"${file.name}" loaded — you can now export with the uploaded font.`); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + /** Export using the built-in Noto Sans font. */ |
| 96 | + protected exportWithBuiltInFont(): void { |
| 97 | + if (!this.builtInFontData) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + this.isExporting.set(true); |
| 102 | + this.exportStatus.set('Exporting PDF with Noto Sans…'); |
| 103 | + |
| 104 | + const options = new IgxPdfExporterOptions('NotoSansExport'); |
| 105 | + options.customFont = { |
| 106 | + name: 'NotoSans', |
| 107 | + data: this.builtInFontData |
| 108 | + }; |
| 109 | + |
| 110 | + if (this.builtInBoldFontData) { |
| 111 | + options.customFont.bold = { |
| 112 | + name: 'NotoSans-Bold', |
| 113 | + data: this.builtInBoldFontData |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + this.pdfExporter.exportEnded.subscribe({ |
| 118 | + next: () => { |
| 119 | + this.isExporting.set(false); |
| 120 | + this.exportStatus.set( |
| 121 | + 'PDF exported with Noto Sans. Note: CJK characters (Japanese, Chinese, Korean) require a CJK font.' |
| 122 | + ); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + this.pdfExporter.export(this.grid(), options); |
| 127 | + } |
| 128 | + |
| 129 | + /** Export using the user-uploaded font file. */ |
| 130 | + protected exportWithUploadedFont(): void { |
| 131 | + const fontData = this.uploadedFontData(); |
| 132 | + if (!fontData) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + this.isExporting.set(true); |
| 137 | + this.exportStatus.set('Exporting PDF with uploaded font…'); |
| 138 | + |
| 139 | + const options = new IgxPdfExporterOptions('CustomFontExport'); |
| 140 | + options.customFont = { |
| 141 | + name: 'CustomFont', |
| 142 | + data: fontData |
| 143 | + }; |
| 144 | + |
| 145 | + this.pdfExporter.exportEnded.subscribe({ |
| 146 | + next: () => { |
| 147 | + this.isExporting.set(false); |
| 148 | + this.exportStatus.set('PDF exported successfully with the uploaded font!'); |
| 149 | + } |
| 150 | + }); |
| 151 | + |
| 152 | + this.pdfExporter.export(this.grid(), options); |
| 153 | + } |
| 154 | + |
| 155 | + /** Export with the default PDF font (Helvetica). */ |
| 156 | + protected exportWithDefaultFont(): void { |
| 157 | + this.isExporting.set(true); |
| 158 | + this.exportStatus.set('Exporting PDF with default font (Helvetica)…'); |
| 159 | + |
| 160 | + const options = new IgxPdfExporterOptions('DefaultFontExport'); |
| 161 | + |
| 162 | + this.pdfExporter.exportEnded.subscribe({ |
| 163 | + next: () => { |
| 164 | + this.isExporting.set(false); |
| 165 | + this.exportStatus.set( |
| 166 | + 'PDF exported — non-Latin characters may not render correctly with the default Helvetica font.' |
| 167 | + ); |
| 168 | + } |
| 169 | + }); |
| 170 | + |
| 171 | + this.pdfExporter.export(this.grid(), options); |
| 172 | + } |
| 173 | + |
| 174 | + private readFontFile(file: File): Promise<string> { |
| 175 | + return new Promise((resolve, reject) => { |
| 176 | + const reader = new FileReader(); |
| 177 | + reader.onload = () => { |
| 178 | + const result = reader.result as string; |
| 179 | + const base64 = result.includes(',') ? result.split(',')[1] : result; |
| 180 | + resolve(base64); |
| 181 | + }; |
| 182 | + reader.onerror = () => reject(reader.error); |
| 183 | + reader.readAsDataURL(file); |
| 184 | + }); |
| 185 | + } |
| 186 | +} |
0 commit comments