私はTCPDFライブラリを使用して、その場でPDFを作成しています。私のアプリケーションでは、これらのドキュメントを個別のPDFドキュメントを含むZIPファイルに一括してダウンロードできるようにしたいと考えています。どのように私はこれを達成するかもしれないかについての任意のアイデア?動的に生成されたPDFファイルをzipファイルに保存するにはどうすればよいですか?
以下は、オンザフライで3つの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');
?>
このコードの問題は何ですか?それは動作しませんか? –
セマンティクスはここですが、私はWindowsユーザーにはっきりと言いたいことがあります。* "zipフォルダ"はありません。* Zip **ファイル**が存在し、内部的にファイルシステム構造を模倣することができます。 * zipファイル*は単なるファイルです。 A * zipフォルダ*は、ユニコーンが生きている架空の場所です:) – rdlowrey
上記のコードは機能しません。奇妙なTCPDFエラーを投げます。現在の出力 "S"はファイルを文字列として保存すると思いますが、それはおそらく問題でしょうか? http://www.tcpdf.org/doc/classTCPDF.html#a3d6dcb62298ec9d42e9125ee2f5b23a1 – Michael