You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter3中Dompdf生成PDF在Adobe Acrobat显示空白求助

问题:Dompdf生成的PDF在Adobe Acrobat中显示空白,其他阅读器正常

我在CodeIgniter 3中使用Dompdf将HTML转换为PDF,下载后浏览器内置PDF阅读器可正常显示,但在Adobe Acrobat中打开仅显示空白页面,其他阅读器则能正常打开。

实现代码

$html = $this->load->view('viewhtml', $data, TRUE);
//echo $html;
$dompdf = new Dompdf();
$dompdf->loadHtml($html);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');

// Render the HTML as PDF
$dompdf->render();
$dompdf->getCanvas()->get_cpdf()->setEncryption("1234");

// Output the generated PDF to Browser
$dompdf->stream("name.pdf", array("Attachment" => false));

exit(0);

补充信息

  • 浏览器内置阅读器显示正常
  • Adobe Acrobat打开显示空白
  • 其他第三方PDF阅读器显示正常
  • 测试PDF文件(密码:1234)

排查方向及解决办法

  1. 加密方式兼容性问题
    直接调用底层CPDF的setEncryption方法,可能和Adobe Acrobat的加密标准不兼容。建议改用Dompdf官方推荐的加密方式(适用于0.8.x及以上版本):
// 替换原加密代码
$options = new Options();
$dompdf = new Dompdf($options);
// 渲染完成后设置加密
$dompdf->set_option('password', '1234');

也可以先移除加密代码,测试无加密PDF是否能在Adobe中正常显示,排除加密因素。

  1. HTML/CSS兼容性问题
    Adobe对PDF解析更严格,可能是HTML/CSS中的特殊属性导致异常:
  • 检查是否使用position: fixed/absolute等定位属性,旧版Dompdf处理这类属性时易出现兼容问题
  • 排查未闭合的HTML标签、非标准HTML结构
  • 简化HTML内容,逐步添加元素定位问题根源
  1. Dompdf版本问题
    旧版Dompdf存在PDF生成兼容bug,建议升级到最新稳定版(0.8.x及以上),新版本修复了大量与Adobe阅读器的兼容问题。

  2. 输出头信息问题
    确保HTTP头信息正确,避免内容截断:

// 在stream前添加头信息
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="name.pdf"');
$dompdf->stream("name.pdf", array("Attachment" => false));

内容的提问来源于stack exchange,提问作者namarahasia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 03:50:14