1

我想用 pdf 导出数据。当我选择一条记录时,它工作正常...如何一键创建多个 pdf?这是我尝试过的

require_once('eng.php');
require_once('tcpdf.php');
$pageLayout = array(750, 800); 
$pdf =new TCPDF('P', 'pt', $pageLayout, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->AddPage();
$html = 'my html';
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
$pdf->Output('filename.pdf', 'D');

我怎样才能放一个循环来创建更多的 pdf 单独文件?

4

4 回答 4

2

以下代码对我有用:

使用变量的变量并在循环中创建新的 pdf 对象,使用“F”而不是“D”保存到服务器。

require_once('eng.php');
require_once('tcpdf.php');
$pageLayout = array(750, 800); 

$content=Array(
 '<html><body>Document A</body></html>',
 '<html><body>Document B</body></html>',
 '<html><body>Document C</body></html>'
);

foreach($content as $i=>$html){
$pdfname = $pdf . $i;
$$pdfname =new TCPDF('P', 'pt', $pageLayout, true, 'UTF-8', false);
$$pdfname->SetCreator(PDF_CREATOR);
$$pdfname->AddPage();
$$pdfname->writeHTML($html, true, false, true, false, '');
$$pdfname->lastPage();
$$pdfname->Output('filename_' . $i . '.pdf', 'D');
}
于 2015-06-18T07:19:35.627 回答
1

尝试这个:

require_once('eng.php');
require_once('tcpdf.php');
$pageLayout = array(750, 800); 

$content=Array(
 '<html><body>Document A</body></html>',
 '<html><body>Document B</body></html>',
 '<html><body>Document C</body></html>'
);

foreach($content as $i=>$html){
    $pdf =new TCPDF('P', 'pt', $pageLayout, true, 'UTF-8', false);
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->AddPage();
    $pdf->writeHTML($html, true, false, true, false, '');
    $pdf->lastPage();
    $pdf->Output('filename_' . $i . '.pdf', 'D');
}
于 2013-06-03T12:54:03.677 回答
0

我已经完善了答案以匹配我的评论

require_once('eng.php');
require_once('tcpdf.php');
$pageLayout = array(750, 800); 

$content=Array(
 '<html><body>Document A</body></html>',
 '<html><body>Document B</body></html>',
 '<html><body>Document C</body></html>'
);

$zip = new ZipArchive();
$filename = "/tmp/final.zip";
$zip->open($filename, ZipArchive::CREATE);

foreach($content as $i=>$html){
    $pdf =new TCPDF('P', 'pt', $pageLayout, true, 'UTF-8', false);
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->AddPage();
    $pdf->writeHTML($html, true, false, true, false, '');
    $pdf->lastPage();
    $pdf->Output('/tmp/filename_' . $i . '.pdf', 'F');
    $zip->addFile('/tmp/filename_' . $i . '.pdf','filename_' . $i . '.pdf');
}

$zip->close();

header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="final.zip"');
readfile($filename);

答案假设一个 posix 环境 /tmp 目录,这只是一个例子,你应该使用 sys_get_temp_dir() 来获取临时目录,你还应该在下载文件后删除它们。

于 2013-06-03T13:30:44.657 回答
0

我正在创建一个 pdf 文档以保存在磁盘中,它在循环中也能完美运行。在这里我只复制了那个函数有循环

 private function drawBody()
{
   
        foreach($this->ordersList as $store => $storeord){
         // to stop the tcpd overwriting the headers and calculating wrong page numbers used $nextstore
                
                    $filename = 'Click and Collect List to '.$this->storeID.'-'.$this->storename;
                    // Master page headers
                    $this->pdf->SetFont('Arial','B');
                    $this->pdf->SetFontSize(20);
                    $this->pdf->SetY(50);
                    
                    $this->pdf->Cell(0,7,$filename,0,1,'L');

                    $this->pdf->SetFontSize(8);
                    $this->pdf->Ln();
                    $this->pdf->Ln();
                    $this->pdf->SetFillColor(255,255,255);
                    $this->pdf->Cell(15,10,'ID',1,0,'C',1,'',1);
                    $this->pdf->Cell(20,10,'Date ordered',1,0,'C',1,'',1);
                    $this->pdf->Cell(5,10,'Qty',1,0,'C',1,'',1);
                    $this->pdf->Cell(15,10,'Customer ID',1,0,'C',1,'',1);
                    $this->pdf->Cell(30,10,'Name',1,0,'C',1,'',1);
                    $this->pdf->Cell(25,10,'Mobile',1,0,'C',1,'',1);
                    $this->pdf->Cell(70,10,'Address',1,0,'C',1,'',1);
                
                    $this->pdf->Ln();
                   
                
                // Master page list of orders
                foreach($storeord as $ord => $det){                       
                    $this->pdf->SetFont('Arial');
                    $this->pdf->SetFillColor(255,255,255);
                    $this->pdf->SetFontSize(8);
                    $this->pdf->Cell(15,10,''. $ord,1,0,'L',1);
                    $this->pdf->Cell(20,10,''. $det[orderdate],1,0,'L',1);
                    $this->pdf->Cell(05,10,''. $det[qty],1,0,'L',1);
                    $this->pdf->Cell(15,10,''. $det[custID],1,0,'L',1);
                    $this->pdf->Cell(30,10,''. $det[custName],1,0,'L',1);                        
                    $this->pdf->Cell(25,10,''. $det[mobile],1,0,'L',1);
                    $this->pdf->MultiCell(70,10,''. $det[address],1,'L',false);                        
                   
                }
           
                // Draw Collection page for each store
             $this->pdf->AddPage();                    
             $this->drawCollectionPage();
             
             $this->pdf->Output('Click and Collect List to - '.$this->storeID.'-'.$this->storename.'.pdf','F');
             
        }

}

于 2020-07-31T15:21:50.033 回答