How To Read PDF File Contents in Laravel 10 Example

Reading Time: 4 minutes
48 Views

Extracting text from PDF files is a common requirement in web applications for parsing and utilizing PDF content. In Laravel 10, utilizing the spatie/pdf-to-text package allows developers to effortlessly extract text from PDF documents, enabling seamless integration of PDF data into Laravel applications.

In this tutorial, we’ll see the comprehensive process of reading PDF file contents in Laravel 10 using the spatie/pdf-to-text package. This functionality empowers developers to extract textual data from PDF files, facilitating further processing or display within Laravel applications.

Read More: Work with VUE 3 Axios Get Request in Laravel 10 Example

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Installation “spatie/pdf-to-text” Package

Open project into terminal and run this command,

composer require spatie/pdf-to-text

It will install this required composer package into your system but this package also depends on pdftotext system software.

So, according to Operating System you have to install this.

For Ubuntu.

sudo apt-get install poppler-utils

For Mac.

brew install poppler

For RedHat, CentOS, Rocky Linux, or Fedora.

yum install poppler-utils

Read More: How To Use Laravel 10 Blade Components Example

Create PDF Controller

Open project terminal and run this command,

php artisan make:controller PDFController

It will create a controller class with name PDFController.php inside app/Http/Controllers folder.

Open controller class and write this complete code into it,

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Spatie\PdfToText\Pdf;

class PDFController extends Controller
{
    public function index()
    {
        $text = Pdf::getText(public_path('your_pdf_file.pdf'));
  
        print_r($text);
    }
}

Remember you have to place this file “your_pdf_file.pdf” into your /public folder of application.

Add Route

Open routes/web.php and add this route into it,

//...
use App\Http\Controllers\PDFController;

//...
Route::controller(PDFController::class)->group(function(){
    Route::get('read-pdf-file', 'index');
});

Application Testing

Run this command into project terminal to start development server,

php artisan serve

URL: http://127.0.0.1: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

Read More: How To Generate Test Users in Laravel 10 Tutorial

Example PDF File,

That’s it.

We hope this article helped you to learn about How To Read PDF File Contents in Laravel 10 Example in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more