CodeIgniter中使用MPDF转换HTML为PDF时Glyphicons不显示问题
我之前也碰到过MPDF不加载Glyphicons的问题,折腾了好几种方法才搞定,给你分享几个靠谱的解决方案:
解决MPDF中Glyphicons图标不显示的问题
确保字体文件被正确嵌入
Glyphicons依赖的字体文件(比如glyphicons-halflings-regular.eot、glyphicons-halflings-regular.ttf、glyphicons-halflings-regular.woff)需要让MPDF能访问到。你可以把这些字体文件放到MPDF的ttfonts目录下,或者在初始化MPDF时手动指定字体路径:$mpdf = new \Mpdf\Mpdf([ 'fontDir' => __DIR__ . '/path/to/your/fonts/', 'fontdata' => [ 'glyphicons' => [ 'R' => 'glyphicons-halflings-regular.ttf', 'useOTL' => 0xFF, ] ] ]);同时要保证CSS里的字体声明准确无误:
@font-face { font-family: 'Glyphicons Halflings'; src: url('glyphicons-halflings-regular.ttf') format('truetype'); font-weight: normal; font-style: normal; } .glyphicon { font-family: 'Glyphicons Halflings'; font-style: normal; font-weight: normal; }替换为Base64编码的字体(最稳妥的方法)
如果总是遇到路径访问问题,直接把字体文件转换成Base64编码写进CSS里,这样MPDF肯定能读取到:@font-face { font-family: 'Glyphicons Halflings'; src: url(data:application/font-ttf;charset=utf-8;base64,[替换为你的字体Base64编码内容]) format('truetype'); font-weight: normal; font-style: normal; }你可以用在线工具把TTF文件转成Base64,直接粘贴编码内容就行。
检查CSS伪元素与兼容性
Glyphicons大多是通过::before伪元素生成的,要确保MPDF能正确解析伪元素的内容编码:.glyphicon-search::before { content: "\e003"; /* 这里要对应Glyphicons图标的正确Unicode编码 */ }另外,MPDF对部分CSS属性支持有限,避免给
.glyphicon类添加MPDF不兼容的样式,也不要让其他CSS规则覆盖了图标字体的声明。临时禁用字体子集化(用于排查问题)
有时候MPDF的字体子集化会漏掉Glyphicons的特殊字符,你可以先关闭子集化测试:$mpdf->setAutoFont(false); $mpdf->allow_charset_conversion = false;如果图标能显示,再调整子集化设置,确保包含Glyphicons的字符范围。
内容的提问来源于stack exchange,提问作者Arya ghosh
相关产品推荐
相关产品推荐

