top of page

How to Extract Any Text From a Pdf in Laravel

Updated: Mar 2, 2023

Portable Document Format (PDF) is a popular file format used for sharing documents that are often required to be printed or viewed in a particular layout. However, extracting text from a PDF can be a challenging task. Laravel, a PHP web application framework, provides various libraries and packages that can be used to extract text from PDFs easily. In this article, we will discuss how to extract text from a PDF file in Laravel.


Requirements

To extract text from a PDF file in Laravel, you will need the following requirements:

  1. Laravel Framework: You will need a Laravel web application framework installed on your system. You can download and install Laravel using the Composer package manager.

  2. spatie/pdf-to-text package: Laravel provides a package named "spatie/pdf-to-text" that can be used to extract text from PDF files. You can install this package using the Composer package manager.

  3. PDF file: You will need a PDF file from which you want to extract text. You can use any PDF file for this purpose.

  4. Code editor: You will need a code editor to write and edit the Laravel application code. You can use any code editor, such as Visual Studio Code, Sublime Text, or Atom.

  5. Web server: You will need a web server to run your Laravel application. You can use any web server, such as Apache, Nginx, or PHP built-in server.


Steps to extract text from a PDF file in Laravel:


STEP 1: Install the required package: Laravel provides a package named "spatie/pdf-to-text" that can be used to extract text from PDFs. To install this package, run the following command in your terminal.

composer require spatie/pdf-to-text

STEP 2: Create a controller: Create a new controller named "PdfController" using the following command:

php artisan make:controller PdfController

STEP 3: Write code to extract text: In the "PdfController" controller, write the following code to extract text from a PDF file:

use Spatie\PdfToText\Pdf;

public function extractText()
{
    $text = Pdf::getText('path/to/pdf/file.pdf');

    return view('pdf.text', ['text' => $text]);
}

The above code imports the "Pdf" class from the "spatie/pdf-to-text" package and defines a method named "extractText". The "extractText" method calls the "getText" method of the "Pdf" class and passes the path to the PDF file as an argument. The "getText" method returns the extracted text from the PDF file. The extracted text is then passed to the "text" view using the Laravel's view method.


STEP 4: Create a route: Create a route in the "web.php" file that maps to the "extractText" method of the "PdfController" controller.

Route::get('/pdf/extract-text', 'PdfController@extractText')->name('pdf.extract-text');

STEP 5: Create a view: Create a new view named "text.blade.php" in the "resources/views/pdf" directory. The view will display the extracted text from the PDF file.

<!DOCTYPE html><html><head><title>Extracted Text</title></head><body><div>
            {{ $text }}
        </div></body></html>

The above view displays the extracted text using the "{{$text}}" blade syntax.


Conclusion:

In this article, we have discussed how to extract text from a PDF file in Laravel using the "spatie/pdf-to-text" package. We have also provided step-by-step instructions on how to create a controller, route, and view to display the extracted text. By following these steps, you can easily extract text from any PDF file in your Laravel application.

0 comments
bottom of page