Laravel 10 Read Content from PDF File in Laravel?

Hello Artisan, today I'll show you how to Read Content from PDF File in our Laravel application. I would like to share with you how to read pdf file in laravel. Here you will learn laravel read content from pdf file. I would like to show you read pdf file text in laravel. follow the below example for how to read text from pdf file in laravel.

If you want to read content from pdf file in laravel then i will give you simple example here. we will use spatie/pdf-to-text composer package to read pdf file in laravel application.

let's go through the steps to read a PDF file in Laravel using the "spatie/pdf-to-text" composer package.

Table of Contents

  1. Install Laravel App
  2. Install spatie/pdf-to-text
  3. Install Requirements
  4. Create PdfController
  5. Add Route
  6. Run Laravel App

Install Laravel

Creating the Laravel app is not necessary for this step, but if you haven't done it yet, you can proceed by executing the following command

composer create-project laravel/laravel PDF-Read

Install spatie/pdf-to-text

Here, we will install laravel spatie/pdf-to-text package that allow to read pdf file. so, let's run following command.

composer require spatie/pdf-to-text

Install Requirements

To utilize the "spatie/pdf-to-text" package for reading PDF files in Laravel, it's important to note that the package relies on the "pdftotext" system software. Therefore, before proceeding with the package, you must ensure that "pdftotext" is installed on your system or server. To install it, you can follow the commands provided below.

For Ubuntu:
sudo apt-get install poppler-utils
For Mac:
brew install poppler
For RedHat, CentOS, Rocky Linux or Fedora:
yum install poppler-utils

Create PDFController

In this step, we will create a new PdfController; in this file, we will add two method index() to read pdf file.

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

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

app/Http/Controllers/PDFController.php
<?php

namespace App\Http\Controllers;

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

}

Add Route

Furthermore, open routes/web.php file and add one route to call 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 and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('read-pdf-file',[PdfController::class,'index'])->name('generatePDF');

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

Output

That's it for today. I hope it'll be helpful in upcoming project. You can also download this source code from GitHub. Thanks for reading. ๐Ÿ™‚