Tropicalista
Tropicalista

Reputation: 3137

Is possible to embed fontawesome font in fpdf?

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

Upvotes: 2

Views: 5780

Answers (3)

mr.baby123
mr.baby123

Reputation: 2312

You can re-create the font-awesome and re-map it to A-Z characters, and that way, use it easily in fpdf.

// Import and prepare font for usage by engine:
$pdf->AddFont('fontawesome-123-full','','fontawesome-123-full.php');
...
// print FontAwesome's asterisk
$pdf->Text($x, $y, chr(0x21));

You can download the ready font and ready-to-use FPDF font files here:

http://mdb-blog.blogspot.com/2021/12/using-fontawesome-in-php-fpdf.html

Upvotes: 0

dmdewey
dmdewey

Reputation: 199

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

Upvotes: 3

DroidOS
DroidOS

Reputation: 8890

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.

Upvotes: 3

Related Questions