Multi-Downloads with PHP and mPDF

3

I'm trying to perform a function in PHP that performs multiple PDF file downloads within a repeat loop. The problem is that you only download the file independently of the size of the array.

Follow the code below:

$testes = array('teste','teste2');

foreach ($testes as $teste) {

   $arquivoHtml = file_get_contents(__SYSTEM_URL__.'/templates/teste.html');

   $arquivoHtml = str_replace('%TESTE%', $teste, $arquivoHtml);

   $mpdf0 = new mPDF('', 'A4', 0, 'Tahoma', 0, 0, 0, 0, 0, 0, 0, 0, 'P');

   $mpdf0 -> WriteHTML(utf8_encode($arquivoHtml));
   $mpdf0 -> Output("{$teste}.pdf", 'D');
}   
    
asked by anonymous 31.08.2017 / 20:57

2 answers

3

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

31.08.2017 / 23:58
0

It's because of this line:

$arquivoHtml = str_replace('%TESTE%', $teste, $arquivoHtml);

You have to replace the name here first:

__SYSTEM_URL__.'/templates/teste.html'

And then add in file_get_contents

It drops only one because it only takes the test.html

Code:

$testes = array('teste','teste2');

foreach ($testes as $teste) {

   $arquivo = str_replace('%TESTE%', $teste, __SYSTEM_URL__.'/templates/teste.html');

   $arquivoHtml = file_get_contents($arquivo);

   $mpdf0 = new mPDF('', 'A4', 0, 'Tahoma', 0, 0, 0, 0, 0, 0, 0, 0, 'P');

   $mpdf0 -> WriteHTML(utf8_encode($arquivoHtml));
   $mpdf0 -> Output("{$teste}.pdf", 'D');
} 
    
31.08.2017 / 22:17