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

ran*_*ery 6 typescript pdfmake angular

我得到了

错误错误:在虚拟文件系统中找不到文件“Roboto-Regular.ttf”

当尝试在 Angular 6 中使用 pdfMake 时。

我做到了

declare module 'pdfmake/build/pdfmake.js';
declare module 'pdfmake/build/vfs_fonts.js';`
Run Code Online (Sandbox Code Playgroud)

在 types.d.ts 中,还有

"typeRoots": [
  "node_modules/@types",
  "../src/typings.d.ts"
],
Run Code Online (Sandbox Code Playgroud)

在 tsconfig.json 中。

在我使用 pdfmake 的组件中

import 'pdfmake/build/vfs_fonts.js';
import * as pdfMake from 'pdfmake/build/pdfmake.js';
Run Code Online (Sandbox Code Playgroud)

看起来像 vfs_fonts.js 加载,因为我将 console.log 添加到此文件并且它可以工作。

我也尝试添加

<script src="./assets/fonts/vfs.js"></script>
Run Code Online (Sandbox Code Playgroud)

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

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

import * as pdfMake from 'pdfmake/build/pdfmake.js';
import 'pdfmake/build/vfs_fonts.js';
Run Code Online (Sandbox Code Playgroud)

解决方案:

import * as pdfFonts from 'pdfmake/build/vfs_fonts';
Run Code Online (Sandbox Code Playgroud)

pdfMake.vfs = pdfFonts.pdfMake.vfs;
Run Code Online (Sandbox Code Playgroud)

解决了。

小智 3

for ts file

import pdfMake from "pdfmake/build/pdfmake";
import pdfFonts from "pdfmake/build/vfs_fonts";

constructor(){
    pdfMake.vfs = pdfFonts.pdfMake.vfs;
}
Run Code Online (Sandbox Code Playgroud)

  • 我收到错误:“vfs”是只读的。你使用打字稿吗?你是怎么解决这个问题的? (2认同)