Skip to Content
laravel pdf generator with Dompdf
laravel code / August 1, 2023

PDF is a popular file format for storing data for offline consumption. HTML to PDF conversion capabilities must be included in the web application if the user is to be able to download HTML content as a PDF file. PHP may be used to convert HTML to PDF using the Dompdf package.

Dompdf is a PHP package that aids in the conversion of HTML documents to PDF documents. The Dompdf library is the best solution for creating PDF files and adding HTML information to PDF documents if your application is developed with Laravel. In this tutorial, we’ll teach you how to use Dompdf to convert HTML to PDF in Laravel.

In Laravel, you can convert HTML to PDF.
To install Dompdf using Composer in your Laravel application, run the command below.

composer require dompdf/dompdf

namespace App\Http\Controllers; 
use App\Http\Controllers\Controller; 
// Reference the Dompdf namespace 
use Dompdf\Dompdf; 
class HomeController extends Controller 
public function convert() 
// Instantiate and use the dompdf class 
$dompdf = new Dompdf(); 
// Load HTML content 
$dompdf->loadHtml('<h1>Welcome to</h1>'); 
// (Optional) Setup the paper size and orientation 
$dompdf->setPaper('A4', 'landscape'); 
// Render the HTML as PDF 
// Output the generated PDF (1 = download and 0 = preview) 
$dompdf->stream("welcome.pdf", array("Attachment"=>1)); 


This Laravel 8 HTML to PDF converter software makes it simple to create a PDF document with dynamic information. This example code works with any version of the Laravel Framework, as well as Lumen. Dompdf offers a number of customizable options for tailoring the PDF generating capabilities to your specific requirements.

PHPCODE © 2024