Tutorial Details

How to Read Content from PDF File in Laravel
25 Nov

How to Read Content from PDF File in Laravel

Hi Guys,

This extensive guide will teach you how to read content from PDF files in Laravel. I would like to share with you how to read pdf files in Laravel. Here you will learn Laravel read content from a pdf file. I would like to show you read PDF file text in Laravel. follow the below example for how to read text from a PDF file in Laravel.

If you want to read content from a pdf file in Laravel then I will give you a simple example here. we will use spatie/pdf-to-text composer package to read pdf files in the Laravel application.

so, let's follow the below steps to read the pdf file in Laravel.

Step 1: Install Laravel App

This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command: 

composer create-project laravel/laravel example-app

Step 2: Install spatie/pdf-to-text

Here, we will install a Laravel spatie/pdf-to-text package that allows us to read a pdf file. so, let's run the following command:

composer require spatie/pdf-to-text

Step 3: Install Requirements

Here, we used spatie/pdf-to-text package to read a pdf file. this package needs pdftotext system software. so you need to install it in your system or server with the following commands. so, let's run the following command:

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

Furthermore, open the routes/web.php file and add one route to call 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 step, we will create a new PDFController; in this file, we will add two method index() to read a pdf file.

Make sure you have sample-demo.pdf in your public folder. i have already given bellow:

next, let's update the following code to Controller File.

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);
    }
     
}   

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the 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

I hope it can help you...

0 Comments

Leave a Comment