9951 explained code solutions for 126 technologies


php-laravelHow can I generate a PDF file with Laravel and PHP?


Generating a PDF file with Laravel and PHP is a straightforward process. The following example code will generate a PDF file from a HTML view using the Dompdf library:

use Dompdf\Dompdf;

$html = view('pdf.my-pdf-view')->render();

$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();

$dompdf->stream("mypdf.pdf");

This code consists of the following parts:

  1. The first line imports the Dompdf library.
  2. The second line loads the HTML view that will be converted into a PDF file.
  3. The third line creates a new instance of the Dompdf library.
  4. The fourth line loads the HTML content into the Dompdf instance.
  5. The fifth line sets the paper size and orientation.
  6. The sixth line renders the PDF file.
  7. The seventh line streams the PDF file to the browser.

For more information about generating PDF files with Laravel, please consult the following resources:

Edit this code on GitHub