Skip to content

Server side pdfmake embeds fonts incompletely #1659

@ValeSauer

Description

@ValeSauer

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:

  1. The one from the examples. However I think this is a bad example because it uses Roboto, which seems to be included anyway and works.
  2. The one from the server-side dev-playground. I thought this might solve path issues because I am working a windows platform, but it didn't work.
  3. The client side vs_fonts.js approach which is propoped in the github issues.

None of them worked for me. They actually all create the same result, which looks like that:
image

I can exclude the following causes:

  • incorrect path, it would throw errors. already tried out.
  • corrupted ttf files, as i tried it with various fonts and sources

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
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions