angular - 在 Angular 6 的虚拟文件系统中找不到文件 'Roboto-Regular.ttf' (pdfMake)

标签 angular typescript pdfmake

我得到

ERROR Error: File 'Roboto-Regular.ttf' not found in virtual file system

尝试在 Angular 6 中使用 pdfMake 时。

我做到了

declare module 'pdfmake/build/pdfmake.js';
declare module 'pdfmake/build/vfs_fonts.js';`

在typings.d.ts 中,还有

"typeRoots": [
  "node_modules/@types",
  "../src/typings.d.ts"
],

在 tsconfig.json 中。

在我使用 pdfmake 的组件中

import 'pdfmake/build/vfs_fonts.js';
import * as pdfMake from 'pdfmake/build/pdfmake.js';

似乎正在加载 vfs_fonts.js,因为我将 console.log 添加到了该文件并且它可以工作。

我也尝试添加

<script src="./assets/fonts/vfs.js"></script>

但仍然遇到同样的错误。 也许有人对此有解决方案?

更新1: 之前导入pdfmake.js没有解决问题

import * as pdfMake from 'pdfmake/build/pdfmake.js';
import 'pdfmake/build/vfs_fonts.js';

解决方案:

import * as pdfFonts from 'pdfmake/build/vfs_fonts';

pdfMake.vfs = pdfFonts.pdfMake.vfs;

已解决。

最佳答案

使用 NPM 安装脚本:

npm i -S pdfmake
npm i -D @types/pdfmake

在你的 Angular 组件/服务中:

import * as pdfMake from 'pdfmake/build/pdfmake';
import * as pdfFonts from 'pdfmake/build/vfs_fonts';

// ...

public downloadPdf(): void
{
  const pdfData: TDocumentDefinitions = {
    content: [
      'First paragraph'
    ]
  };

  const pdfFile = pdfMake.createPdf(pdfData, null, null, pdfFonts.pdfMake.vfs);

  pdfFile.download();
}

关于angular - 在 Angular 6 的虚拟文件系统中找不到文件 'Roboto-Regular.ttf' (pdfMake),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53622307/

相关文章:

jquery - 重定向到主页时,登录后第一次下拉菜单不起作用

angularjs - 等待 Angular2 中定义元素

javascript - 如何在reactjs中使用javascript更改样式

javascript - pdfMake中图像之间的神奇空间

javascript - pdfmake.js 中表格的线宽

pdfmake - 如何动态检查 PDFMake 中的剩余页面高度?

angular - 作为属性组件的值的插值

angular - 如何从 typescript 中另一个函数的外部访问函数内部的变量?

javascript - 如何在 Nest.js 中使用查询参数?

angular - 使用 createReducer 函数时为生产构建 angular+ngrx 8 时出错