0

Good day !

I use mpdf in a loop

require_once __DIR__ . '/vendor/autoload.php';
$path_html= __DIR__.'/html/';
$path_pdf= __DIR__.'/pdf/';
$amount_file=10;
$mpdf = new \Mpdf\Mpdf();
if (!file_exists($path_pdf)) {mkdir($path_pdf, 0755, true);}
function random_html($dir = 'html')
{
    $files = glob($dir . '/*.*');
    $file = array_rand($files);
    return $files[$file];
}
$alphanum = 'abxzrmhtuiops123456789';

for ($amount_file_count=1; $amount_file_count <= $amount_file; $amount_file_count++) {

    $rand_file_name = substr(str_shuffle($alphanum), 2, 10) ;
    $html_file = file_get_contents(random_html());
    $mpdf->WriteHTML($html_file);
    $mpdf->Output($path_pdf . $rand_file_name . '.pdf');
    echo $amount_file;
}

When create next pdf add old null page, Why? How do this correct? How correct FOR cicle ?

  • Have you tried moving `$mpdf = new \Mpdf\Mpdf();` inside the `for`? – Andrea Olivato Jul 24 '21 at 13:56
  • Does this answer your question? [mPDF - How to create multiple PDFs?](https://stackoverflow.com/questions/44533081/mpdf-how-to-create-multiple-pdfs) – Finwe Jul 25 '21 at 07:07

1 Answers1

-1
<?php
require_once __DIR__ . '/vendor/autoload.php';
$path_html= __DIR__.'/html/';
$path_pdf= __DIR__.'/pdf/';
$amount_file=10;
$mpdf = new \Mpdf\Mpdf();
if (!file_exists($path_pdf)) {mkdir($path_pdf, 0755, true);}
function random_html($dir = 'html')
{
    $files = glob($dir . '/*.*');
    $file = array_rand($files);
    return $files[$file];
}
$alphanum = 'abxzrmhtuiops123456789';
for ($amount_file_count=1; $amount_file_count <= $amount_file; $amount_file_count++) {
    $mpdf = new \Mpdf\Mpdf();
    $rand_file_name = substr(str_shuffle($alphanum), 2, 10);
    $html_file = file_get_contents(random_html());
    $mpdf->WriteHTML($html_file);
    $mpdf->Output($path_pdf . $rand_file_name . '.pdf');
    echo $amount_file;
}
  • To be helpful to others, please explain your answer. In all this code, it is quite hard to spot what has changed. Also, remove the unrelated code for the example to be more readable and concise. – Finwe Jul 26 '21 at 07:49