How to Extract Text from a PDF File in Laravel?

Spread the love

Hello Guys,

Discover the Complete Guide on How to Extract Text from a PDF File in Laravel

Learn how to effortlessly extract text from PDF files in Laravel with this comprehensive guide. In this resource, we will delve into the process of reading PDF content within a Laravel environment. Follow the example provided below to gain insights into reading text from PDF files using Laravel.

If you’re looking to read PDF content in Laravel, we’ve got a straightforward example to share with you. Our approach involves utilizing the spatie/pdf-to-text composer package to facilitate PDF file reading in your Laravel application.

Follow the steps below to seamlessly How to Extract Text from a PDF File in Laravel:

Also Read: How to Word limit of a string in Laravel?

Step 1: Install Laravel App

While this step is optional, if you haven’t yet created your Laravel app, feel free to proceed by running the following command:

composer create-project laravel/laravel example-app

Step 2: Install spatie/pdf-to-text

In this guide, we’ll demonstrate how to install the Laravel spatie/pdf-to-text package, which enables you to extract text from PDF files. To get started, execute the following command:

composer require spatie/pdf-to-text

Step 3: Install Requirements

In this tutorial, we’ll walk you through the process of using the spatie/pdf-to-text package to extract text from PDF files. To make this work, you’ll need to have the pdftotext system software installed on your system or server. To get started, please follow these commands:

Install the pdftotext system software using the following command:

Also Read: How can i the Get Last Record from Database in Laravel?

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 4: Add Route

Additionally, navigate to your 'routes/web.php‘ file and insert a route for invoking the PDF file code.

routes/web.php
<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\PDFController;
  
/* 
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::controller(PDFController::class)->group(function(){
    Route::get('read-pdf-file', 'index');
});

Step 5: Create PDFController

In this guide, we will demonstrate how to create a PDFController and incorporate two essential methods, namely index(), for reading PDF files.

Before proceeding, ensure that you have the “sample-demo.pdf” file located in your public folder, as provided below:

Now, let’s proceed with updating the code in the Controller File.

Also Read: How to INSTALL FONT AWESOME ICONS In Angular 16 with Example

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'));
  
        dd($text);
    }
     
}   

We trust you’ll discover our Laravel Blogs to be a valuable resource.

Leave a Comment