Pdfkit: Can't embed font in browser version

Created on 9 Dec 2014  路  6Comments  路  Source: foliojs/pdfkit

Hey, so I'm trying to embed Lato-Regular.ttf which is located in /fonts/Lato-Regular.ttf.

Using .font('Lato-Regular.ttf) or even registering this font, yields an 'undefined is not a function' at ttf.coffee line 19. Upon further investigation it seems that the "require fs" for filesync isn't returning the correct object (this is just my guess, since I'm not incredibly familiar with node.js, but it doesn't look right [http://i.imgur.com/P8CkHbW.png]).

I'm running a XAMPP stack if it makes a difference.

Most helpful comment

You need to download the font using an ajax request first. PDFKit does not do this for you. Here's an example (untested):

var xhr = new XMLHttpRequest;
xhr.onload = function() {
  // set the font using the arraybuffer returned from the xhr
  doc.font(xhr.response);
};

xhr.responseType = 'arraybuffer';
xhr.open('GET', '/fonts/Lato-Regular.ttf', true);
xhr.send();

All 6 comments

You need to download the font using an ajax request first. PDFKit does not do this for you. Here's an example (untested):

var xhr = new XMLHttpRequest;
xhr.onload = function() {
  // set the font using the arraybuffer returned from the xhr
  doc.font(xhr.response);
};

xhr.responseType = 'arraybuffer';
xhr.open('GET', '/fonts/Lato-Regular.ttf', true);
xhr.send();

Much appreciated, figures I was missing something obvious.

Using window.fetch:

fetch('/fonts/Lato-Regular.ttf')
    .then(response => response.arrayBuffer())
    .then(font => doc.font(font));

is it possible to use this same method to register a font? it doesn't seem like it.

and does this work with the latest version? my pdf is coming up blank.

looks like woff is treated differently. works with ttf.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

balsamiqMichele picture balsamiqMichele  路  4Comments

stephen-last picture stephen-last  路  4Comments

nqgajanan picture nqgajanan  路  4Comments

michapixel picture michapixel  路  4Comments

oknoorap picture oknoorap  路  4Comments