How to Read Content from PDF File in Laravel?

By Hardik Savani November 5, 2023 Category : Laravel

Hey Developer,

This extensive guide will teach you how to read content from pdf file in laravel. 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.

so, let's follow the below step to read 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 laravel spatie/pdf-to-text package that allow to read pdf file. so, let's run following command:

composer require spatie/pdf-to-text

Step 3: Install Requirements

Here, we used spatie/pdf-to-text package for read pdf file. this package need pdftotext system software. so you need to install in your system or server with following commands. so, let's run 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 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 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 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 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

Output:

26/06/2023, 18:32

Discover Keyword Ideas

This is demo file

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad

minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit

in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia

deserunt mollit anim id est laborum.

localhost:8000/discover-keywords

PDF File:

I hope it can help you...

Shares