[PHP] dompdf 使用记录
2024-09-03 01:38:09
# 安装字体,解决中文乱码
参考: 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代码中设置中文字体
<?php
require '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);
```
修改为
```php
preg_match_all('/./u', $text, $array);
$words = $array[0];
$wc = count($words);
```
最新文章
- Android Hook 借助Xposed
- C# 构造post参数一种看起来直观点的方法[转]
- ajax传递数组到后台
- C# 代码示例_结构/数组/枚举...
- 项目跑起来之后,一会儿后台就会报错Illegal access: this web application instance has been stopped already. Could not load [com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask]. The following stack trace
- ulipad 常用快捷键
- web旋转式
- [转载]传智播客_SQL入门
- 一张图讲解为什么需要自己搭建自己的git服务以及搭建的途径
- 有关XCode6(iOS8)UITableViewCell与iOS7在UITableViewCell问题
- java中try 与catch的使用
- vue中什么样的数据可以是在视图中显示
- Android必知必会-Handler可能引起的内存泄露
- Yii1操作phpexcel
- Hibernate学习(八)———— Hibernate检索策略(类级别,关联级别,批量检索)详解
- pandas-cheat-sheet
- folly无锁队列,尝试添加新的函数
- Spring boot项目集成Sharding Jdbc
- Long与long的区别
- 7. I/O复用