Actually, this code will only be executed once, because, the default is one request at a time and in the command of mpdf->Output has the output of a download.
To do something like this you need to generate the PDF
, compress them and then download those PDF
, an example would be the generation of a file Zip
and then at the end of the generation of these PDF
would download at once, in order for this code to work a folder has to be created temp
in the execution of that script
.
Code
Observing: the current code had changes for this mainly in the generation with the template, and paths, stay tuned to these modifications and something else.
<?php
require('vendor/autoload.php');
// Criando PDF temporário
$testes = array('teste1', 'teste2');
$arquivoHtml = file_get_contents('templates/teste.html');
foreach ($testes as $teste)
{
$arquivoNew = str_replace('%TESTE%', $teste, $arquivoHtml);
$mpdf0 = new mPDF('', 'A4', 0, 'Tahoma', 0, 0, 0, 0, 0, 0, 0, 0, 'P');
$mpdf0->WriteHTML(utf8_encode($arquivoNew));
$mpdf0->Output("temp/{$teste}.pdf", 'F');
}
// Criando Zip Temporário
$cwd = "temp/";
$nameZip = "arquivos_".(uniqid()).".zip";
$zip = new ZipArchive();
if ($zip->open($nameZip, ZIPARCHIVE::CREATE) === true)
{
$open = opendir($cwd);
while ($folder = readdir($open)) {
if ($folder != '.' && $folder != '..') {
$arq = str_replace('./', '', $cwd . '/' . $folder);
$zip->addFile($arq);
}
}
}
$zip->close();
// Removendo PDF Temporários
$open = opendir($cwd);
while ($folder = readdir($open))
{
if ($folder != '.' && $folder != '..')
{
unlink($cwd . '/' .$folder);
}
}
// Download ZipFile PDF
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$nameZip.'"');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($aquivoNome));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Expires: 0');
readfile($nameZip);
// Removendo Zip Temporário
unlink($nameZip);
References
I will try to implement and soon I answer
– Bruno Folle
It worked, thank you very much!
– Bruno Folle