Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed fonts with FPDF and mPDF

Tags:

fonts

embed

fpdf

I'm trying to embed Helvetica Neue into my PDF, but all of the resources I find online are overly complicated and filled with what I think is unneeded information.

Can anyone point me to a step by step tutorial of how to include fonts in FPDF?

Thanks in advance.

like image 586
Carson Avatar asked Aug 20 '10 12:08

Carson


Video Answer


2 Answers

It's not too hard.

Suppose we want to add the Rockford font.

First, we need to use ttf2pt1 (or some equivalent program) to generate the AFM file for Rockford. Run the following command in a shell.

ttf2pt1 Rockford

The command will create Rockford.afm in the current shell directory.

Change your shell to the the makefont directory in the fpdf installation directory

cd /<...>/fpdf/font/makefont

Execute an interactive php shell

php -a

Run the following commands in the interactive PHP shell.

php > require("makefont.php");
php > MakeFont("/<font location>/Rockford.ttf", "/<font location>/Rockford.afm");
php > exit

You should see two new files, Rockford.php and Rockford.z in the current directory. Copy these two files to the 'fonts' directory in the fpdf installation directory.

cp Rockford.z Rockford.php /<...>/fpdf/font/

At this point, the installation of the new font into FPDF is done.

To use your font when generating a PDF, you must first import the font as follows.

$fpdf->AddFont('Rockford', '', 'Rockford.php');

You can then use the font when needed in your script. For example, to set the font to Rockford size 8, you would write the following statement.

$fpdf->SetFont('Rockford', '', 8);
like image 151
Adam Prax Avatar answered Sep 21 '22 07:09

Adam Prax


Bringing this back up as there's a pretty quick and easy converted online now - fPDF Font File Converter

like image 28
TeckniX Avatar answered Sep 18 '22 07:09

TeckniX