And*_*rs8 2 javascript pdfmake angular
安装自定义字体文件的标准方法是通过 gulp 工具,如下所述:https : //pdfmake.github.io/docs/fonts/custom-fonts-client-side/
但是如果这对你来说失败了,对我来说在 Windows 上它似乎是一个兔子洞,肯定有另一种方法来获取数据。是的。
方案一是修改node_modules/pdfmake/build目录下的vsf_fonts.js和pdfmake.js。在第一个中,您将添加数据,在第二个中,您将修改 defaultClientFonts 对象。我承认我说得有点含糊,因为问题是,如果您运行“npm 更新”或类似的操作,您将清除所有这些更改,下面的选项 2 更好,因为它保留了所有股票代码单独,最终它更简单。无需冗长,准确地描述了一个方法可以工作,那我,大概所有的人,建议不要。
我的应用程序是 Angular 8 设置,生成检查 PDF 客户端。所以我需要一个 MICR 字体。(编辑添加:当我升级到 Angular 9、Angular 10 和 Angular 11 时仍然有效。)
我想出的“如何使用自定义字体”的解决方案如下。
GnuMICR.ttf.Base64.encoded.ts 文件只包含这一行:
Run Code Online (Sandbox Code Playgroud)export const strGnuMICR:string = "AAEAAAALAIAAA.....FDAUQCQ1IAAAA=";
现在在您正在使用的主要代码中,您需要在顶部包含常规的 pdfmake :
Run Code Online (Sandbox Code Playgroud)import pdfMake from 'pdfmake/build/pdfmake' import pdfFonts from 'pdfmake/build/vfs_fonts'
以及导入您的字体(我的字体位于我创建的名为 fonts 的子目录中。不要使用 assets 子目录,这将在 dev 中工作,但不会被拉入以进行正确的构建):
Run Code Online (Sandbox Code Playgroud)import { strGnuMICR } from './../../fonts/GnuMICR.ttf.Base64.encoded'
pdfmake 谈到“虚拟文件系统”,当您看到变量“ vfs _fonts”时就暗示了这一点,但实际上,它只是一个数组。您还将看到默认值是像“Roboto-Regular.ttf”这样的文件名,但这又不是虚拟文件系统上的文件:“Roboto-Regular.ttf”只是 vfs_fonts.js 数组中数据的名称. 为清楚起见,您没有对实际文件做任何事情,所有这些都使用您已经构建的 base64 编码变量。
到您在主代码中执行工作的位置:
Run Code Online (Sandbox Code Playgroud)generatePDF(){ // this adds our base64 encoded data to the existing 'virtual file system' pdfFonts.pdfMake.vfs['GnuMICR_b64']=strGnuMICR // you're going to wipe out the standard stuff when you create this // variable, so we need to add the stock Roboto fonts back in. I // set all fonts to the same thing, as "italic MICR" would be silly. pdfMake.fonts = { Roboto: { normal: 'Roboto-Regular.ttf', bold: 'Roboto-Medium.ttf', italics: 'Roboto-Italic.ttf', bolditalics: 'Roboto-MediumItalic.ttf' }, GnuMICR:{ normal: 'GnuMICR_b64', bold: 'GnuMICR_b64', italics: 'GnuMICR_b64', bolditalics: 'GnuMICR_b64' }, } // that's it, all the install work is done // build the pdf, using our new font via a style we define let docDefinition = { content: [ {text:'regular Roboto text'}, {text:'and a line of MICR text follows:'}, {text:'C11111111C A222222222A 333333333C 0123456789',style:'micr'}, ], styles:{ micr:{ font: 'GnuMICR', fontSize: 12, } }, defaultStyle:{ font: 'Roboto', fontSize: 15, } }; // annnnnnd generate that PDF!! pdfMake.createPdf(docDefinition).open(); }
好处:
缺点:
| 归档时间: |
|
| 查看次数: |
1363 次 |
| 最近记录: |