pdfmake with Angular 4 and webpack

Created on 28 Sep 2017  路  4Comments  路  Source: bpampuch/pdfmake

I tried the solutions proposed in #1058 but I'm using webpack not @angular/cli.
I defined the scripts in vendor.ts like this:

import 'pdfmake/build/pdfmake';
import 'pdfmake/build/vfs_fonts';

And I also declared pdfMake in my ProvidePlugin

new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'window.jQuery': 'jquery',
        Popper: ['popper.js', 'default'],
        pdfMake: 'pdfmake'
    }),

But how do I declare pdfFonts ? If I don't declare it, I get this error:
ERROR Error: File 'Roboto-Regular.ttf' not found in virtual file system

and if I do, I get this:
Could not find a declaration file for module 'pdfmake/build/pdfmake'

My component looks something like this ( removed the non-essential code and

//imports
declare var pdfMake: any;
declare var pdfFonts: any;

@Component....
export class MyComponent {
    constructor () {
         pdfMake.vfs = pdfFonts.pdfMake.vfs;
    }

    printFile () {
         var dd = { content: 'your pdf data' };
         pdfMake.createPdf(dd).download();
    }   
}

Most helpful comment

Hi, @dushkostanoeski

var pdfMake = require('pdfmake/build/pdfmake.js');
var pdfFonts = require('pdfmake/build/vfs_fonts.js');
pdfMake.vfs = pdfFonts.pdfMake.vfs; 

this fix it for me

All 4 comments

Hi, @dushkostanoeski

var pdfMake = require('pdfmake/build/pdfmake.js');
var pdfFonts = require('pdfmake/build/vfs_fonts.js');
pdfMake.vfs = pdfFonts.pdfMake.vfs; 

this fix it for me

Yup, this worked for me too, thanks!

The thing that bugs me is that it's not the angular way :)

@dushkostanoeski hi, can you tell me how do you resolve the problem at last?

I created a service:

export class PrinterService {
 private readonly pdfFonts: any;
    pdfMake: any;

    constructor() {
        this.pdfMake = require('pdfmake/build/pdfmake.js');
        this.pdfFonts = require('pdfmake/build/vfs_fonts.js');
        this.pdfMake.vfs = this.pdfFonts.pdfMake.vfs;
    }
}

and then just inject the service wherever you need

Was this page helpful?
0 / 5 - 0 ratings

Related issues

svenyonson picture svenyonson  路  3Comments

einfallstoll picture einfallstoll  路  3Comments

MathLavallee picture MathLavallee  路  3Comments

kamilkp picture kamilkp  路  3Comments

Christian24 picture Christian24  路  3Comments