How to create PDF in Node.js using PDFMake and vfs_fonts?

It looks like this question has been asked quite a few times with older versions of PDFMake, but hasn't been updated by what appears to be the latest directory structure. Also, copying fonts to the root fonts folder is not very good.

How do I get a server version of PDFMake ("pdfmake": "^ 0.1.31") running on Node.js with the vfs_fonts.js file included?

Install with npm on the command line

npm install pdfmake fs --save

      

Download the index.js app from Node.js with the following:

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('pdfmake/src/printer');
var printer = new PdfPrinter(fonts);

var dd = {
    content: [
        'First paragraph',
        'Another paragraph'
    ]
}
var pdfDoc = printer.createPdfKitDocument(dd);
pdfDoc.pipe(fs.createWriteStream('basics.pdf')).on('finish',function(){
    //success
});
pdfDoc.end();

      

Hit run and bam:

/usr/local/bin/node index.js
fs.js:640
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^
Error: ENOENT: no such file or directory, open 'fonts/Roboto-Regular.ttf'
    at Error (native)
    at Object.fs.openSync (fs.js:640:18)

      

The problem seems to be related to the location of the fonts / roboto files .... On the client side this is solved by including the vfs_fonts.js file. Server side, I'm not sure. There is NO font folder or .ttf files included. the example of a meteorite I found doesn't seem to apply.

Any ideas? All official examples refer to the src / fonts folder. Not a good way to install the npm install server module.

+6


source to share


2 answers


This is what I did to fix this issue.

Downloaded the module "roboto-font": "0.1.0" and assigned the path to these fonts in the Roboto object and it worked fine.



let fonts = {
    Roboto: {
        normal: 'node_modules/roboto-font/fonts/Roboto/roboto-regular-webfont.ttf',
        bold: 'node_modules/roboto-font/fonts/Roboto/roboto-bold-webfont.ttf',
        italics: 'node_modules/roboto-font/fonts/Roboto/roboto-italic-webfont.ttf',
        bolditalics: 'node_modules/roboto-font/fonts/Roboto/roboto-bolditalic-webfont.ttf'
    }
};
let printer = new pdfMake(fonts);
let pdfDoc = printer.createPdfKitDocument(pdfData);
pdfDoc.pipe(fs.createWriteStream(reportName));
pdfDoc.end();

      

+3


source


You need to download the Roboto font first https://fonts.google.com/specimen/Roboto and copy them inside the fonts folder. Update your font as follows:

var fonts = {
  Roboto: {
    normal: path.join(__dirname, '..', 'your_public_folder', '/fonts/Roboto-Regular.ttf'),
    bold: path.join(__dirname, '..', 'your_public_folder', '/fonts/Roboto-Medium.ttf'),
    italics: path.join(__dirname, '..', 'your_public_folder', '/fonts/Roboto-Italic.ttf'),
    bolditalics: path.join(__dirname, '..', 'your_public_folder', '/fonts/Roboto-MediumItalic.ttf')
  }
}

      



For this example replace " your_public_folder " with the name of the folder where you have all the html, css and js files.

+1


source







All Articles