如何使用 PHP 将 dom pdf 生成的 pdf 文件添加到 ZipArchive

Aak*_*shC 1 php dompdf ziparchive

我已经使用 dom pdf 从 html 文本内容创建一个 .pdf 文件。同时,我必须创建一个 zip 存档并将该 pdf 文件添加到 zip。

该 zip 还包含更多两个文件夹。我在将 DOMPDF 生成的文件添加到 zip 存档时卡住了。任何人都可以为我提供解决方案吗?

Aak*_*shC 5

使用此代码从 html 内容制作 pdf 文件。

$html = "put your html code here";
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper("a4", "portrait");
$dompdf->render();
$output = $dompdf->output();
file_put_contents('Otherfile.pdf', $output);
Run Code Online (Sandbox Code Playgroud)

使用 render() 函数后,使用 output() 函数将 pdf 文件存储在变量中。并使用 file_put_contents() 将该内容放入新文件中。然后使用以下代码将该文件放入 .zip 中。

$files = 'Otherfile.pdf';
$zipname = 'zipname.zip';
$zip = new ZipArchive;      
$zip->open($zipname, ZipArchive::OVERWRITE);
$zip->addFile($files);

$zip->close();
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename='".$zipname."'");
header("Pragma: no-cache"); 
header("Expires: 0");
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($zipname));
Run Code Online (Sandbox Code Playgroud)