How to Read Content from PDF File in Laravel?

 

Step 1: Install spatie/pdf-to-text

composer require spatie/pdf-to-text

Step 2: Install Requirements

For Ubuntu:

sudo apt-get install poppler-utils

For Mac:

brew install poppler

For RedHat, CentOS, Rocky Linux or Fedora:

yum install poppler-utils

Step 3: Add Route

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\PDFController;

Route::controller(PDFController::class)->group(function(){
    Route::get('read-pdf-file', 'index');
});

Step 4: Create PDFController

app/Http/Controllers/PDFController.php

<?php
namespace App\Http\Controllers;
       
use Illuminate\Http\Request;
use Spatie\PdfToText\Pdf;
    
class PDFController extends Controller
{     
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $text = Pdf::getText(public_path('sample-demo.pdf'));
  
    }  
} 

Run Laravel App:

php artisan serve 

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/read-pdf-file