Search code examples
angulartypescriptpdfmake

File 'Roboto-Regular.ttf' not found in virtual file system in Angular 6 (pdfMake)


I`m getting

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

when trying using pdfMake in angular 6.

I did

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

in typings.d.ts and also

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

in tsconfig.json.

In component where i`m using pdfmake i have

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

Seems like vfs_fonts.js loading because i added console.log to this file and it works.

Also i tryed just to add

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

But still getting the same error. Maybe someone got a solution for this?

UPD 1: Importing pdfmake.js before not solved a problem

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

SOLUTION:

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

and

pdfMake.vfs = pdfFonts.pdfMake.vfs;

SOLVED.


Solution

  • for ts file
    
    import pdfMake from "pdfmake/build/pdfmake";
    import pdfFonts from "pdfmake/build/vfs_fonts";
    
    constructor(){
        pdfMake.vfs = pdfFonts.pdfMake.vfs;
    }