CodeIgniter中Dompdf循环生成多PDF文件失败及异常求助
CodeIgniter + Dompdf 批量生成PDF失败问题解决
在CodeIgniter中使用Dompdf循环为每个用户生成独立PDF文件时,仅能成功生成第一份,后续循环抛出Dompdf\Exception异常,提示"No block-level parent found. Not good."
尝试的代码
public function export_information(){ $this->load->library('pdf'); $export_info_sql = "SELECT * FROM offences_issued"; $export_info_query = $this->db->query($export_info_sql); $export_info_result = $export_info_query->result(); $query_html = ""; $counter = 1; foreach ($export_info_result as $offence) { $directory_path = FCPATH."offences/".$counter.".pdf"; $query_html .= "<table width=100% border=1>"; $query_html .= "<tr><th>Offence ID</th></tr>"; $query_html .= "<tr><td>".$offence->issued_id."</td></tr>"; $query_html .= "</table>"; $this->pdf->loadHtml($query_html); $this->pdf->setPaper('A4', 'portrait'); $this->pdf->render(); $output = $this->pdf->output(); if (!file_exists($directory_path)) { file_put_contents($directory_path, $output); } $counter++; } // echo $query_html; // exit; }
错误信息
An uncaught Exception was encountered Type: Dompdf\Exception Message: No block-level parent found. Not good. Filename: D:\xampp\htdocs\project101\application\libraries\dompdf\src\Positioner\Inline.php Line Number: 44 Backtrace: File: D:\xampp\htdocs\project101\application\libraries\dompdf\src\FrameDecorator\AbstractFrameDecorator.php Line: 873 Function: position File: D:\xampp\htdocs\project101\application\libraries\dompdf\src\FrameReflower\Inline.php Line: 51 Function: position File: D:\xampp\htdocs\project101\application\libraries\dompdf\src\FrameDecorator\AbstractFrameDecorator.php Line: 894 Function: reflow File: D:\xampp\project101\project101\application\libraries\dompdf\src\FrameReflower\Page.php Line: 141 Function: reflow File: D:\xampp\htdocs\project101\application\libraries\dompdf\src\FrameDecorator\AbstractFrameDecorator.php Line: 894 Function: reflow File: D:\xampp\htdocs\project101\application\libraries\dompdf\src\Dompdf.php Line: 842 Function: reflow File: D:\xampp\htdocs\project101\application\controllers\Portal.php Line: 45 Function: render File: D:\xampp\htdocs\project101\index.php Line: 315 Function: require_once
问题原因与修复方案
- HTML结构异常:
$query_html使用.=拼接,每次循环会累加之前的HTML内容,导致Dompdf解析重复嵌套的表格时出错,需在每次循环开始时重置$query_html。 - 实例状态污染:同一个Dompdf实例重复调用渲染方法会残留之前的状态,建议每次循环重新初始化实例。
- 文件生成逻辑限制:
!file_exists($directory_path)会跳过已存在的文件,若需要覆盖旧文件可移除该判断。
修正后的代码
public function export_information(){ $export_info_sql = "SELECT * FROM offences_issued"; $export_info_query = $this->db->query($export_info_sql); $export_info_result = $export_info_query->result(); $counter = 1; foreach ($export_info_result as $offence) { // 每次循环重置HTML内容,添加完整文档结构 $query_html = "<!DOCTYPE html><html><body>"; $directory_path = FCPATH."offences/".$counter.".pdf"; $query_html .= "<table width=100% border=1>"; $query_html .= "<tr><th>Offence ID</th></tr>"; $query_html .= "<tr><td>".$offence->issued_id."</td></tr>"; $query_html .= "</table></body></html>"; // 每次循环重新初始化Dompdf实例 $this->load->library('pdf'); $pdf = $this->pdf; $pdf->loadHtml($query_html); $pdf->setPaper('A4', 'portrait'); $pdf->render(); $output = $pdf->output(); // 直接生成文件,覆盖已存在的文件 file_put_contents($directory_path, $output); // 销毁实例避免内存占用 unset($pdf); $counter++; } }
额外优化建议
- 给HTML添加完整的文档结构(DOCTYPE、html、body标签),提升Dompdf解析稳定性。
- 若处理大量数据,可添加内存限制:
ini_set('memory_limit', '256M');,避免内存溢出。
内容的提问来源于stack exchange,提问作者Muddasir Abbas
相关产品推荐
相关产品推荐

