PDF generation
Required
Installation
 | composer require mpdf/mpdf
  
 | 
Usage
Générer un fichier PDF depuis un contenu HTML
 | function __invoke(Environment $twig)
{
  $html = $twig->render('foobar.html.twig');
  $pdf = new Mpdf();
  $pdf->WriteHTML($html);
  $pdf->Output('path/to/dir/foobar.pdf', Destination::FILE));
}
  
 | 
Par exemple pour envoyer directement sur un storage Flysystem :
 | function __invoke(Environment $twig, FilesystemOperator $storage)
{
  $html = $twig->render('foobar.html.twig');
  $pdf = new Mpdf();
  $pdf->WriteHTML($html);
  $content = $pdf->Output('', Destination::STRING_RETURN));
  $storage->write('foobar.pdf', $content);
}
  
 | 
ou dans un fichier ZIP :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12  | function __invoke(Environment $twig)
{
  $html = $twig->render('foobar.html.twig');
  $pdf = new Mpdf();
  $pdf->WriteHTML($html);
  $content = $pdf->Output('', Destination::STRING_RETURN));
  $zip = new \ZipArchive();
  $zip->open('archive.zip', \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
  $zip->addFromString('foobar.pdf', $content);
}
  
 | 
                
                  
                    
  
    
      Last update: October 14, 2025