13Sep07

Custom font with FPDF on OS X – ttf2pt1, fondu

Creating a PDF with a custom font through PHP and the nice FPDF-library is a straight-forward task – if you have the font available as TrueType. If you – like me – are working on a Mac, it’s more likely that your fonts are in any format but TrueType, so you need to convert them first, and generate the font metrics description file. That’s where my problems began.

FPDF comes with a tool (makefont.php) to create the necessary files and maps based on a TrueType-font, so I just needed something to help me convert my fonts to TrueType. My fonts are almost all Postscript Type 1 (LWFN), so I found fondu to be an appropriate tool. To generate the font metrics description file (.afm), ttf2pt1 was suggested as the best option.

So, the strategy is (assuming you already have FPDF (I used version 1.53) ready to use on your webserver):
1. Use fondu to convert ScalaSanReg (LWFN font file that resides in /Users/eirik/Desktop) to TrueType (using the proper encoding, in my case latin1 (ISO-8859-1)).
2. Realize that you need DarwinPorts to easily install ttf2pt1, download and install DarwinPorts.
3. Use ttf2pt1 to generate the font metrics description file.
4. Use the makefont.php-script that comes with FPDF to generate the font definition file that FPDF needs, as well as a compressed font file.
5. Declare and use the font in a PHP-script that creates a dynamic PDF with FPDF.

Here’s a description of the solutions that worked for me:
1. Downloading and installing fondu was easy, just download and run the package. Then open a terminal window and run:

cd /Users/eirik/Desktop <-- the location of your font
fondu -latin1 ScalaSanSpaReg

This worked first time, and resulted in a new TrueType file called ScalaSans-Regular.pfb.

2. Now to create the font metrics description file (.afm), we need to install ttf2pt1. The easiest way to do this is to use DarwinPorts. There is only one hitch. You need Xcode installed first. I ran into all kinds of trouble because I didn’t have Xcode installed, so make sure it’s installed. If it isn’t, insert “Mac OS X Install Disc 1″ that came with your computer, and run /Xcode Tools/XcodeTools.mpkg (valid for 10.4, not sure if this is correct for older versions). This will install what you need. Now that you have Xcode, installing DarwinPorts should work like a charm, just download the package and run it. After completing the install, you need to run an update to make sure you have the current ports in your list. In your terminal window:

sudo port -d selfupdate

If you get a “command not found” error, you probably don’t have the location of port in your PATH. On my computer, port was installed in /opt/local/bin, so I had to run this in a terminal window:

export PATH="/opt/local/bin:$PATH"

After doing this you have to close your terminal window and open a new one to use the updated PATH, and you should be able to run the update successfully:

sudo port -d selfupdate

I had to do this several times because it seemed that rsync was timing out occasionally, so keep running the command until you see something like “Congratulations, you have successfully installed the MacPorts system.”.

3. We can now install ttf2pt1 (note that the path I first change to in the terminal commands may be different on your computer, run a spotlight-search for ttf2pt1 to find the correct location):

cd /opt/local/var/macports/sources/
          rsync.macports.org/release/ports/print/ttf2pt1
sudo port install ttf2pt1

This will check for dependencies and install everything you need to run ttf2pt1 (zlib and freetype). ttf2pt1 will be installed in the same location as port, so if you updated your PATH previously, you should now be able to generate the font metrics description file (.afm) with this command:

cd /Users/eirik/Desktop <-- the location of your ttf-file
ttf2pt1 -a -l latin1 ScalaSans-Regular.pfb scalasans

This resulted in two files: ScalaSans-Regular.afm, and ScalaSans-Regular.t1a, but we only need the .afm-file.

4. We now have the two files needed to use makefont.php to generate the font definition file and compressed font:
- ScalaSans-Regular.pfb (the TrueType font file)
- ScalaSans-Regular.afm (the font metrics description file)

Now, copy both files into the “fpdf/tutorial”-folder that also contains a file called makefont.php. Edit makefont.php, it should look something like this (again, I’m using the latin1-encoding, this may be different for you – the default for the script is cp1252):

require('../font/makefont/makefont.php');
MakeFont('ScalaSans-Regular.pfb',
          'ScalaSans-Regular.afm','ISO-8859-1');

Running the script from your browser (e.g. http://localhost/fpdf/tutorial/makefont.php) will result in two files inside the “tutorial”-folder:
- ScalaSans-Regular.php (the font definition file)
- ScalaSans-Regular.z (the compressed font)
Copy these files to the “fpdf/font”-folder (if you can’t find ScalaSans-Regular.z, use ScalaSans-Regular.pfb instead), and you’re ready to use the font in your script.

5. Here’s an example of how you can use a custom font with FPDF (assuming this new file is located in the fpdf-folder, at the same level as fpdf.php):

require('fpdf.php');

$pdf = new FPDF('P','mm','a4');
$pdf->AddFont('ScalaSans-Regular', '',
          'ScalaSans-Regular.php');
$pdf->AddPage();
$pdf->SetFont('ScalaSans-Regular', '', 12);
$pdf->SetXY(60, 60);
$pdf->MultiCell(120, 5,
          'This is a test with ScalaSans-Regular');
$pdf->Output('custom_font_test.pdf', 'D');

I hope this will some day be of help to someone, otherwise I mainly regard this as a checklist for myself if I ever re-install the OS on this computer…

4 Responses to “Custom font with FPDF on OS X – ttf2pt1, fondu”


  1. 1 Eirik Posted September 13th, 2007 - 01:39

    Hmh, some of those terminal-commands were looong.. Oh well.. :)

  2. 2 tsu Posted January 22nd, 2008 - 05:49

    Was looking for instructions on how to use custom fonts with FPDF and stumbled upon this. This is also helpful for Windows users (ttf2pt1 comes with gnuwin32), kudos for sharing.

  3. 3 Eirik Posted January 22nd, 2008 - 06:26

    Happy to see that my trial and errors could be beneficial for someone else. I spotted an error in the last script on the page, updated now.

  4. 4 Michael Posted July 15th, 2008 - 06:17

    Yes ist helped a lot. But here ist another hint for error massages like

    returned error 127
    Command output: sh: make: command not found

    while trying to install ttf2pt1 via macports.

    You have to install install XCode which brings “make” with it.

    I got this Hint via http://justindriscoll.us/2008/02/installing-macpports-160-from-package.html

Leave a Reply


Comment guidelines: No spamming, no profanity, and no flaming. Inappropriate comments will be deleted outright.




Archives

Categories

Recent Comments