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 autoload.inc.php i.e. first line of code accordingly.

<?php
require_once 'dompdf/autoload.inc.php'; //we've assumed that the dompdf directory is in the same directory as your PHP file. If not, adjust your autoload.inc.php 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
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();
?>

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.

3 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments