4

I am developing a vsts web extension . I want a to use PDFmake.js file to generate a pdf. PDFmake.js file is imported to node_nodules folder by installing as npm install pdfmake.

I want to import this js file to my ts file. this is how i am doing ,

import pdfmake = require("../node_modules/pdfmake/build/pdfmake");

It gives me below error.

Severity    Code    Description Project File    Line    Suppression State
Error   TS5055  Cannot write file 'D:/TFS/DM Helper Tools/DEV/WebExtension/RapidCCB/build/pdfmake.js' because it would overwrite input file.    <Unknown>       1   Active
Dee_wab
  • 1,171
  • 1
  • 10
  • 23
Anusha B
  • 95
  • 1
  • 1
  • 8

5 Answers5

8

I found following solution with using combination require+import and without any special chamges in tsconfig.json. I used in my case pdfmake-unicode, but vfs_fonts should work similarly, I suppose.

// on pdfmake: 0.1.38, pdfmake-unicode: 0.0.1

Install:

npm install pdfmake --save
npm install pdfmake-unicode --save
npm install @types/pdfmake --save-dev

Import:

const pdfMakeX = require('pdfmake/build/pdfmake.js');
const pdfFontsX = require('pdfmake-unicode/dist/pdfmake-unicode.js');
pdfMakeX.vfs = pdfFontsX.pdfMake.vfs;
import * as pdfMake from 'pdfmake/build/pdfmake';

Generate:

downloadTest = () => {
    const docDefinition = {
        content: [{
            table: {
                headerRows: 1,
                widths: ['*', 'auto', 100, '*'],
                body: [
                    ['First', 'Second', 'Third', 'Последняя'],
                    ['Value 1', 'Value 2', 'Value 3', 'Value 4'],
                    [{ text: 'Bold value', bold: true }, 'Val 2', 'Val 3', 'Чё']
                ]
            }
        }]
    };
    pdfMake.createPdf(docDefinition).download('pdfmake.pdf');
}

Use:

<button onClick={this.downloadTest}>Download</button>
Ruslan
  • 1,076
  • 10
  • 10
7

Following the instruction:

First,

npm install pdfmake --save

Second, add below to typings.d.ts:

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

Third, in the file where pdfMake is being used, either component or service, add below lines:

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

tsconfig.json

{
  "compileOnSave": true,
  "compilerOptions": {
    "baseUrl": ".",
    "module": "commonjs",
    "noImplicitAny": true,
    "removeComments": true,
    "sourceMap": true,
    "target": "ES5",
    "forceConsistentCasingInFileNames": true,
    "strictNullChecks": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,

    "typeRoots": [],
    "types": [] //Explicitly specify an empty array so that the TS2 @types modules are not acquired since we aren't ready for them yet.
  },
  "exclude": ["node_modules"]
}

Last, use pdfMake.xxx() as usual.

See this link

manish kumar
  • 4,412
  • 4
  • 34
  • 51
  • I cannot follow the second step .i am developing a vsts extemsion and not using angular .all i have is a TS file . – Anusha B May 29 '18 at 06:17
  • You just make a typings.d.ts , which has typescript declaration [read more](https://angular.io/guide/typescript-configuration) – manish kumar May 29 '18 at 08:00
  • After doing all this, i am getting so many error in lib.d.ts – Anusha B May 30 '18 at 09:38
  • you might not be having a tsconfig.json. See the updated answer @AnushaB – manish kumar May 31 '18 at 04:49
  • 1
    You don't need the step of modifying `typings.d.ts`. You can install typings for pdfmake from here https://www.npmjs.com/package/@types/pdfmake. +1 for rest of the steps :) – Naveen Jun 12 '19 at 09:17
  • should we even try @types/pdfmake? it seems like the package is half-backed. https://github.com/DefinitelyTyped/DefinitelyTyped/issues There are more than 3k opened issue. I just hope the author from pdfmake can officially support typescipt. – Weilies Aug 19 '20 at 03:32
4

If you're using a "Typescript", then:

  • install:
npm i pdfmake --save
  • import:
import * as pdfMake from "pdfmake / build / pdfmake";
import * as pdfFonts from 'pdfmake / build / vfs_fonts';

(pdfMake as any).vfs = pdfFonts.pdfMake.vfs;
1

Here's a Angular 7 component based on @Ruslan example.

import {Component, Input, OnInit} from '@angular/core';

// configure pdf settings
import * as pdfMakeConfig from 'pdfmake/build/pdfmake.js';
import * as pdfFontsX from 'pdfmake-unicode/dist/pdfmake-unicode.js';
pdfMakeConfig.vfs = pdfFontsX.pdfMake.vfs;

// import core lib
import * as pdfMake from 'pdfmake/build/pdfmake';

@Component({
  selector: 'app-pdf',
  templateUrl: './pdf.component.html',
  styleUrls: ['./pdf.component.scss']
})
export class PdfComponent implements OnInit {
  // see https://pdfmake.github.io/docs/
  @Input() pdf: any;
  @Input() filename = 'download.pdf';
  @Input() demo: boolean;

  constructor() { }

  ngOnInit() {
    if (this.demo) {
      this.pdf = {
        content: [
          {text: 'PdfComponent Example', style: 'header'},
          {text: 'This was generated using Angular and PdfMake', style: 'body'},
          {text: 'PdfMake', link: 'https://pdfmake.github.io/docs/', style: 'link'}
        ],
        styles: {
          header: {
            fontSize: 22,
            bold: true
          },
          body: {
            fontSize: 12
          },
          link: {
            fontSize: 12,
            color: '#03A9F4'
          }
        }
      };
    }
  }

  downloadTest() {
    pdfMake.createPdf(this.pdf).download(this.filename);
  }

}
<button (click)="downloadTest()">Download</button>
ForrestLyman
  • 1,544
  • 2
  • 17
  • 24
0

For anyone looking to make this work with Ionic 5 & Angular 10 with pdfMake v0.1.68 just use below imports

import { Component, OnInit } from '@angular/core';
const pdfMake = require('pdfmake/build/pdfmake.js');
const pdfFonts = require("pdfmake/build/vfs_fonts");
pdfMake.vfs = pdfFonts.pdfMake.vfs;
import * as pdfMake from 'pdfmake/build/pdfmake';

@Component({
....
....
})
export class pdfPage implements OnInit {
...
    

  createPDF(){

     if(this.platform.is('cordova')){

     }else{
      pdfMake.createPdf(docDefinition).download('pdfmake.pdf');
     }
  }
}

If you get this error on build/serve

error TS2591: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i @types/node` and then add `node` to the types field in your tsconfig.

Then run command

npm i @types/node

and update only /tsconfig.app.json as below:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": ["node"] <---- add this line ONLY
  },
Sunil Kumar
  • 1,718
  • 16
  • 33