私が達成しようとしていること: ユーザーが選択した pdf ファイルを含む zip ファイルを強制的にダウンロードします。
これを達成するためにコントローラーで行ったこと:
フォルダー APP.WEBROOT_DIR.DS."package_files" に PDF レポートを生成します (私は MPDF ライブラリを使用しました) *正しい読み取り可能な PDF を生成します。ここで $this->render(); と呼びます。
php の Zip 機能を使用して、package.zip (上記の指定したフォルダーからの pdf ファイルで構成されます) を生成します *正しい zip ファイルを生成し、サーバーからダウンロードすると、Windows で有効な zip ファイルとして開きます。
コントローラーのviewClassをMediaに設定し、パラメーターを設定してzipファイルとしてダウンロードを強制します。 *ここでも、ここで$this->render(); 問題: 実行すると zip ファイルが取得されますが、winrar で開くと、Zip ファイルがアーカイブの予期しない終了を報告します。
この問題を解決するのに役立つ記事はありません...
私が推測するのは、レンダリングを2回呼び出すと、ファイルが破損すること です
私のコントローラーコード:
/** before this code i generate pdf files and have no issue **/
/** now scan through the directory and add all the pdf files to a zip archive **/
$dir = new Folder("".APP.WEBROOT_DIR.DS."package_files");
$files = $dir->find('.*\.pdf');
$zip = new ZipArchive();
foreach ($files as $file) {
$file_path = $dir->pwd() . DS . $file;
$filename = $dir->pwd() . DS ."package.zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFile($file_path,$file);
}
$zip->close();
/** now render the action to download the generated zip file **/
$this->viewClass = 'Media';
$params = array(
'id' => 'package.zip',
'name' => 'packaged_file',
'download' => true,
'extension' => 'zip',
'path' => APP . WEBROOT_DIR.DS.'package_files' . DS
);
$this->set($params);
$this->render();