Multiple downloads with PHP and mPDF

Asked

Viewed 1,127 times

3

I am trying to perform a function in PHP that performs several downloads of the PDF file within a loop. The problem is that it only downloads once the file independent of the array size.

Follows code:

$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');
}   

2 answers

3


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

  • 1

    I will try to implement and soon I answer

  • 1

    It worked, thank you very much!

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 to file_get_contents

It only downloads 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');
} 
  • Still, when entering the loop only download once the file

  • Try to keep it just 'test' because he may not be recognizing it by this capital letter. but anyway try to put an echo to see if the path is correct. $file = str_replace('test', $test, SYSTEM_URL.'/templates/html test.');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.