Actually, this code will only be executed once, because the default is one request at a time and the mpdf- > Output has the output of a download.
In order to do something similar, you need to generate the PDF
, compact them and then download those PDF
, an example would be the generation of a Zip
file, and then at the end of the PDF
generation would do the one-time download, for this code to work, a temp
folder must be created when running this script
.
Code
the current code had changes to this one especially in generation with the template in , and paths, stay tuned for 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