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:
- Ensure you are always validating the HTML markup using W3C Validator - broken code can have adverse affects, not as forgiving as a browser!
- Some error messages received in the past have been related to PHP version so ensure that the environment is compatible
- Use HTML tables and code similar to how you would if you were creating an email build
- If a generated PDF may exceed more than one page, try to avoid nesting everything within one table otherwise, you may have situations where the entire page is skipped or even not loading at all