How to Read Content from PDF File in Laravel |
How to Read Content from PDF File in Laravel |

How to Read Content from PDF File in Laravel


Laravel, a popular PHP web application framework, provides a robust platform for building and managing web applications.Read Content from PDF File in Laravel can be a common requirement, especially when dealing with document-centric applications. In this article, we’ll explore a step-by-step guide on how to achieve this using Laravel.

Unlock the potential of your Laravel application by effortlessly reading PDF content using the “barryvdh/laravel-dompdf” package. With a step-by-step guide, this article empowers you to install, configure, and implement a solution, ensuring easy comprehension. Elevate your document-centric projects with this efficient PDF integration in Laravel.


Before we dive into the implementation, make sure you have the following prerequisites:

  1. Laravel Installed
  2. Composer Installed
  3. Basic understanding of Laravel and PHP

Step 1: Install Required Package

To read content from a PDF file, we’ll use a package called barryvdh/laravel-dompdf. Install it using Composer:

composer require barryvdh/laravel-dompdf

Step 2: Configure Package

Once installed, you need to configure the package. Open config/app.php and add the service provider and alias:

'providers' => [
    // ...

'aliases' => [
    // ...
    'PDF' => Barryvdh\DomPDF\Facade::class,

Step 3: Create Controller

Generate a controller that will handle PDF-related functionality:

php artisan make:controller PdfController

Open the generated PdfController.php file and add the following code:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class PdfController extends Controller
    public function readPdf()
        // Replace 'example.pdf' with your PDF file path
        $pdfContent = PDF::loadFile(public_path('example.pdf'))->getText();

        return view('', compact('pdfContent'));

Step 4: Create View

Create a Blade view file to display the extracted content. Run the following command to generate the view:

php artisan make:view

Open the generated resources/views/pdf/read.blade.php file and add the following code:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PDF Content</title>
    <h1>PDF Content</h1>
    <p>{{ $pdfContent }}</p>

Step 5: Define Route

Finally, define a route in routes/web.php to access the PDF content:

use App\Http\Controllers\PdfController;

Route::get('/read-pdf', [PdfController::class, 'readPdf']);

Step 6: Test the Implementation

Run your Laravel development server:

php artisan serve

Visit http://localhost:8000/read-pdf in your browser to see the extracted content from the PDF file.

Congratulations! You’ve successfully learned how to read content from a PDF file in Laravel using the barryvdh/laravel-dompdf package. Feel free to customize and enhance the implementation based on your specific requirements.

Leave a Reply