Convert HTML to PDF in Laravel with Dompdf

A file with the .pdf file extension is called a Portable Document Format (PDF) file. It is typically used for read-only documents. Nowadays most the read-only documents shared in PDF format like eBooks, application forms, user manuals and, etc

PDF is a commonly used format to share and store data for offline uses. Sometimes we need to generate the online website data in PDF. There are multiple tools and third-party extensions available that you can use to do the same task.

In this tutorial, we will learn how to convert HTML to PDF in Laravel with Dompdf. If you want to check HTML to PDF conversion using PHP you can check that here.

Dompdf is used to convert HTML to PDF. It is very easy to use and you will be able to integrate it with the minimum possible code as given below.

Below is a step-by-step Laravel 8|7 tutorial about how to convert HTML to PDF within the Laravel application using the Dompdf extension.

Sometimes in some parts of the website, we need to store the data in PDF format or we need to give an option to the user to download the HTML content in PDF. In that case, we need something which can help us to convert the content to PDF. so here comes DomPdf which is a library used for converting direct HTML to PDF. It can read external stylesheets, inline style tags, and the style attributes of individual HTML elements. So whatever the design of the HTML page is, you can directly convert that layout to PDF using Dompdf.

The Dompdf is a PHP library that helps to convert HTML to PDF documents. If your application is built with Laravel, the Dompdf library is the best option to create a PDF file and add HTML content to a PDF document. In this tutorial, we will show you how to convert HTML to PDF in Laravel with Dompdf.

Following are the steps to convert HTML to PDF in Laravel

Convert HTML to PDF in Laravel

Run the following command to install Dompdf via Composer in your Laravel application.

composer require dompdf/dompdf

For this example, we will use the convert() method of the HomeController to convert HTML content to PDF documents.

  • At first, define the reference the Dompdf namespace.
  • In the convert() method, add the code to handle the HTML to PDF conversion process.
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)); 


Dompdf provides a simple way to create a PDF documents with dynamic content in Laravel. You can use this example in Laravel’s other released versions. it’s very easy to use and compatible with CSS frameworks. You can use Dompdf in HTML and PHP just click our tutorial HTML to PDF conversion using PHP to check that.