Use font in mPDF generated document

I would like you to help me with this problem, this is my first encounter with mPDF, which I think is wonderful, but I want to display a report with the same font type as my website, which is explained in its documents how to achieve this, but it still does not work for me. So far I have been doing the following:

  • Add the Open Sans URL from Google to my document after it <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'> .
  • then I add a stylesheet file containing the font <link rel="stylesheet" href="<?php echo base_url(); ?>assets/publicidad/css/reporte.css" type="text/css"> Inside reporte .css, which I already added, I have a definition to use the Open Sans font

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

  • I am creating pdf with mPDF that works well, but when I want to use Open Sans as documentation stands without displaying the font I need, I downloaded the ttfs file from "Open Sans" from http://www.fontsquirrel.com/fonts/ open-sans , and added to the ttfonts folder of the mPDF directory, as he says in his documentation.


After that, I follow this clear documentation http://mpdf1.com/manual/index.php?tid=453 , but still I donโ€™t get the desired font displayed in my PDF document that I didnโ€™t still have it the part that says: "4. To use the font with certain languages, you also need to edit the configuration file (config_cp.php), imagine that Frutiger contains the full set of characters needed for the Thai language:" but I donโ€™t think that the problem is because I am using the default configuration that I suppressed.

 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 is not recognized by the stackoverflow editor)

And finally, what I have done so far is that I should do everything that I want, following the documentation:

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

PD: I put single quotes because I added this path to my html document, but I also tried without them, without success. Hope you can help me, thanks in advance.

+6
source share
3 answers

I solved my problem: there should be no spaces in the font definition, so I replaced the font-face css "opensans" in my ad and

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

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

+12
source

Notes

Firstly, do not use the site mpdf1.com , it is closed. Use https://github.com/mpdf/mpdf . There are different codes for versions 6 and 7

Example for mPDF v6.x

( source )

1) download MyFont.ttf to /mpdf/ttfonts
2) in /mpdf/config_fonts.php , inside the $this->fontdata array, add yours:

 "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 run your own 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'); 
+14
source

I ran into the same problems: Make sure you have no spaces when specifying the font name, keep it lowercase.

it worked for me

+1
source

All Articles