php 在 mPDF 生成的文档中使用字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15367101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
use font in mPDF generated document
提问by dennisbot
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:
我希望你能帮我解决这个问题,我第一次处理 mPDF,我认为它很棒,但我想用与我的网络相同类型的字体来显示报告,这在其文档中解释了如何来实现这一点,但它似乎仍然对我不起作用。到目前为止我所做的是:
- 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'>. 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' fontbody { font-family: 'Open Sans', Sans-Serif; } which displays the font as expected.
- 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.
- 将来自 google 的 Open Sans 的 url 添加到我的文档中,该文档将在它之后生成,
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>. 然后我添加一个样式表文件,其中包含
<link rel="stylesheet" href="<?php echo base_url(); ?>assets/publicidad/css/reporte.css" type="text/css">我已经添加的内部 reporte.css字体的使用 ,我有一个使用“Open Sans”字体的定义body { font-family: 'Open Sans', Sans-Serif; 按预期显示字体。
- 我用 mPDF 生成 pdf 效果很好,但是当我想
按照文档显示 Open Sans 时,它没有显示所需的字体,我从http://www.fontsquirrel.com下载了“Open Sans”的 ttfs 文件/fonts/open-sans,并添加到 mPDF 目录的 ttfonts 文件夹中,如其文档中所述。
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.
在它之后,我遵循这个清晰的文档http://mpdf1.com/manual/index.php?tid=453,但直到现在我没有在我的 PDF 文档中显示所需的字体,这是我迄今为止没有做的部分说“4.要使用特定语言的字体,您还需要编辑配置文件(config_cp.php);让我们想象一下 Frutiger 包含泰语所需的全套字符:”,但我不要认为这是问题所在,因为我使用的是我放下的默认配置。
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)
(以上内容未被 stackverflow 编辑器识别)
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.
PD:我把单引号放在我的 html 文档中是因为我是这样添加的,但我也尝试过不使用它们,但没有成功。我希望你能帮助我,在此先感谢。
回答by T.Todua
Notes
笔记
Firstly, dont use mpdf1.comwebsite, it's closed. Use https://github.com/mpdf/mpdf. There are different codes for Version 6 and 7
首先,不要使用mpdf1.com网站,它已关闭。使用https://github.com/mpdf/mpdf。版本 6 和 7有不同的代码
Example for mPDF v6.x
mPDF v6.x 的示例
(source)
(来源)
1) upload MyFont.ttfinto /mpdf/ttfonts
2) in /mpdf/config_fonts.php, inside $this->fontdataarray, add your:
1) 上传MyFont.ttf到2) 中,在 数组中,添加您的:/mpdf/ttfonts/mpdf/config_fonts.php$this->fontdata
"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_fontttin css:
3)然后,无论您在何处执行自定义脚本,都my_custom_fonttt在 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');
回答by dennisbot
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
我已经解决了我的问题,字体定义中不应该有空格,所以我在我的 css font-face 声明中用“opensans”替换了
$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
请注意字体类型('OpenSans-Regular.ttf')应该在 mpdf 文件夹的 ttfonts 文件夹中
回答by Vicky Parab
I was facing same issues: Make sure you don't have Spaces while defining font name, keep it lowercase.
我遇到了同样的问题:确保在定义字体名称时没有空格,保持小写。
it worked for me
它对我有用

