Dompdf對中文的支持不夠友好,例如默認情況下中文段落不會自動換行。
另外,如果DOM文檔中存在遠程圖片路徑,也不能正常加載該圖片,生成的pdf中顯示 image not found or type unknown
錯誤。
解決以上兩個問題,需要修改Dompdf的源文件。
支持中文換行
修改dompdf庫根目錄下的 dompdf/src/FrameReflower/Text.php
文件第112行(此處爲dompdf 0.8版本,不同版本可能略有不同)。
//原代碼
// split the text into words
$words = preg_split('/([\s-]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
//修改爲
preg_match_all("/./u", $text, $array);
$words = array(0);
加載遠程圖片
修改dompdf庫根目錄下的 dompdf/src/Options.php
文件第182行(此處爲dompdf 0.8版本,不同版本可能略有不同)。
另外,DOM文檔中的圖片路徑爲絕對路徑。
//Enable remote file access
private $isRemoteEnabled = true; //默認爲false