2012-01-23 8 views
1

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

?> 
+0

このコードの問題は何ですか?それは動作しませんか? –

+3

セマンティクスはここですが、私はWindowsユーザーにはっきりと言いたいことがあります。* "zipフォルダ"はありません。* Zip **ファイル**が存在し、内部的にファイルシステム構造を模倣することができます。 * zipファイル*は単なるファイルです。 A * zipフォルダ*は、ユニコーンが生きている架空の場所です:) – rdlowrey

+0

上記のコードは機能しません。奇妙なTCPDFエラーを投げます。現在の出力 "S"はファイルを文字列として保存すると思いますが、それはおそらく問題でしょうか? http://www.tcpdf.org/doc/classTCPDF.html#a3d6dcb62298ec9d42e9125ee2f5b23a1 – Michael

答えて

-2

私はそれはあなたの問題を解決することをわからないんだけど、あなたは必要はありません。

$files_to_zip[$i] = $filename;

の代わり:

$files_to_zip[] = $filename;

???

+0

彼はいません。 PHPでは、大括弧で配列に追加すると、最後に次の数値インデックスが追加されます。 – davidethell

0

ファイルを圧縮するためにPDFを必要とするため、出力呼び出しを 'S'ではなく 'F'を渡すように変更します。

タイムアウトに関しては、set_time_limit()でPHPのタイムアウトを増やしてみましたか?サーバーがその機能を許可している場合は、必要に応じてset_time_limit(90)以上を試して、スクリプトが機能しているかどうかを確認してください。

2
<?php 

require_once('tcpdf_include.php'); 

// create new PDF document 
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 

// set document information 
$pdf->SetCreator(PDF_CREATOR); 
$pdf->SetAuthor('Somnath Paul'); 
$pdf->SetTitle('TCPDF Example with ZIP'); 
$pdf->SetSubject('TCPDF '); 
$pdf->SetKeywords('TCPDF'); 

// set default header data 
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128)); 
$pdf->setFooterData(array(0,64,0), array(0,64,128)); 

// set header and footer fonts 
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); 
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); 

// set default monospaced font 
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); 

// set margins 
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); 
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER); 
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER); 

// set auto page breaks 
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); 

// set image scale factor 
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

// set some language-dependent strings (optional) 
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) { 
    require_once(dirname(__FILE__).'/lang/eng.php'); 
    $pdf->setLanguageArray($l); 
} 

// --------------------------------------------------------- 

// set default font subsetting mode 
$pdf->setFontSubsetting(true); 

// Set font 
// dejavusans is a UTF-8 Unicode font, if you only need to 
// print standard ASCII chars, you can use core fonts like 
// helvetica or times to reduce file size. 
$pdf->SetFont('dejavusans', '', 14, '', true); 

// Add a page 
// This method has several options, check the source code documentation for more information. 
$pdf->AddPage(); 

// set text shadow effect 
$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal')); 

// Set some content to print 
$html = <<<EOD 
<h1>This is a sample PDF </h1> 
EOD; 

// Print text using writeHTMLCell() 
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); 

// --------------------------------------------------------- 

// Close and output PDF document    

$pdf->Output('example_001.pdf', 'F'); 

// ZIP the PDF file 

$temp_file_name = 'my-pdf.zip'; 

$zip = new ZipArchive(); 
$file = $temp_file_name; 
$zip->open($file, ZipArchive::OVERWRITE); 
$zip->addFile('example_001.pdf', 'a.pdf'); 
$zip->close(); 


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


//============================================================+ 
// END OF FILE 
//============================================================+ 
0
この

書き込み:

for($i=0;$i < 2 ;$i = $i+1){ 
... 
} 

の代わり:

for($i=0;$i < 2 ;$i+1){ 
... 
} 
関連する問題