douduoquan2824 2015-09-30 15:35
浏览 238

使用dompdf循环不能正常保存多个pdf文件

I am using dompdf. I want to save multiple pdf files to the server. Below is my code through which im trying to accomplish this task. But im getting all my orders in one pdf file. That means only one pdf file is generated although the loop is executing more than once. And the data is appending in the same pdf file and also the data is correct.

$sel_orders = "SELECT * FROM tbl_orders WHERE `user_id`=$user_id AND `mail`='pending' GROUP BY order_num";
$order_res = mysql_query($sel_orders);
$sum = mysql_num_rows($order_res);

$dir = dirname(__FILE__);
require_once($dir.'/dompdf/dompdf_config.inc.php');
if($sum > 0)
{
    while($row1 = mysql_fetch_array($order_res))
    {
        $order_num = $row1['order_num']; // test-123
        $post = new stdClass;
        ob_start();
        include($dir.'/pdf.php');
        $pdf_html = ob_get_contents();
        ob_end_clean();
        $dompdf = new DOMPDF(); // Create new instance of dompdf
        $dompdf->load_html($pdf_html); // Load the html
        $dompdf->render(); // Parse the html, convert to PDF
        $pdf_content = $dompdf->output(); // Put contents of pdf into variable for later
        error_reporting(E_ERROR | E_PARSE);     
        $file_to_save = '"http://www.exampe.com/test/orders/$order_num.pdf"';
        file_put_contents($file_to_save, $dompdf->output());        

    }
}

In pdf.php file I have my html code which is printed in pdf created. Each time the control enters the loop I want to create one separate pdf. I tried the solution from below link but it doesnt work for me. Someone plz point me what im doing wrong?? If any further info needed plz let me know. Thanks!

I want to create multiple pdfs in a loop using dompdf?

  • 写回答

1条回答 默认 最新

  • douxuanyi2813 2015-09-30 15:47
    关注

    I very much doubt a web site is going to let you save a file to it using a URL, either way its easier to use a relative path and filename, even if this is your site where the code is running.

    Start by using a real and valid filename for the output. Also you save the document into $pdf_content and then dont use that variable.

    $pdf_content = $dompdf->output(); 
    
    // make this a valid directory and filename for your site
    // check that the web server account has correct privilages to write to it
    $file_to_save = "/test/orders/$order_num.pdf";
    
    file_put_contents($file_to_save, $pdf_content);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条