PHP TCPDF Create Multiple PDFS With One Command

馋奶兔 提交于 2019-12-13 18:13:15

问题


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?


回答1:


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



回答2:


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:


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.



来源:https://stackoverflow.com/questions/16896671/php-tcpdf-create-multiple-pdfs-with-one-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!