3

I am generating PDF using mPDF PHP library. I am able to successfully generate it using the default font configuration. However, I want to render a Google font in my PDF. I tried using the steps mentioned in this link, but it did not work. Below is the code I use.

$mPDFO = new mPDF('utf-8', 'A4', 0, '', 10, 10, 10, 0, 0, 0, 'L');

Can anyone help me using Google Font in mPDF ?

Tojo Chacko
  • 1,230
  • 1
  • 13
  • 25
  • I think you have to combine the steps in the question you provided and this one: https://stackoverflow.com/questions/44739201/google-webfonts-in-pdf-generated-by-dompdf Download the fonts, convert them then include them. :-) – Edwin Nov 14 '17 at 14:41

1 Answers1

10

As mentioned in mPDF's documentation, it's not possible to use remote fonts directly by referencing them in the HTML. Follow the below steps to use custom fonts:

  1. Download the fonts & upload them to mPDF's fonts directory /ttfonts

  2. Declare the font-family you need to use in config_fonts.php under: $this->fontdata

  3. Now comes the main part. You need to mention the font-family in the instance that you create to call mPDF's object which is what you missed, like so:

    $mPDFO = new mPDF('utf-8', 'A4', 0, 'Source Sans Pro', 10, 10, 10, 0, 0, 0, 'L');

  4. Finally call the SetFont method like $mPDFO->SetFont('Source Sans Pro'); just below your instance

Community
  • 1
  • 1
nimsrules
  • 2,026
  • 20
  • 22
  • 2
    Mind you, this works with any custom font that comes with a `.ttf` file. Doesn't have to be Google Font only. – nimsrules Nov 15 '17 at 06:21