Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server side pdfmake embeds fonts incompletely #1659

Closed
ValeSauer opened this issue Mar 1, 2019 · 3 comments · Fixed by foliojs/fontkit#191
Closed

Server side pdfmake embeds fonts incompletely #1659

ValeSauer opened this issue Mar 1, 2019 · 3 comments · Fixed by foliojs/fontkit#191

Comments

@ValeSauer
Copy link

ValeSauer commented Mar 1, 2019

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
});
@blikblum
Copy link
Contributor

blikblum commented Mar 1, 2019

For the record.

Loading FiraSans in https://fontkit-demo.now.sh/ works.

But in pdfkit not: https://repl.it/@blikblum/pdfkit-firasans

@ValeSauer
Copy link
Author

ValeSauer commented Mar 5, 2019

This PR foliojs/fontkit#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'
 },

@liborm85
Copy link
Collaborator

Released in 0.1.54.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants