php - DOMPDF 不工作,给出错误

标签 php dompdf

我有一个从 mysql 表中获取数据的 form.php 文件。 form.php

<?php
 if(mysql_num_rows($sql)==0){
?>
<table>
    <tr>
        <td>1.</td>
        <td>America</td>
    <tr>
</table>
<a href="pdf.php" target="_blank">Export as PDF</a>
<?php
 } else {
?>
<table>
    <tr>
        <td>1.</td>
        <td>India</td>
    <tr>
</table>
<a href="pdf.php" target="_blank">Export as PDF</a>
<?php
 }
?>

pdf.php

require_once("dompdf/dompdf_config.inc.php");
ob_start();
$html = file_get_contents('form.php');
$dompdf = new DOMPDF();
date_default_timezone_set('Asia/Kolkata');
$tym = date('g:i s');
$filename = 'FAA-8130_3_'.$tym;
$dompdf->load_html($html);
ob_end_flush();
$dompdf->render();
$dompdf->stream($filename. ".pdf", array("Attachment" => 0));

当我尝试将其导出为 PDF 文件时,出现错误...

Fatal error: Call to a member function prepend_child() on a non-object in C:\wamp\www\path\to\dompdf\include\frame_tree.cls.php on line 231

我不明白为什么会这样。我查看了this , thisthis , 但未能解决。

最佳答案

0.6.1 之前的 dompdf 将支持在呈现为 PDF 之前处理 PHP。出于安全原因,它已被删除,因此您现在应该在将文档提供给 dompdf 之前完全处理文档中的任何 PHP。

pdf.php 中的代码很接近,但不太正确。以下应该工作(我也重新组织以提高可读性)。

date_default_timezone_set('Asia/Kolkata');
require_once("dompdf/dompdf_config.inc.php");

$tym = date('g:i s');
$filename = 'FAA-8130_3_'.$tym;

ob_start();
require_once 'form.php';
$html = ob_get_clean();
ob_end_clean();

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($filename. ".pdf", array("Attachment" => 0));

关于php - DOMPDF 不工作,给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36357603/

相关文章:

php - 将论坛,如 bbpress,指向现有数据库

java - 我可以获取任何其他浏览器窗口的 URL 吗?

php - Dompdf 错误 : "The Row #4 could not be found" when converting PHP file to PDF

php - 库存超出限制,仍然插入订单表

php - Behat 和 Symfony 数据装置

php - DOMDocument::save[domdocument.save]:无法打开流:权限被拒绝

html - 内容上的页眉和页脚...我该如何解决这个问题?

laravel - barryvdh/laravel-dompdf :- fopen failed to open stream: No such file or directory in laravel 5. 7

php - dompdf 在文档末尾插入空白页

php - Laravel 5.0 - 从模板生成 PDF 并添加数据