Skip to content

PDF generation🔗

Required🔗

Installation🔗

1
composer require mpdf/mpdf

Usage🔗

Générer un fichier PDF depuis un contenu HTML🔗

1
2
3
4
5
6
7
8
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));
}

Sauvegarder le contenu d'un PDF sans passer par un fichier intermédiaire🔗

Par exemple pour envoyer directement sur un storage Flysystem :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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: December 20, 2024