Technical discuss

解决Dompdf中文换行及无法加载图片的问题

  • btt
  • [btt]
  • Topic Author
  • Offline
  • Administrator
  • Administrator
More
21 Feb 2021 13:04 - 21 Feb 2021 13:09 #168 by btt
New Topic
Dompdf对中文的支持不够友好,例如默认情况下中文段落不会自动换行。
另外,如果DOM文档中存在远程图片路径,也不能正常加载该图片,生成的pdf中显示 
Code:
image not found or type unknown
 错误。
解决以上两个问题,需要修改Dompdf的源文件。

1.支持中文换行
修改dompdf库根目录下的 
Code:
dompdf/src/FrameReflower/Text.php
 文件第112行(此处为dompdf 0.8版本,不同版本可能略有不同)。
原代码
Code:
// split the text into words $words = preg_split('/([\s-]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE);

修改为
Code:
preg_match_all("/./u", $text, $array); $words = array(0);

2.加载远程图片
修改dompdf库根目录下的 
Code:
dompdf/src/Options.php
 文件第182行(此处为dompdf 0.8版本,不同版本可能略有不同)。
另外,DOM文档中的图片路径为绝对路径。
Code:
//Enable remote file access private $isRemoteEnabled = true; //默认为false
Last edit: 21 Feb 2021 13:09 by btt.

Please Log in or Create an account to join the conversation.