php mPDF, impossible to set font-family and font-size
Solution 1
There are two ways with which u can add your font to generated pdf:
1. Setting font to mpdf
Keep your fonts in ttfonts folder in mpdf lib. Then, in config_fonts.php file add your font in array :
$this->fontdata = array( "YourNewFont" => array( 'R' => "YourNewFont.ttf"));
Then set font to the mpdf object using :
$mpdf->SetFont('YourNewFont');
Now, your fornt is set. Using $mpdf->WriteText('My Text'); will add text to pdf with the set font.
2. Use font in Html
Add your font family in a css file using @font-face {}
Then, import that css file like below:
$stylesheet = file_get_contents('MyStyleWithFont.css');
$mpdf->WriteHTML($stylesheet,1);
Now, you can use your font with html. Define font-family for the text in either css file or inline with html. Use $mpdf->WriteHTML(My Text); to print html.
Solution 2
I know i'm late, hope it would be helpful for someone in future. While creating mPDF object set the mod parameter as 'UTF-8' or ' ' instead of 'c'. 'c' will force system to use core fonts only and thus neglect the custom font.
$mpdf = new mPDF('UTF-8','A4')
Note: this is applicable for mPDF v6 and below
Zota
A web developer from a sunny part of south-east Europe. Want to consider myself proficient in a number of major web technologies, and earn my living staring on my dual 24' screens for most of the day :)
Updated on June 04, 2022Comments
-
Zota almost 2 years
Ok after so much time spent reading their documentation, and my tests I decided to consult the SO community for a help.
I use mPDF to generate pdf from HTML. The library works fine, pdf is produced etc.
But I wasn't able to set the font-family and font-size as I want it.
Code I tried:
$mpdf = new mPDF('c', 'A4'); $mpdf->CSSselectMedia='mpdf'; $mpdf->setAutoTopMargin = 'stretch'; $mpdf->setAutoBottomMargin = 'stretch'; $mpdf->setBasePath($url); $mpdf->SetHeader($url); $mpdf->WriteHTML($html); $mpdf->SetFooter($url); $mpdf->Output($filename.'.pdf', 'I');
In my stylesheet with mpdf media, I have bunch of styles that are dealing with all of the html code. And I also have some rules for the font-family and the font-size ie:
body{ border: none; font-size: 10pt; colosr:#000; font-family: Times, Georgia, "Times New Roman", serif !important; }
But the font settings are not apllied at all.
I also tried the following params on instantiation:
$mpdf = new mPDF('UTF-8', 'A4',9, 'dejavuserif');
But still nothing. I still have sans-serif in the pdf, and also in very big size.
Can someone shed some light on this?
-
realtebo about 3 yearshow to set font size ?