I was searching for answers all over github and stackexchange and could not find a working solution for this issue.
I try to embed font FiraSans. However, it seems to embedded incompletely. Only a view characters are actually displayed correctly. I can copy even the "invisible" characters to any text editor and I receive the correct text.
I tried three approaches:
None of them worked for me. They actually all create the same result, which looks like that:
I can exclude the following causes:
Does anyone have a solution for this?
My code:
const pdfMakePrinter = require('pdfmake/src/printer');
const fs = require('fs')
var path = require('path');
const docDefinition = {
content: [
'abcdefghijklmnopqrstuvwABCDEFGHIJKLMNOPQRSTUVW0123456789',
],
defaultStyle: {
font: 'FiraSans'
}
};
var generatePdf = function (docDefinition, callback) {
try {
const fontDescriptors = {
FiraSans: {
normal: './fonts/FiraSans-Regular.ttf',
//normal: path.join(__dirname, '..', 'pdf-generate', '/fonts/FiraSans-Regular.ttf'),
//normal: new Buffer(require('pdfmake/build/vfs_fonts.js').pdfMake.vfs['FiraSans-Regular.ttf'], 'base64'),
bold: '/fonts/FiraSans-Medium.ttf',
italics: '/fonts/FiraSans-Italic.ttf',
bolditalics: '/fonts/FiraSans-MediumItalic.ttf'
}
};
const printer = new pdfMakePrinter(fontDescriptors);
const doc = printer.createPdfKitDocument(docDefinition);
doc.pipe(
fs.createWriteStream('docs/filename.pdf').on("error", (err) => {
console.error(err.message);
})
);
doc.on('end', () => {
console.log("PDF successfully created and stored");
});
doc.end();
} catch (err) {
throw (err);
}
};
generatePdf(docDefinition, (response) => {
res.setHeader('Content-Type', 'application/pdf');
res.send(response); // Buffer data
});
For the record.
Loading FiraSans in https://fontkit-demo.now.sh/ works.
But in pdfkit not: https://repl.it/@blikblum/pdfkit-firasans
This PR https://github.com/foliojs/fontkit/pull/191 solves the issue in way, that the server side embedding works with the code below. There are no other workarounds or anything needed.
FiraSans: {
normal: './fonts/FiraSans/FiraSans-Regular.ttf',
bold: './fonts/FiraSans/FiraSans-Medium.ttf',
italics: './fonts/FiraSans/FiraSans-Italic.ttf',
bolditalics: './fonts/FiraSans/FiraSans-MediumItalic.ttf'
},
Released in 0.1.54.
Most helpful comment
Released in 0.1.54.