php DOMPDF,我无法同时创建两个PDF

kfgdxczn  于 2023-06-28  发布在  PHP
关注(0)|答案(2)|浏览(152)

当我尝试一次创建两个pdf时,它会抛出错误。。
致命错误:未捕获的异常“DOMPDF_Exception”,消息为“未找到块级父级”。Copyright © 2016 www.cnjs.com All Rights Reserved.粤ICP备16048888号-1 DOMPDF_Exception:未找到块级父级。Copyright © 2018 www.cnjs.com All Rights Reserved.粤ICP备15044888号-1
代码如下:

$this->load->library('pdf');
                    $this->pdf->set_base_path($data['path']);
                    $this->pdf->load_view('adm/invoice/si2i',$data);
                    $this->pdf->render();
                    $output = $this->pdf->output();
                    file_put_contents("uploads/invoice/invoice_".$invoice_file_name.".pdf", $output);

$this->load->library('pdf');
                    $this->pdf->set_base_path($data['path']);
                    $this->pdf->load_view('adm/invoice/si2i',$data);
                    $this->pdf->render();
                    $output = $this->pdf->output();
                    file_put_contents("uploads/invoice/invoice_".$invoice_file_name.".pdf", $output);

请帮帮我。
感谢您的评分

kgsdhlau

kgsdhlau1#

我也遇到了同样的问题。解决方案是codeigniter pdf库$this->load->library('pdf');创建一个DOMPDF示例,每次都调用它,但是这个类不能正确地清理它自己,所以如果你需要生成多个pdf,它会崩溃。
解决方案是根据需要手动示例化DOMPDF类。不要使用codeigniter pdf Package 器。

//require at the top of our script
require_once(APPPATH .'libraries/dompdf/dompdf_config.inc.php');

//get the html first (base dompdf cant do the combined load/render)
$view = $this->load->view("viewname", $viewData, true);
//create a new dompdf instance (this is the crucial step)
$this->pdf = new DOMPDF();
//render and output our pdf
$this->pdf->load_html($view);
$this->pdf->render();
$pdf = $this->pdf->output(array("compress" => 0));
file_put_contents("some/file/path.pdf", $pdf );
yebdmbv4

yebdmbv42#

当我试图在循环中生成pdf时,我遇到了CodeIgniter 3的这个问题。问题是由于多次加载pdf库而未清除现有示例。为了避免这种情况-我每次都创建了新的PDF示例。

require_once APPPATH.'third_party/dompdf/autoload.inc.php';
use Dompdf\Dompdf;

class Index extends CI_Controller {

    public function gencert() {
       $nurseids = $this->nurse_model->getNurseIdsForCert();
       foreach($nurseids as $nid){
         //print_r($nid);
         $this->nursecertificate($nid->nurseid);
       } 
    }
    
    private function nursecertificate($nurseid){
       // Load pdf library
       //$this->load->library('pdf');
       $this->pdf = new DOMPDF();
    
       // Load HTML content
       $this->pdf->loadHtml($html, []);
       ...

相关问题