16

I am a web programmer with no in-depth knowledge of fonts and am struggling to get TCPDF to include our custom OpenType font. We have bought OpenType font files (.oft), which are not protected by any kind of DRM.

A lot of questions regarding this error message end up getting the same advice. I've set the correct file permissions for the folders used by TCPDF (755) and I have no trouble using the addTTFfont() to including .ttf TrueType fonts like so:

$pdf->addTTFfont('/path-to-font/DejaVuSans.ttf', 'TrueTypeUnicode', '', 32);

So I've made up the following code to include our OpenFont type. The addTTFfont() documentation seems to indicate support of OpenTypeUnicode and OpenType parameters.

$pdf->addTTFfont('/path-to-font/customfont.otf', 'OpenTypeUnicode', '', 96);

Which results in:

TCPDF ERROR: Could not include font definition file:

We're using TCPDF v6.0.020 and I've been reading the TCPDF Fonts information page with no luck so far. I've noticed TCPDF also has the addFont() function (documentation here) which seems more obvious to use as it does not include any reference to any font type. However, I was unable to get this function to work with the little documentation it has.

Any help would be greatly appreciated.

Seroczynski
  • 472
  • 1
  • 4
  • 14

7 Answers7

17

Are you sure that you are getting that error when calling addTTFfont()? The reason I ask is because I checked the TCPDF code (just did a global search on "Could not include font definition file") and that message only appears in the addFont() method.

These two methods can be a bit confusing, but I wrote myself some notes a few months ago, which I will partially reproduce below in the hope that it helps you somehow:

  • addTTFfont() - The primary function of this method is to convert a font from TTF (or OTF) to the "raw" version that TCPDF needs. The way this function is implemented you could, in theory, use it as your primary method of adding fonts to a document. It will check the tcpdf font folder first and if the converted files aren't there it will go ahead and do the conversion. It is only a little bit more overhead, but still not my preferred method of adding fonts to files, as you need to know what style of font you are converting for the process to even work successfully. IMO, it is better to use this method to pre-convert any fonts that you plan on using and simply use addFont() to add the "raw" versions to the document.

  • AddFont() - This adds a "raw" (ie. already converted) font to the document, which means it is then available for writing text.

  • SetFont() - This sets the font for the next chunk of text that you write.

So I would use addTTFfont() to pre-convert the font to the "raw" version, then use addFont() and setFont() in the code that actually creates the PDF.

If addFont() is failing with the error message above, it means it cannot find the font definition file. Keep in mind that if you call addFont() with style set ('i', 'b', 'bi', etc), all it does is append this to the file name (before the extension).

Most importantly, you need to make sure that your call to addTTFFont() is producing the "raw" font files and saving them into your fonts folder. There should be three files per style, with extensions of .php, .z and .ctg.z. So if you converted a font called blah.ttf you will end up with blah.php, blah.z and blah.ctg.z. If you convert blah bold.ttf, TCPDF will figure out that it is a bold font and append the 'b' to the end of the file names: blahb.php, blahb.z and blahb.ctg.z.

Hopefully there will be some nugget in here that will help! Good luck!

JamesG
  • 4,288
  • 2
  • 31
  • 36
  • Yes I am sure. I've tried to explain my situation to the best I could. When I check my server after the error I always get the same result; one `customfont.z` file but no `customfont.php` file. If I am to understand TCPDF correctly, the error I have described points towards the failed creation of the `customfont.php` (definition) file. – Seroczynski Oct 16 '13 at 09:26
  • That's right, you should end up with three files (.php, .z and .ctg.z). It is awfully strange that addTTFfont() would only create the .z file and not the other two. Is the function returning the name of the font (indicating success) or boolean false (indicating error)? – JamesG Oct 16 '13 at 20:48
  • Update: I just tried converting a TTF file that TCPDF didn't like and it created the .z file but neither of the other two files. The function returned FALSE. In my particular case it failed when checking the "snft version" at around line 365 in tcpdf_fonts.php (I'm on version 6.0.039, but I don't think this file has changed much recently). To be honest, although the author has done an incredible job with TCPDF, the font conversion aspect has been the bane of my life and so I've used alternative means of converting my fonts. – JamesG Oct 17 '13 at 04:44
  • Thanks for looking further into it JamesG. Can you tell me what alternative methods you have found to convert your font? We really want to use our font in the PDF files. – Seroczynski Oct 17 '13 at 17:34
  • 3
    To be honest, I've had the most success with this site: http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf You will have to convert from OTF to TTF first, but I think there are other sites that will handle that aspect fairly easily. The other issue that you might run into is that many (most? all?) OTF/TTF fonts have a two-byte encoding but PostScript fonts, which is what addTTFfont() and that web site both convert to, use a one-byte encoding, which means you only get the first 255 characters from the original font. Good luck! – JamesG Oct 17 '13 at 20:41
  • @JamesG Could you please add a working code snippet for a OCR-B font? I don't get it working (maybe a path or syntax problem). – PeterCo Jan 09 '18 at 09:28
  • I can't provide a working code snippet unless I know what type of font it is (eg. TTF, OTF, etc). Even then, all you really need to do is play around with the addTTFFont() function and see if it works. More information about converting fonts can be found here: https://tcpdf.org/docs/fonts/ – JamesG Jan 10 '18 at 23:44
9

TCPDF New Font add and turkish char problem solved method.

Use Converter Link:

  1. http://fonts.snm-portal.com
  2. Download 3 files,
  3. Copy TCPDF-master/fonts
  4. Useage: $pdf->SetFont('roboto', '', 14);

enter image description here

Limitless isa
  • 3,689
  • 36
  • 28
2

To solve this problem with generating font.

Make sure all path are correct.

Use this link

Remember to put folder make_font inside folder of html2pdf

Remember about correct path to: html2pdf.class.php inside folder make_font in index.php

or

If you have other problem - font: Could not include font definition file:

It's a problem of correct name of your new font.

For example using when generating pdf:

Correct: $html2pdf->addFont('lato i', '', 'latoi.php');

Incorrect: $html2pdf->addFont('lato', '', 'latoi.php');

Remember there are only 2 files generated:

latoi.php

latoi.z

You don't need any ctg file.

I hope it will help

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
Tom Sawin
  • 31
  • 3
0

In my case the problem was, that the fonts path wasn't writable. I fixed it by changing permissions of the folder: var_dump(K_PATH_FONTS);

Dion
  • 3,145
  • 20
  • 37
0

In my case, it was simply a wrong path to the (no more found) .ttf font file. For a test I used the drive D:/ successfully, but I forgot to change it back to the prod drive C:/ in my php file in the code line where I use the addTTFfont('C:/...') command.

Unfortunately, the TCPDF error message doesn't output anything about the source file and the code line producing the problem.

PeterCo
  • 910
  • 2
  • 20
  • 36
0

add the dejavusans.php file into the font folder into the tcpdf library into the codeingiter library folder.

0

"cid0jp" this font supports Japanese and Chinese.

Praveen Kumar
  • 67
  • 1
  • 5