How to add an open source font to TCPDF v.6

Disclaimer: These are notes for my own personal use. They are not intended to give advice or instruction to anyone. Treat them like alpha software: Any use of the content is entirely at your own risk, and I can give no support for any of it.

You only need to do this once. The moment you have integrated the new font, it will always be available to that particular TCPDF installation.

addTTFfont()

Three steps

  • Create a PHP file to call only this once (provided, of course, the font gets added to your TCPDF installation successfully).
  • Run the file by calling it in your web browser.
  • Test the success of font addition.

In depth information

Embed or not? (yes)

If you don’t embed the font, you rely on the recipient having that font. Since you already are in a situation where your font is not part of the standard fonts coming with TCPDF, the risk is high that your user does not have your new font. Embed it to play it safe!

Subsetting? (yes)

Yes, please. Unless your user needs to edit.

Subsetting means that TCPDF only embeds the bits of font info needed to display your document. As long as your user does not need to edit your document, this is fine and keeps your PDF file as small as possible.

The code

$goudy = TCPDF_FONTS::addTTFfont('OFLGoudyStMTT.ttf', 'TrueTypeUnicode', '', 32);

This adds the font to TCPDF – the font file “OFLGoudyStMTT.ttf” can be deleted after successful integration.

In this case, TCPDF added the following three files to its “fonts” subfolder: “oflgoudystmtt.z”, “oflgoudystmtt.php”, “oflgoudystmtt.ctg.z”

I can now delete (if I want to) the file “OFLGoudyStMTT.ttf”.

The Goudy font is now available to TCPDF like any other file:

$pdf->SetFont("oflgoudystmtt", "", 15.5);

If I want to embed the font as a subset only, I add the boolean “true” at the end of the series of arguments:

$pdf->SetFont("oflgoudystmtt", "", 15.5, "", "true");

The arguments are: family, style, size, font file, subset.

The font I am using in this example is Sorts Mill Goudy by Barry Schwartz – see link: https://theleagueofmoveabletype.com/sorts-mill-goudy/?

Links

A lot of the information above comes from these links: