Question

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.

Was it helpful?

Solution 2

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

OTHER TIPS

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

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

it worked for me

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top