dongyan3237
2013-12-04 07:20 浏览 55
已采纳

有可能在fpdf中嵌入fontawesome字体吗?

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: http://fpdf.fruit-lab.de/

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";
    
        makefont('fontawesome-webfont1.ttf','fa1.map');
        makefont('fontawesome-webfont2.ttf','fa2.map');
        makefont('fontawesome-webfont3.ttf','fa3.map');
    

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

        $pdf->AddFont('FA1','',fontawesome-webfont1.php);
        $pdf->AddFont('FA2','',fontawesome-webfont2.php);
        $pdf->AddFont('FA3','',fontawesome-webfont3.php);
    

    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 :

        $pdf->SetFont('FA1','',14);
    
        $WineGlass = chr(32);
    
        $pdf->Cell(40,10,$WineGlass);
    

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

        <?php
    
        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));
    
        $pdf->AddPage();
        $pdf->SetMargins(0,0,0);
        $pdf->SetAutoPageBreak(0,0);
        // add custom fonts
    
        $pdf->AddFont('H','','helvetica.php');
    
        $pdf->AddFont('FA','',$font);
    
        $pdf->SetFillColor(200,200,200);
    
        $pdf->SetXY(9,9);
    
        for ($i = 32; $i <= 256; $i++) {
    
            $y = $pdf->GetY();
            $x = $pdf->GetX();
    
            $pdf->SetX($x);
            $pdf->SetTextColor(0, 0, 0);
            $pdf->SetFont('FA','',14);
            $pdf->Cell(12,12,chr($i),1,0,'C');
    
            $pdf->SetXY($x,$y+12);
    
            $pdf->SetTextColor(0, 0, 0);
            $pdf->SetFont('H','',14);
            $pdf->Cell(12,12,$i,1,0,'C',1);
    
            $y = $pdf->GetY();
            $x = $pdf->GetX();
    
            $pdf->SetXY($x,$y-12);
    
            if ($x > 400) {
             $pdf->SetXY(9,$y+14);       
            }
    
            if ($i == 328){
                $pdf->AddPage();
            }
    
        }
    
        $pdf->Output("charmap.pdf",'I');
    
    点赞 评论 复制链接分享

相关推荐