B.T
B.T

Reputation: 551

Service side PDFMake basic script returns error

I'm working on a Web-Application on which i need to generate PDFs using pdfmakelibrary. I have no problems when running it on client side (For printing) but I can't manage to get it to work on server side using node.js (to save my pdfs on the server).

My server is running on linux and node.js and npm are correctly installed. I also installed the pdfmake package using sudo npm i pdfmake. The module seems to be correctly installed as it is listed when using npm ls.

I then tried to run a simple script to generate a basic pdf :

var pdfmake = require('pdfmake');


var fonts = {
        Roboto: {
                normal: 'fonts/Roboto-Regular.ttf',
                bold: 'fonts/Roboto-Medium.ttf',
                italics: 'fonts/Roboto-Italic.ttf',
                bolditalics: 'fonts/Roboto-MediumItalic.ttf'
        }
};

var PdfPrinter = require('../src/printer');
var printer = new PdfPrinter(fonts);
var fs = require('fs');

var docDefinition = {
        content: [
                'First paragraph',
                'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
        ]
};

var pdfDoc = printer.createPdfKitDocument(docDefinition);
pdfDoc.pipe(fs.createWriteStream('pdfs/basics.pdf'));
pdfDoc.end();

When executing (sudo node pdftest.js) this script return the following error : Error: Cannot find module '../src/printer'

For me, this /printer folder should be located within the pdfmake folder, so I shouldn't have this error.

I tried to reinstall the package by running again sudo npm i pdfmake but it didn't change anything.

Also, this is what I get when I install it :

/home/odroid
`-- [email protected]
  `-- [email protected]
    `-- [email protected]
      `-- [email protected]
        `-- [email protected]
          `-- [email protected]

npm WARN enoent ENOENT: no such file or directory, open '/home/odroid/package.json'
npm WARN odroid No description
npm WARN odroid No repository field.
npm WARN odroid No README data
npm WARN odroid No license field.

Does anyone already got this problem ? Can you help me out ? Thanks !

Upvotes: 0

Views: 2548

Answers (1)

Antonio Narkevich
Antonio Narkevich

Reputation: 4326

The path to printer is specified incorrectly. If you install the package with npm install it's gonna be placed in node_modules folder. You can read more about package installation here.

So in order to run your code, I have changed the path to:

var PdfPrinter = require('./node_modules/pdfmake/src/printer');

Also please make sure:

  • you have fonts folder and Roboto exists inside
  • you have created the pdfs folder (mkdir pdfs)

I didn't touch the rest and was able to generate this document:

enter image description here

Upvotes: 2

Related Questions