Skip to main content
PHP Blog

Back to all posts

How to Export Pdf File In Laravel?

Published on
6 min read
How to Export Pdf File In Laravel? image

Best PDF Export Tools in Laravel to Buy in October 2025

1 GEARWRENCH Professional Bi-Directional Diagnostic Scan Tool | GWSMARTBT

GEARWRENCH Professional Bi-Directional Diagnostic Scan Tool | GWSMARTBT

  • OE-LEVEL DIAGNOSTICS ON YOUR SMART DEVICE FOR ULTIMATE CONVENIENCE.
  • FREE LIFETIME SOFTWARE UPDATES-NO SUBSCRIPTIONS OR HIDDEN FEES!
  • COMPLETE CONTROL WITH LIVE ACTUATION TESTS & 23 RESET/RELEARN FUNCTIONS.
BUY & SAVE
$138.13 $149.99
Save 8%
GEARWRENCH Professional Bi-Directional Diagnostic Scan Tool | GWSMARTBT
2 Financial Management of Health Care Organizations: An Introduction to Fundamental Tools, Concepts and Applications

Financial Management of Health Care Organizations: An Introduction to Fundamental Tools, Concepts and Applications

BUY & SAVE
$76.50 $139.95
Save 45%
Financial Management of Health Care Organizations: An Introduction to Fundamental Tools, Concepts and Applications
3 Elitech 2Pack Digital Bluetooth Hygrometer Thermometer, Refrigerator Thermometer with Free APP, Real-Time Temperature Humidity Monitor, PDF&CSV Data Export, IPT-100S with External Probe

Elitech 2Pack Digital Bluetooth Hygrometer Thermometer, Refrigerator Thermometer with Free APP, Real-Time Temperature Humidity Monitor, PDF&CSV Data Export, IPT-100S with External Probe

  • ACCURATE MONITORING: DUAL SENSORS ENSURE PRECISE INDOOR AND OUTDOOR DATA.
  • FLEXIBLE RECORDING: ONLINE/OFFLINE MODES WITH 5,000 DATA SETS STORAGE.
  • EASY DATA ACCESS: INSTANT REVIEW AND APP MONITORING FOR CONVENIENCE.
BUY & SAVE
$56.99
Elitech 2Pack Digital Bluetooth Hygrometer Thermometer, Refrigerator Thermometer with Free APP, Real-Time Temperature Humidity Monitor, PDF&CSV Data Export, IPT-100S with External Probe
4 Qualitative Data Collection Tools: Design, Development, and Applications (Qualitative Research Methods)

Qualitative Data Collection Tools: Design, Development, and Applications (Qualitative Research Methods)

BUY & SAVE
$51.00
Qualitative Data Collection Tools: Design, Development, and Applications (Qualitative Research Methods)
5 Powerbuilt Power Steering and Alternator Pulley Remove and Install Tool Set, Replace Vehicle Steering Pump, Car Repair Tools, Storage Case - 648605

Powerbuilt Power Steering and Alternator Pulley Remove and Install Tool Set, Replace Vehicle Steering Pump, Car Repair Tools, Storage Case - 648605

  • VERSATILE KIT FITS MOST ALTERNATORS AND POWER STEERING PULLEYS.

  • PROTECTS DELICATE PARTS; REDUCES RISK OF DAMAGE DURING USE.

  • COMES IN A STURDY CASE WITH INSTRUCTIONS AND USAGE PHOTOS.

BUY & SAVE
$35.50 $59.99
Save 41%
Powerbuilt Power Steering and Alternator Pulley Remove and Install Tool Set, Replace Vehicle Steering Pump, Car Repair Tools, Storage Case - 648605
6 Microeconomics: Principles, Applications, and Tools

Microeconomics: Principles, Applications, and Tools

BUY & SAVE
$279.99
Microeconomics: Principles, Applications, and Tools
7 All About PDF Stamps In Acrobat® & Paperless Workflows

All About PDF Stamps In Acrobat® & Paperless Workflows

BUY & SAVE
$24.95
All About PDF Stamps In Acrobat® & Paperless Workflows
8 Macroeconomics: Principles, Applications, and Tools

Macroeconomics: Principles, Applications, and Tools

BUY & SAVE
$286.65
Macroeconomics: Principles, Applications, and Tools
+
ONE MORE?

To export a PDF file in Laravel, you can use the barryvdh/laravel-dompdf package.

First, you need to install the package by running the following command in your terminal:

composer require barryvdh/laravel-dompdf

Next, publish the package's config file by running the following command:

php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

After that, you can create a new route in your web.php file that will handle the PDF export:

Route::get('export-pdf', 'PdfController@exportPdf');

Create a new controller called PdfController and define the exportPdf method that will generate and export the PDF file:

use PDF;

public function exportPdf() { $data = ['name' => 'John Doe', 'email' => 'john@example.com']; $pdf = PDF::loadView('pdf.invoice', $data);

return $pdf->download('invoice.pdf');

}

In the example above, we are loading a view called pdf.invoice and passing some data to it. Make sure you have a Blade view file invoice.blade.php in the resources/views/pdf directory.

You can customize the PDF file by adding HTML markup to the Blade view file. Finally, you can download the PDF file by visiting the export-pdf route in your browser.

This is a basic example of how to export a PDF file in Laravel using the barryvdh/laravel-dompdf package. You can explore more advanced features and customization options in the official documentation of the package.

How to set PDF properties like title and author in Laravel?

To set PDF properties like title and author in Laravel, you can use the SetOption method provided by the Dompdf library. Here's an example:

use Dompdf\Dompdf; use Dompdf\Options;

$options = new Options(); $options->set('title', 'My PDF Title'); $options->set('author', 'John Doe');

$dompdf = new Dompdf($options);

$html = 'Hello, World!';

$dompdf->loadHtml($html); $dompdf->render();

$dompdf->stream('document.pdf', array('Attachment' => 0));

In this code snippet, we first create an instance of the Options class and set the title and author properties using the set method. Then, we create a new Dompdf instance with the specified options. Next, we load the HTML content into the Dompdf instance, render the PDF, and stream it for download with the specified filename.

By setting these properties in the Options, you can customize various PDF properties like title, author, subject, keywords, etc.

How to generate a PDF file with dynamic content in Laravel?

To generate a PDF file with dynamic content in Laravel, you can use the "barryvdh/laravel-dompdf" package. Here's how you can do it:

  1. Install the "barryvdh/laravel-dompdf" package by running the following composer command:

composer require barryvdh/laravel-dompdf

  1. Once the package is installed, you can use it in your Laravel application to generate PDF files with dynamic content. Here's an example of how you can generate a PDF file with dynamic content in a Laravel controller:

use Barryvdh\DomPDF\Facade as PDF;

class PDFController extends Controller { public function generatePDF() { $data = [ 'title' => 'Sample PDF Document', 'content' => 'This is a sample PDF document generated with dynamic content in Laravel.' ];

    $pdf = PDF::loadView('pdf.document', $data);
    
    return $pdf->download('document.pdf');
}

}

  1. Create a view file for the PDF content (e.g., resources/views/pdf/document.blade.php), and add the dynamic content that you want to include in the PDF file:
  1. Finally, create a route in your Laravel application to access the "generatePDF" method in the controller:

Route::get('generate-pdf', 'PDFController@generatePDF');

Now, when you visit the /generate-pdf URL in your browser, it will generate a PDF file with the dynamic content specified in the controller and view file. You can customize the PDF layout and content as needed in the view file.

How to create a PDF file from a JSON response in Laravel?

To create a PDF file from a JSON response in Laravel, you can follow these steps:

  1. First, you need to install the dompdf package in your Laravel project. You can do this by running the following command in your project directory:

composer require dompdf/dompdf

  1. Next, you need to create a new controller in your Laravel project where you will handle the JSON response and generate the PDF file. You can do this by running the following command:

php artisan make:controller PDFController

  1. In the PDFController, you can create a method that will receive the JSON response, convert it to HTML, and then generate a PDF file using dompdf. Here is an example of how you can do this:

use Dompdf\Dompdf; use Dompdf\Options;

class PDFController extends Controller { public function createPDF() { $data = json_decode($jsonResponse, true);

    $html = '<h1>JSON Data</h1>';
    $html .= '<ul>';
    foreach ($data as $key => $value) {
        $html .= '<li>' . $key . ': ' . $value . '</li>';
    }
    $html .= '</ul>';

    $dompdf = new Dompdf();
    $options = new Options();
    $options->set('isHtml5ParserEnabled', true);
    $dompdf->setOptions($options);

    $dompdf->loadHtml($html);
    $dompdf->render();

    $dompdf->stream('json\_to\_pdf.pdf');
}

}

  1. Finally, you need to create a route in your routes/web.php file that will point to the createPDF method in your PDFController:

Route::get('/create-pdf', 'PDFController@createPDF');

Now, when you access the /create-pdf route in your browser, it will generate a PDF file from the JSON response and prompt you to download it.

How to render a blade template to PDF in Laravel?

To render a Blade template to PDF in Laravel, you can use the "dompdf" package which allows you to generate PDF files from HTML and CSS. Here is a step-by-step guide on how to render a Blade template to PDF:

Step 1: Install the dompdf package You can install the dompdf package in Laravel by running the following composer command: composer require dompdf/dompdf

Step 2: Create a Blade template Create a Blade template that you want to render to PDF. For example, you can create a template named "pdf-template.blade.php" in the "resources/views" directory.

Step 3: Create a PDF controller Create a new controller (e.g. PdfController) with a method that will render the Blade template to PDF. You can use the following code as an example:

namespace App\Http\Controllers;

use PDF; use Illuminate\Http\Request;

class PdfController extends Controller { public function exportPdf() { $data = [ 'title' => 'Sample PDF', 'content' => 'This is a sample PDF generated using dompdf in Laravel.' ];

    $pdf = PDF::loadView('pdf-template', $data);
    return $pdf->download('sample\_pdf.pdf');
}

}

Step 4: Create a route Create a route that will call the "exportPdf" method in the PdfController. You can add the following code to your routes file (e.g. web.php):

Route::get('export-pdf', 'PdfController@exportPdf');

Step 5: Generate the PDF You can now visit the route you have created (e.g. http://example.com/export-pdf) to generate the PDF from the Blade template.

That's it! You have successfully rendered a Blade template to PDF in Laravel using the dompdf package.