How to Read Content from PDF File in Laravel?

29-Dec-2023

.

Admin

How to Read Content from PDF File in Laravel?

Hello Dev,

Today, I will let you know an example of how to read content from a PDF file in Laravel 10. this example will help you how to read content from a PDF document in Laravel 10. you can see how to easily extract any text from a pdf in Laravel. if you want to see an example of how to show pdf files in Laravel Blade then you are in the right place.

This extensive guide will teach you how to read content from a PDF file in Laravel. I would like to share with you the process of reading a PDF file in Laravel. Here, you will learn how to read content from a PDF file using Laravel. I will demonstrate how to read the text from a PDF file in Laravel through the example below. Follow the steps outlined in the example to learn how to read text from a PDF file in Laravel.

If you want to read content from a PDF file in Laravel, I'll provide you with a simple example here. We will use the spatie/pdf-to-text


Composer package to read PDF files in a Laravel application.

So, let's follow the steps below to read a PDF file in Laravel.

Step 1: Install Laravel App

This step is not required; however, if you haven't created the Laravel app yet, feel free to execute the following command.

composer create-project laravel/laravel example-app

Step 2: Install spatie/pdf-to-text

Here, we will install the Laravel spatie/pdf-to-text package, which allows us to read PDF files. So, let's run the following command.

composer require spatie/pdf-to-text

Step 3: Install Requirements

Here, we are using the spatie/pdf-to-text package to read PDF files. This package requires the pdftotext system software, which you can install on your system or server using the following commands.

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 a route to invoke 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 file and add two methods, namely index(), to read the PDF file.

Ensure that you have sample-demo.pdf in your public folder; I have already provided it below.

Next, let's update the code in the Controller file with the following changes.

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 completed. Now, type the following command and press 'Enter' to run the Laravel app.

php artisan serve

Now, open your web browser, enter the provided URL, and view the app output.

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

Output

26/06/2023, 18:32

Discover Keyword Ideas

This is a 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...

#Laravel 10