Search code examples
phpzend-frameworkfont-awesomempdf

How to use Font Awesome with MPDF?


I am using Zend Framework and creating PDF with mpdf.
I am trying to use fontawesome for denoting some of the articles but the fonts of font awesome are not rendering properly below is the code .

$stylesheet =  file_get_contents("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css");
$stylesheet .= file_get_contents("https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css");

$this->mpdf->WriteHTML($stylesheet,1);
$this->mpdf->WriteHTML($html,2);
$this->mpdf->allow_charset_conversion = true; 
$this->mpdf->charset_in = 'windows-1252';
$this->mpdf->Output();

The code I am using in the html

<span class="company-name">&#xf21b; name of the company</span>

Thanks in advance.


Solution

  • EDIT: Nowadays, this is an obsolete method, and should only be used on legacy systems.

    I recommend the answer below, from @Arie, to modify the mPDF font in its recent versions and/or if it was installed by Composer, passing the parameters when instantiating the class.


    The easy way:

    1. Copy fontawesome-webfont.ttf file into /mPDF/ttfonts/ directory.

    2. Edit /mPDF/config_fonts.php, search the array started by $this->fontdata and add to it:

    "fontawesome" => array(
        'R' => "fontawesome-webfont.ttf",
    ),
    
    1. Change your document CSS properly, with your new font family. Eg.:
    $stylesheet = '.company-name { font-family: fontawesome; }';
    
    1. When you instantiate the class, let the first parameter in blank:

    Eg.:

    $mpdf = new mPDF();
    

    or

    $mpdf = new mPDF('', 'A4');
    

    I am tested with mPDF 6.0 and it worked.

    Also, the mPDF manual explain how to do it with more options: Fonts in mPDF 6.x