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

如何使用PHP将dom pdf生成的pdf文件添加到ZipArchive,php,dompdf,ziparchive,Php,Dompdf,Ziparchive,我使用dom pdf从html文本内容创建了一个.pdf文件。同时,我必须创建一个zip存档并将该pdf文件添加到zip 该zip还包含两个以上的文件夹。在将DOMPDF生成的文件添加到zip归档文件时,我被卡住了。有人能为我提供解决方案吗?使用此代码从html内容生成pdf文件 $html = "put your html code here"; $dompdf = new DOMPDF(); $dompdf->load_html($html); $dompdf->set_pape

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


该zip还包含两个以上的文件夹。在将DOMPDF生成的文件添加到zip归档文件时,我被卡住了。有人能为我提供解决方案吗?

使用此代码从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);
使用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));

使用此代码从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);
使用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));

您采取了哪些步骤来添加PDF?你在用什么?PDF文件是放在zip文件的根目录下,还是需要放在现有文件夹下?@BrianS感谢您的回复。我在一天前找到了解决办法。:)很高兴知道。如果有人对你如何解决问题感兴趣,请将你的解决方案作为答案发布。你采取了哪些步骤来添加PDF?你在用什么?PDF文件是放在zip文件的根目录下,还是需要放在现有文件夹下?@BrianS感谢您的回复。我在一天前找到了解决办法。:)很高兴知道。如果有人对你如何解决问题感兴趣,请将你的解决方案作为答案张贴出来。