Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

TCPDF stops on a custom or OpenType font with a font definition error

Your error

TCPDF ERROR: Could not include font definition file: opensans1 more line it can show

Fix it fast

Most common cause, 1 of 4

No definition file was ever generated for the font

Convert the font once with the bundled command-line tool, or from PHP with TCPDF_FONTS::addTTFfont.

Fix
php vendor/tecnickcom/tcpdf/tools/tcpdf_addfont.php -i /path/to/OpenSans-Regular.ttf,/path/to/OpenSans-Bold.ttf -o /var/www/app/fonts/

// or in a build script (writes to K_PATH_FONTS)
define('K_PATH_FONTS', '/var/www/app/fonts/');
require 'vendor/autoload.php';
$name = TCPDF_FONTS::addTTFfont('/path/to/OpenSans-Regular.ttf');
// 'opensans' on success, false on failure
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

TCPDF ERROR: Could not include font definition file means SetFont asked AddFont for a family whose generated definition is not in K_PATH_FONTS. TCPDF 6.11 cannot read a raw .ttf or .otf, and its converter rejects OpenType files with CFF outlines. Convert a TrueType build with tcpdf_addfont.php and use the returned name; on TCPDF 7, import with tc-lib-pdf-font instead.

  • Applies to TCPDF 6.11, TCPDF 7.0, PHP 8.4, PHP 8.5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Check three things at once: which TCPDF line Composer installed, what is really in the fonts folder, and what kind of font file you have. An OpenType file with CFF outlines starts with the four bytes OTTO.

Run this first
composer show tecnickcom/tcpdf | grep -E '^versions'
ls -l /var/www/app/fonts/ | head
head -c 4 /path/to/MyFont.otf; echo

Then find the line you see in this list and open its fix:

  1. 1No definition file was ever generated for the fontThe error names a family such as opensans, and the fonts folder has no opensans.php
  2. 2The font is OpenType with CFF outlines, which the 6.x converter rejectshead prints OTTO for the font file, and addTTFfont() returns false instead of a name
  3. 3The name passed to SetFont is not the name the converter producedA definition file exists, but under another name, for example opensansb.php while the code asks for OpenSans-Bold
  4. 4The project moved to TCPDF 7, which ignores the old .php definitionsThe same font code worked before, and composer show now reports tecnickcom/tcpdf 7.0.x

What does the error look like?

  • TCPDF ERROR: Could not include font definition file: opensans
  • Fatal error: Uncaught Exception: TCPDF ERROR: Could not include font definition file: myfont

Cause 1 of 4

No definition file was ever generated for the font

You seeThe error names a family such as opensans, and the fonts folder has no opensans.php

Check

Could not include font definition file on TCPDF 6 means AddFont found no generated .php definition for the family. Its docblock says a definition file has to be generated first and kept in the folder named by K_PATH_FONTS; SetFont calls AddFont, so a plain SetFont with a raw .ttf family fails the same way.

Check
ls -l /var/www/app/fonts/opensans*
grep -rn "K_PATH_FONTS" config/ src/ 2>/dev/null | head

Fix

Convert the font once with the bundled command-line tool, or from PHP with TCPDF_FONTS::addTTFfont. Both write the .php definition and its data files into a folder that must be writable, and the PHP call returns the font name to use.

Fix
php vendor/tecnickcom/tcpdf/tools/tcpdf_addfont.php -i /path/to/OpenSans-Regular.ttf,/path/to/OpenSans-Bold.ttf -o /var/www/app/fonts/

// or in a build script (writes to K_PATH_FONTS)
define('K_PATH_FONTS', '/var/www/app/fonts/');
require 'vendor/autoload.php';
$name = TCPDF_FONTS::addTTFfont('/path/to/OpenSans-Regular.ttf');
// 'opensans' on success, false on failure

Verify

The definition files exist and SetFont no longer throws.

Verify
ls -l /var/www/app/fonts/opensans.php /var/www/app/fonts/opensansb.php
php -r "define('K_PATH_FONTS','/var/www/app/fonts/'); require 'vendor/autoload.php'; \$p=new TCPDF(); \$p->SetFont('opensans','',12); echo 'ok';"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 4

The font is OpenType with CFF outlines, which the 6.x converter rejects

You seehead prints OTTO for the font file, and addTTFfont() returns false instead of a name

Check

A font definition error after converting an .otf file usually means the converter refused it and wrote nothing. TCPDF 6 reads the type from the first bytes, not from the extension, and returns false for a file that starts with OTTO; the source comment calls it an unsupported OpenType format with CFF data.

Check
head -c 4 /path/to/MyFont.otf; echo
php -r "define('K_PATH_FONTS','/var/www/app/fonts/'); require 'vendor/autoload.php'; var_dump(TCPDF_FONTS::addTTFfont('/path/to/MyFont.otf'));"

Fix

Use the TrueType (.ttf) build of the same family, which most font projects ship next to the .otf files, and run the conversion from the first cause on it. If there is none, convert the outlines with a font editor first and check that the licence allows a modified copy.

Fix
php vendor/tecnickcom/tcpdf/tools/tcpdf_addfont.php -i /path/to/MyFont-Regular.ttf -o /var/www/app/fonts/

Verify

A .php file with the new name appears in the output folder.

Verify
ls -l /var/www/app/fonts/myfont*.php

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 4

The name passed to SetFont is not the name the converter produced

You seeA definition file exists, but under another name, for example opensansb.php while the code asks for OpenSans-Bold

Check

Could not include font definition file with a converted font in place means SetFont asks for a name that does not match the generated file. The converter lowercases the file name, removes everything except letters, digits and underscores, turns bold into b and italic or oblique into i, and drops regular; AddFont then looks for the family without spaces plus the style letters plus .php, and retries without the style.

Check
ls /var/www/app/fonts/ | grep -i opensans

Fix

Use the returned name as the family and pass bold or italic through the style argument. Define K_PATH_FONTS, with a trailing slash, before TCPDF is loaded so it points at the folder with the files.

Fix
define('K_PATH_FONTS', '/var/www/app/fonts/');
require 'vendor/autoload.php';

$pdf->SetFont('opensans', '', 12);   // opensans.php
$pdf->SetFont('opensans', 'B', 12);  // opensansb.php

Verify

Both styles render in a test PDF without an exception.

Verify
php -r "define('K_PATH_FONTS','/var/www/app/fonts/'); require 'vendor/autoload.php'; \$p=new TCPDF(); \$p->AddPage(); \$p->SetFont('opensans','B',12); \$p->Write(0,'test'); \$p->Output('/tmp/t.pdf','F'); echo 'ok';"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

The project moved to TCPDF 7, which ignores the old .php definitions

You seeThe same font code worked before, and composer show now reports tecnickcom/tcpdf 7.0.x

Check

A font definition error right after an upgrade to TCPDF 7 means the old fontname.php plus fontname.z files are no longer read. The 7.0.0 changelog marks TCPDF as deprecated and maintenance-only, drops the legacy PHP descriptors and the bundled fonts folder, and resolves fonts through tc-lib-pdf-font, whose JSON descriptors AddFont accepts.

Check
composer show tecnickcom/tcpdf | grep -E '^versions'
ls vendor/tecnickcom/tc-lib-pdf-font/target/fonts/core/helvetica.json

Fix

Import each custom font with the tc-lib-pdf-font converter into a folder outside vendor, because Composer replaces the package folder on update, and point K_PATH_FONTS at it; TCPDF searches that folder before the tc-lib assets. Use 7.0.23 or newer, which fixed a custom K_PATH_FONTS hiding the tc-lib fonts.

Fix
cd vendor/tecnickcom/tc-lib-pdf-font
php util/convert.php --outpath=/var/www/app/fonts/ --type=TrueTypeUnicode --flags=32 --fonts=/path/to/MyFont-Regular.ttf,/path/to/MyFont-Bold.ttf

Verify

The output folder holds a .json file with its .ctg.z next to it for each font, and the PDF builds.

Verify
ls -l /var/www/app/fonts/

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Convert fonts in a build step and commit the generated files, so production never needs a writable fonts folder.
  • Pin tecnickcom/tcpdf to ^6.11 or ^7.0 in composer.json, so a major upgrade cannot swap the font format under you.
  • Keep the converter's returned font name in one constant and use it in every SetFont call.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Spotless Laundry, EZ Estimater.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.