Pdfmake: Cannot assign to 'vfs' because it is a constant or a read-only property.

Created on 20 Jul 2019  路  8Comments  路  Source: bpampuch/pdfmake

Hi,

I am using Ionic 4 and as instructed, this is what I am doing with the latest version of plugin (0.1.57)
After running npm install pdfmake

in my page.ts :

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

the last line returns error saying it cannot be assigned as it is constant ? Do I have to do any other changes as well ?

Most helpful comment

Add the font files in createPDF

pdfMake.createPdf(
      this.docDefinition,
      {},
      {
        // Default font should still be available
        Roboto: {
          normal: 'Roboto-Regular.ttf',
          bold: 'Roboto-Medium.ttf',
          italics: 'Roboto-Italic.ttf',
          bolditalics: 'Roboto-Italic.ttf'
        },
        // Make sure you define all 4 components - normal, bold, italics, bolditalics - (even if they all point to the same font file)
        TimesNewRoman: {
          normal: 'Times-New-Roman-Regular.ttf',
          bold: 'Times-New-Roman-Bold.ttf',
          italics: 'Times-New-Roman-Italics.ttf',
          bolditalics: 'Times-New-Roman-Italics.ttf'
        }
      },
      pdfFonts.pdfMake.vfs);

All 8 comments

import pdfMake from 'pdfmake/build/pdfmake'
import pdfFonts from ...

The above syntax gives error.

OK. Good luck.

Do u have any idea what can be the issue ? I even tried to downgrade but still had the same error.

@KamayaniT Add code like this

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

instead of your original imports.

Credit to @blikblum for saying this. I just did this and it worked for me.
Thank you!

All the best :)

All is in documenation.

This example from SO helped me (Angular 7)

Otherwise TS complains that pdfmake/build/pdfmake doesn't have a default export when imported as import pdfMake from 'pdfMake/build/pdfmake'. Linked example also solves an issue where pdfMake.vfs is interpreted as read-only.

Add the font files in createPDF

pdfMake.createPdf(
      this.docDefinition,
      {},
      {
        // Default font should still be available
        Roboto: {
          normal: 'Roboto-Regular.ttf',
          bold: 'Roboto-Medium.ttf',
          italics: 'Roboto-Italic.ttf',
          bolditalics: 'Roboto-Italic.ttf'
        },
        // Make sure you define all 4 components - normal, bold, italics, bolditalics - (even if they all point to the same font file)
        TimesNewRoman: {
          normal: 'Times-New-Roman-Regular.ttf',
          bold: 'Times-New-Roman-Bold.ttf',
          italics: 'Times-New-Roman-Italics.ttf',
          bolditalics: 'Times-New-Roman-Italics.ttf'
        }
      },
      pdfFonts.pdfMake.vfs);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

imoum007 picture imoum007  路  3Comments

michaelqiji picture michaelqiji  路  3Comments

davidyeiser picture davidyeiser  路  3Comments

kamilkp picture kamilkp  路  3Comments

ValeSauer picture ValeSauer  路  3Comments