Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Details for using fonts / Unicode fonts #14

Open
hagenbuch opened this issue Nov 17, 2023 · 1 comment
Open

Details for using fonts / Unicode fonts #14

hagenbuch opened this issue Nov 17, 2023 · 1 comment

Comments

@hagenbuch
Copy link

hagenbuch commented Nov 17, 2023

As it took me quite a while to figure this out and the documentation seems to be gone, I'll try to summarize what I found:

tFPDF can use "built in" fonts, that is (to my understanding) whereever a PDF is going to be printed, there will be those fonts at minimum:

Courier, Helvetica (= Arial), Times, Symbol, Zapf Dingbats - including their bold and italics versions.

If you want other fonts, I would get them from Google Werbfont helper but you're fine with other fonts as long it's True Type - and remember that your particular glyphs (cyrillic, greek or the like) are included inside the TTF file if you want to print them!

You will have to place those font files as a True Type Font (.ttf) and only using small caps, no spaces in the filename inside a certain font folder. Usually, you need an extra file for bold and another one for italic letters. Where should that folder be placed?

If you install tFPDF via composer (as we should)

composer require setasign/tfpdf

or even otherwise, a font folder

vendor/setasign/fpdf/font

will be created, containing a subfolder "unifont"

However, for security reasons (vendor folder with PHP code not being directly reachable via a browser request) you should better be placing the "vendor" folder next to the "public" folder containing the web root - but that has nothing to do with tFPDF.

To put your own fonts inside the file tree "vendor" generated by composer is not such a great idea because if you delete that "vendor" folder without much thought in order to recreate it, your fonts will be gone.

Better set a custom font path in the PHP code, like

define('FPDF_FONTPATH', '/www/htdocs/mydomain/font/');

Then copy all the files from abovementioned default "font" folder here, then add your ttf file in the "unifont" folder inside "font", for example source-sans-3-v15-latin_latin-ext-regular.ttf

The "unifont" folder must contain the newest code file "ttfonts.php" from the software package, so remember to update that if composer updates the tftpd package!

My PDF generation code usually starts like this:

$pdf = new tFPDF($orientation = 'P', $unit = 'mm', $format = 'A4');
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->AddFont('SourceSans', '', 'source-sans-3-v15-latin_latin-ext-regular.ttf', true);
$pdf->AddFont('SourceSansB', 'B', 'source-sans-3-v15-latin_latin-ext-700.ttf', true);
$pdf->SetDisplayMode('real');

When tFPDF tries to access your TTF font for the first time, it has to calculate some metrics and produce special font files ending with .mtx.php , .cw.dat and .cw127.php - therefor the webserver must be able to write into that folder!

Also if you install a new version of a ttf file, delete the corresponding old files .mtx.php, .cw.dat and .cw127.php so they may get regenerated.

@JanSlabon
Copy link
Member

Yep, everything's correct with this and as already discussed in #5 this should be addressed in the official package which is hosted on fpdf.org.

This repository is only made for cloning official tFPDF releases which are available at: http://fpdf.org/en/script/script92.php THERE WILL BE NO DEVELOPMENT IN THIS REPOSITORY!

IMHO the change in FPDF 1.86 should be added to tFPDF as well:

v1.86 (2023-06-25)
- Added a parameter to AddFont() to specify the directory where to load the font definition file.

IIRC there's also an issue in the temp-files because there are absolute paths in them, which makes it impossible to deploy them at all. A checksum of the original TTF could also help to keep the temp-files up to date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants