8

I would like you to help me with this issue, I'm dealing for the first time with mPDF which I think it's great, but I want to display the report with the same type of font as my web, which on its documentations explains how to achieve this, but it doesn't still seems to work for me. What I did till now is:

  1. Add the url to Open Sans from google to my document which will be generated after it, <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>.
  2. then I add a style sheet file that contains the use of the font <link rel="stylesheet" href="<?php echo base_url(); ?>assets/publicidad/css/reporte.css" type="text/css"> Inside reporte.css that I've already added, I have a definition to use 'Open Sans' font

    body { font-family: 'Open Sans', Sans-Serif; } which displays the font as expected.

  3. I Generate the pdf with mPDF which works well but when I want to
    display the Open Sans as the documentation stands, it's not showing the desired font, I downloaded the ttfs file of 'Open Sans' from http://www.fontsquirrel.com/fonts/open-sans, and added inside ttfonts folder of mPDF directory as it says within its documentation.


After it I follow this clear documentation http://mpdf1.com/manual/index.php?tid=453, but till now I'm not getting the desired font displayed in my PDF document what I didn't do till now its the part that says "4. To use the font with specific languages, you need also to edit the configuration file (config_cp.php); let us imagine that Frutiger contains a full set of characters needed for the Thai language:", but I don't think that's the problem because I'm using a default configuration which I put down.

function pdf_create($html, $filename, $stream = TRUE) {
            require_once(APPPATH . 'helpers/mpdf/mpdf.php');
            $mpdf = new mPDF();
            $mpdf->SetAutoFont();
            $mpdf->WriteHTML($html);
            if ($stream)
            {
                $mpdf->Output($filename . '.pdf', 'D');
            }
            else
            {
                $mpdf->Output('./uploads/temp/' . $filename . '.pdf', 'F');

                return './uploads/temp/' . $filename . '.pdf';
            }
        }

and in my controller I do this.

$html = $this->load->view('publicidad/reporte_x_curso', $data, true);
$this->load->helper('mpdf');
pdf_create($html, 'Reporte_por_cursos', true);

(the above isn't being recognized by the stackverflow editor)

And finally what I did till now that should do all I want following the documentation is:

$this->fontdata = array(
    "'Open Sans'" => array(
    'R' => 'OpenSans-Regular.ttf'
    )

PD: I put the single quotes because I was adding that way in my html document, but I also tried without them, without success. I hope you can help me, thanks in advance.

BenRoob
  • 1,662
  • 5
  • 22
  • 24
dennisbot
  • 950
  • 1
  • 11
  • 22

4 Answers4

15

Notes

Firstly, dont use mpdf1.com website, it's closed. Use https://github.com/mpdf/mpdf. There are different codes for Version 6 and 7

Example for mPDF v6.x

(source)

1) upload MyFont.ttf into /mpdf/ttfonts
2) in /mpdf/config_fonts.php , inside $this->fontdata array, add your:

"my_custom_font" => array( 
        'R' => 'MyFont.ttf',            // Regular - REQUIRED
        'I' => "MyFont-Oblique.ttf",    // Italic  - OPTIONAL
        'B' => "MyFont-Bold.ttf",       // Bold    - OPTIONAL
),


3) then, wherever you execute your custom script, use my_custom_fonttt in css:

<?php

$mpdf=new mPDF();
$texttt= '
    <html><head><style>
    body { font-family: "my_custom_font"; }
    </style></head>
    <body>My SAMPLE TEXTTTTT</body>
    </html>';
$mpdf->WriteHTML("$texttt");
$mpdf->Output(dirname(__FILE__).'/new_created_file.pdf','F');
Finwe
  • 6,372
  • 2
  • 29
  • 44
T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • 5
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Howdy_McGee Jul 14 '14 at 16:07
  • 4
    Which is exactly what has happened: The requested URL /manual/index.php was not found on this server. 31/03/2016 good call @Howdy_McGee and thank you tazo for updating your answer back in 2014 – Edward Mar 31 '16 at 09:05
  • I couldn't find the option mentioned but just in case it helps someone I found new documentation which is here: https://mpdf.github.io/. If I could find the relevant info I would copy and paste but I can't find it, probably the other answer is better for readers. – Eoin May 22 '17 at 12:44
12

I've solved my problem, there should be no spaces in the definition of the font, so I replaced in my css font-face declaration with "opensans" and

$this->fontdata = array(
    "opensans" => array(
    'R' => 'OpenSans-Regular.ttf'
    )

be aware that the font type ('OpenSans-Regular.ttf') should be inside ttfonts folder of mpdf folder

dennisbot
  • 950
  • 1
  • 11
  • 22
1

I was facing same issues: Make sure you don't have Spaces while defining font name, keep it lowercase.

it worked for me

Vicky Parab
  • 121
  • 2
  • 13
0

Aaargh - the font php files changed between FPDF versions. Make sure you use the correct ones.

anoldermark
  • 366
  • 3
  • 8