[PHP] dompdf 使用记录

# 安装字体,解决中文乱码参考: https://blog.51cto.com/lampzxr/1916038```首先下载composer            curl -sS https://getcomposer.org/installer | php下载dompdf包   php composer require dompdf/dompdf下载load_font.php,此文件的功能是安装中文字体         1.git clone  https://github.com/dompdf/utils.git         2. 复制 load_font.php到 dompdf目录中,与lib 和 src 目录同级。下载中文字体,推荐下载 Droid Sans Fallback 字体,也可用雅黑字体,【雅黑字体会致使导出文档过大】                 下载连接【http://www.17ziti.com/info/71250.html】 安装字体,将字体传到服务器目录下,运行load_font.php       php load_font.php 'Droid'  /data/DroidSansFallback.ttf。      运行后,若没报错,则在 vendor/dompdf/dompdf/lib/fonts/下生了                                    Droid.ttf,Droid.ufm    这两个文件。在PHP代码中设置中文字体<?phprequire 'vendor/autoload.php';use Dompdf\Dompdf; $dompdf = new Dompdf(); $html=<<<HTML <html><head></head><body><!-- font-family:yahei;  china--><div style="font-family:Droid; color: #f00;font-size: 14px"> 中文123 </div></body></html>HTML;$html = iconv('gb2312','utf-8',$html); $dompdf->loadHtml($html); $dompdf->setPaper('A4', 'landscape'); $dompdf->render(); $dompdf->stream();注意 CSS 样式中的 font-family 设置为 以前运行load_font.php中设置的字体名。```# 解决中文换行问题修改文件: dompdf/dompdf/src/FrameReflower/Text.php找到关键字: // split the text into words```php$words = preg_split('/([\s-]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE);$wc = count($words);```修改成```phppreg_match_all('/./u', $text, $array);$words = $array[0];$wc = count($words);```
相关文章
相关标签/搜索