Pdfkit: Registering OTF fonts doesn't work

Created on 14 Apr 2018  Â·  9Comments  Â·  Source: foliojs/pdfkit

Test

Trying to register and then use an OpenType font:

const fs = require('fs');
const PDFDocument = require('pdfkit');

const doc = new PDFDocument({ layout: 'landscape' });
const stream = fs.createWriteStream('out.pdf');

// Register the font family
// OTF fonts don't work, TTF does!
doc.registerFont('Montserrat Bold', 'fonts/v5/Montserrat-Bold.otf');

doc
  .font('Montserrat Bold')
  .text('This is a test of the emergency broadcast signal.');

doc.pipe(stream);
doc.end();

Result

The font didn't register. out.pdf rendered in Chrome or Firefox looks like this:
Got PDF with fonts not registered

Expected result

Font embedded.
Expected result, PDF with Montserrat Bold embedded

It works with TTF. This uses Montserrat Bold v5.001 OTF but I've tried other versions of Montserrat as well as other OpenType fonts. I couldn't get any .otf to work.

Using directly doesn't work either:

doc
  .font('fonts/v5/Montserrat-Bold.otf') // Doesn't work
  .text('This is a test of the emergency broadcast signal.');

Also tried to check in fontkit – no errors there.

fontkit.openSync('fonts/v5/Montserrat-Bold.otf');

Most helpful comment

All 9 comments

Same issue here. can't use otf fonts

Can you use ttf ? I found these work

yep, ttf fonts have no problems, but otf is not working... Cavertion otf to ttf doesn't work form me.

Any news on this one? I'm having a similar issue. In my case OTF fonts don't show up at all.

I cannot reproduce the issue with master version. Probably fixed in the mean time.

I used the font from https://www.fontsquirrel.com/fonts/list/popular. The linked font gives a Unknown font format error

I tried the Montserrat-Bold.otf font in master yesterday. I replaced palatino with it in demo/test.js. The resulting pdf does work in Acrobat, but it fails in both Chrome/Mac and Safari. In Chrome, it falls back to a san-serif font instead of Montserrat. In Safari or Mac Preview, it shows the wrong glyphs - mostly punctuation.

V0.9.0 fixes the issue for me, thank you!

This also fixed all the problems i had - both viewing in non-Acrobat viewers and some fonts crashing in font kit. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

huy-nguyen picture huy-nguyen  Â·  4Comments

tadarao picture tadarao  Â·  5Comments

GuiRitter picture GuiRitter  Â·  3Comments

leore picture leore  Â·  3Comments

ehaubold picture ehaubold  Â·  5Comments