Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFmake: pdfmake.createPdf is not a function

After installing pdfmake using npm:

npm install pdfmake --save-dev

and compiling bundles with webpack I get an error when running:

pdfmake = require 'pdfmake'
pdfmake.createPdf(doc_definition).download('test.pdf')

Saying:

pdfmake.createPdf is not a function

I've read this suggestion, installed the script-loader and changed the requirement to:

pdfmake = require 'script!pdfmake'

But that gave even more errors. Besides, I wouldn't know what that script loader would be needed. Any suggestions?

EDIT As described here it seems as if NPM installs the node (serverside) version of pdfmake instead of the browserside version, both of which have completely different API's. This should be the correct flow for the browserside:

npm install pdfmake --save-dev
pdfmake = require 'pdfmake'
pdfmake.createPdf(doc_definition).download('test.pdf')
like image 389
Berco Beute Avatar asked Feb 18 '16 23:02

Berco Beute


1 Answers

On server side method createPdf is not supported.

Server side pdfmake Nodejs - TypeError: pdfmake.createPdf is not a function #1355

BUT I tried var PDF = require('pdfmake/build/pdfmake') and the error is gone.

Also I suggest you to get the vfs_fonts too

var PDF_Fonts=require('pdfmake/build/vfs_fonts')

and set the pdfmake.vfs like this

PDF.vfs = PDF_Fonts.pdfMake.vfs;

like image 140
Christina Katsakoula Avatar answered Nov 16 '22 04:11

Christina Katsakoula