PdfMake Ionic 4 angular 自定义字体以支持阿姆哈拉语

PdfMake Ionic 4 angular custom font to support Amharic language

import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts.js';
pdfMake.vfs = pdfFonts.pdfMake.vfs;
pdfMake.fonts = {
    myFont: {
        normal: '../../../../assets/fonts/hiwua.ttf',
        bold: '../../../../assets/fonts/hiwua.ttf',
        italics: '../../../../assets/fonts/hiwua.ttf',
        bolditalics: '../../../../assets/fonts/hiwua.ttf',
    }
}
const docDefinition = {
    content: [ ....... ],
    defaultStyle: {
    font: 'myFont'
}

如何添加自定义字体并支持阿姆哈拉语?

确保按照 pdfmake guide for custom client-side font support 中的步骤操作:

  1. 将您的字体文件 (.ttf) 放入 /node_modules/pdfmake/examples/fonts/
  2. /node_modules/pdfmake/ 目录,运行 npm install 然后 gulp buildFonts
  3. 导入生成的/build/vfs_fonts.js/文件。我的导入看起来像这样(基于 this answer):
import * as pdfMakeConfig from 'pdfmake/build/pdfmake.js';
import * as pdfFonts from 'pdfmake/build/vfs_fonts.js';
pdfMakeConfig.vfs  = pdfFonts.pdfMake.vfs;
import * as pdfMake from 'pdfmake/build/pdfmake';

重要提示:在定义pdfMake.fonts时,只需使用文件名,而不是完整路径。我的看起来像这样:

pdfMakeConfig.fonts = {
    ethiopic : {
        normal: 'NotoSansEthiopic-Regular.ttf'
    }
};

然后你可以像这样在文档描述中使用它:

{
    text: 'foo',
    font: 'ethiopic'
}