本文整理汇总了PHP中Converter::save方法的典型用法代码示例。如果您正苦于以下问题:PHP Converter::save方法的具体用法?PHP Converter::save怎么用?PHP Converter::save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Converter
的用法示例。
在下文中一共展示了Converter::save方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: generateArchive
/**
* Generates archive of documents based on specified data.
*
* @param array $data array where each element is data for one document
* @param string $path where to save generated archive
* @param Converter $converter OPTIONAL document converter
*/
public function generateArchive($data, $path, $converter = null)
{
//Files to be deleted after creating archive
$cleanup = array();
//Create directory if nonexistent
@mkdir(dirname($path), 0770, true);
//Prepare result archive
$archive = new \ZipArchive();
if (!$archive->open($path, \ZipArchive::CREATE)) {
throw new \Exception("Failed to open result archive.");
}
//Create documents and add them to archive
foreach ($data as $index => $docdata) {
$ext = $this->template->getExtension();
//Generate file
$file = "{$this->tmp}/document{$index}.{$ext}";
$document = $this->generate($docdata);
//Convert if needed
if ($converter) {
$converter->save($document, $file);
$ext = $converter->getExtension();
} else {
$document->save($file);
}
//Remove this file after we're done
$cleanup[] = $file;
//Put to archive
$archive->addFile($file, "document{$index}.{$ext}");
}
//Write documents to archive
$archive->close();
//Remove temp files, has to be done after closing archive
foreach ($cleanup as $file) {
unlink($file);
}
//Check if archive was really created
if (!file_exists($path)) {
throw new \Exception("Failed to create result archive.");
}
}