How to embed fonts with FPDF and mPDF

落爺英雄遲暮 提交于 2019-12-21 05:12:29

问题


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.


回答1:


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);



回答2:


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




回答3:


Unfortunately this is way more difficult than it should be, albeit I got it working. The link below is a great reference, but I couldn't get fondu to install so I had to use TrueType on Windows, TransType Pro.

http://www.eiriks.net/2007/09/13/custom-font-with-fpdf-on-os-x-ttf2pt1-fondu/

I did just find this HTML to PDF generator and may look into that, it seems pretty easy to customize compared to FPDF. http://www.digitaljunkies.ca/dompdf/



来源:https://stackoverflow.com/questions/3530878/how-to-embed-fonts-with-fpdf-and-mpdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!