php mPDF,无法设置字体系列和字体大小 [英] php mPDF, impossible to set font-family and font-size
问题描述
好了,花了很多时间阅读他们的文档,我决定在测试中向SO社区寻求帮助.
Ok after so much time spent reading their documentation, and my tests I decided to consult the SO community for a help.
我使用mPDF从HTML生成pdf.该库工作正常,生成了pdf等.
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.
我尝试的代码:
$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');
在使用mpdf媒体的样式表中,我有一堆处理所有html代码的样式.我还对字体系列和字体大小有一些规则,即:
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');
但是仍然没有.我在pdf中仍然没有sans-serif,而且尺寸很大.
But still nothing. I still have sans-serif in the pdf, and also in very big size.
有人可以阐明这一点吗?
Can someone shed some light on this?
推荐答案
您可以通过两种方式将字体添加到生成的pdf中:
There are two ways with which u can add your font to generated pdf:
1.将字体设置为mpdf
将字体保留在mpdf lib中的ttfonts文件夹中.然后,在config_fonts.php文件中,将您的字体添加到数组中:
Keep your fonts in ttfonts folder in mpdf lib. Then, in config_fonts.php file add your font in array :
$ this-> fontdata = array( "YourNewFont" =>数组( 'R'=>"YourNewFont.ttf"));
$this->fontdata = array( "YourNewFont" => array( 'R' => "YourNewFont.ttf"));
然后使用以下命令将字体设置为mpdf对象:
Then set font to the mpdf object using :
$ mpdf-> SetFont('YourNewFont');
$mpdf->SetFont('YourNewFont');
现在,您的堡垒已设置好.使用$ mpdf-> WriteText('My Text');将使用设置的字体将文本添加到pdf.
Now, your fornt is set. Using $mpdf->WriteText('My Text'); will add text to pdf with the set font.
2.在HTML中使用字体
使用 @ font-face {}
然后,按如下所示导入该css文件:
Then, import that css file like below:
$ stylesheet = file_get_contents('MyStyleWithFont.css');
$stylesheet = file_get_contents('MyStyleWithFont.css');
$ mpdf-> WriteHTML($ stylesheet,1);
$mpdf->WriteHTML($stylesheet,1);
现在,您可以将字体与html一起使用.为css文件中的文本或与html内联的文本定义字体系列.使用 $ mpdf-> WriteHTML(My Text); 打印html.
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.
这篇关于php mPDF,无法设置字体系列和字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!