Using DomPDF in Laravel and other PHP projects

Before Converting HTML to PDF

Ensure that the page has been set to the correct dimensions in the controller. When loading the PDF View you can set the paper. Something along the lines of:

// Set unique filename example
$filename = mt_rand(100, 1000000).'_'.date("Ymd_His");

$pdf = PDF::loadView('folder-name.file-name');
$pdf->setPaper('A4', 'portrait');

return $pdf->download($filename.'.pdf'); // download new pdf
return $pdf->stream($filename.'.pdf'); // or stream in browser

Also note that the container div of each page should be set to the same dimensions as the paper, you can set the width and height in mm to do this.

Tips when creating a PDF

Since you cannot use tools like inspect element when viewing a PDF you are limited in how you can test and debug.

It is good to switch between HTML and PDF view (if possible) but if you can't do this, please note the following: