2013-12-04 07:20 浏览 55


I'd like to use fontawesome in pdf. I generate my pdf using php library fpdf and font embedding. However I cannot make it works.

I use this tool to generate afm file:

But when I try to use fontawesome I always get white square instead of icons.

I use this syntax to add icon:

MultiCell(0,8,"\uF000 ",0,'C')

enter image description here

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答 默认 最新

  • 已采纳
    duanqiang6501 duanqiang6501 2013-12-04 07:25

    I cannot answer for fpdf since I have never used it. However, I do use mPDF and there I use fontawesome regularly - no issues at all. The only thing I have to ensure is that the content I output to the PDF document (mPDF takes this in the form of HTML markup) hast to be UTF8 encoded.

    mPDF is very good so if you are at an early stage of your project you might just consider switching to it. Otherwise, it is worth exploring whether you too are not running into a UTF8 encoding issue.

    点赞 评论 复制链接分享
  • dongye3917 dongye3917 2015-07-17 22:54

    I figured this out even though this thread is over a year old. It is possible to use font-awesome although you can only use up to 256 characters per subset. I created several character maps to encompass all of the glyphs as of font awesome version 4.3.0. You just use the map that contains the characters you're going to use or you can make three subsets of it. It's not necessarily as performant as other solutions, but fPDF is still much faster than some of the alternatives because it is lacking a lot of the more modern features like unicode support.

    First, you need to use ttf2pt1 to create the afm file for the font.

        ttf2pt1 -a fontawesome-webfont.ttf fontawesome-webfont

    I made three copies of the webfont to run through makefont.php and used each encoding on the corresponding font.

        require "makefont.php";

    To use them in fPDF, put the files generated in the font folder and add them like this:


    Then you use the character number to render the glyph for the corresponding subset of font-awesome. The three font maps above contain the character numbers to use :

        $WineGlass = chr(32);

    I also made a character map generator that will show the character numbers below the glyphs.

        require_once($_SERVER['DOCUMENT_ROOT'] . '/pdf/fpdf.php');
        // Establish / Get variables
        function GETVAR($key, $default = null, $prefix = null, $suffix = null) {
            return isset($_GET[$key]) ? $prefix . $_GET[$key] . $suffix : $prefix . $default . $suffix;
        $font = GETVAR('font','fontawesome-webfont1','','.php');
        $pdf = new FPDF('L','mm',array(268.33,415.3));
        // add custom fonts
        for ($i = 32; $i <= 256; $i++) {
            $y = $pdf->GetY();
            $x = $pdf->GetX();
            $pdf->SetTextColor(0, 0, 0);
            $pdf->SetTextColor(0, 0, 0);
            $y = $pdf->GetY();
            $x = $pdf->GetX();
            if ($x > 400) {
            if ($i == 328){
    点赞 评论 复制链接分享