Is possible to embed fontawesome font in fpdf?

只谈情不闲聊 提交于 2019-12-02 05:55:50

问题


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


回答1:


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.




回答2:


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.

  • http://code.deweyartdesign.com/fpdf/makefont/fa1.map
  • http://code.deweyartdesign.com/fpdf/makefont/fa2.map
  • http://code.deweyartdesign.com/fpdf/makefont/fa3.map

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


来源:https://stackoverflow.com/questions/20369388/is-possible-to-embed-fontawesome-font-in-fpdf

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