1

I want to export data with pdf.When i select one record it works fine...How to create multiple pdfs with one click? here is what i tried

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');

How i can put one loop to create more pdf seperate files?

0

4 Answers 4

2

Following code is working for me :

Use variable of variable and create new pdf object in loop, use 'F' instead of 'D' to save to server.

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');
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

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');
}

3 Comments

Same issue,it only creates filename_0.pdf..i want to create filename_1.pdf and filename_2.pdf also,as there are three items in array
Hi my mistake, you need to change the 'D' option for output tcpdf.org/doc/code/… documents the options. You cannot create the 3 files a single download, you must create them on the server zip then download the resulting zip
'I' send the file inline to the browser,This is again it is doing one at a time...i need to download/save all of them at once
0

I have refined the answer to match my comment

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);

The answer asumes a posix environment the /tmp directory this is only as an example you should use sys_get_temp_dir() to get the temp directory, you should also delete the files after they have been download.

4 Comments

Note: if have changed the option to 'F' to save the files locally
can you please tell me how to use system tmp dir?i do not want to created files on my server
$tmpdir = sys_get_temp_dir(); You will have to create files on the server but tmp dir is the correct place to do it.
you every loop iteration create new pdf object - and when generating 2000 files it may kill the system. I encoutered such problem now
0

I was creating a pdf document to save in the disk and it also worked perfectly within the loop. Here I have copied only that function has the loop

 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');
             
        }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.