我正在使用TCPDF库来动态生成PDF。对于我的应用程序,我希望用户能够批量下载这些文档,并将它们放入一个ZIP文件中,其中包含单独的PDF文档。有什么办法可以实现这一点吗?
以下是一个测试脚本,尝试动态创建三个PDF文档。当前运行时会超时。
TCPDF文档列出了不同的输出文件方法,但似乎都不起作用:
以下是一个测试脚本,尝试动态创建三个PDF文档。当前运行时会超时。
TCPDF文档列出了不同的输出文件方法,但似乎都不起作用:
http://www.tcpdf.org/doc/classTCPDF.html#a3d6dcb62298ec9d42e9125ee2f5b23a1
<?php
/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
date_default_timezone_set('America/Los_Angeles');
$files_to_zip = array();
require_once('../../libraries/tcpdf/tcpdf.php');
for($i=0;$i < 2 ;$i+1){
$pdf = new TCPDF();
$pdf->AddPage('P', 'Letter');
$txt = 'Test '.$i;
$pdf->MultiCell(1, 1, $txt);
$filename = $pdf->Output('test'.$i.'.pdf', 'S');
$files_to_zip[] = $filename;
}
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive.zip');
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="my-archive-test.zip"');
readfile('my-archive-test.zip');
?>