DOMPDF Example in PHP

PHP Code Snippets No Comments

DOMPDF Example in PHP

If you’re looking for DOMPDF Example in PHP, then you’re at right place.

First of all you’ll have to get the latest library of the DOMPDF from Github.
Now you just need following 7 lines of code to put in your PHP file which you’re using the generate PDF.
In the following code, we’ve assumed that the dompdf directory is in the same directory as your PHP file. If not, adjust your i.e. first line of code accordingly.

require_once 'dompdf/'; //we've assumed that the dompdf directory is in the same directory as your PHP file. If not, adjust your i.e. first line of code accordingly.

// reference the Dompdf namespace

use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF

// Output the generated PDF to Browser

Hope this helps.
In case you face issues with images and images in the downloaded PDFs are not showing then check this tutorial of DOMPDF image not showing.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments