Laravel

How to Read Content from a PDF File in Laravel?

Share your learning

So, the question is how to read Content from a PDF File in laravel and here is the solution.

People widely use PDF (Portable Document Format) files to share documents as they maintain the formatting across various platforms.

In web applications, there might be scenarios where you need to read the content from PDF files programmatically, such as extracting text for indexing, processing, or displaying information to users.

In this article, we will explore how to read content from PDF files in a Laravel application using different options.

Prerequisites

Before we begin, make sure you have the following prerequisites:

  1. Basic understanding of Laravel and PHP.
  2. Laravel installed on your system.
  3. Composer installed to manage PHP dependencies.
  4. A text editor or an Integrated Development Environment (IDE).

Let’s discuss the options available to read the content from a pdf file in laravel.

Option 1: Using “spatie/pdf-to-text” Package to read content from a pdf file in laravel

The “spatie/pdf-to-text” package allows us to extract text from PDF files using Laravel. It is built on top of the “poppler-utils” package, which must be installed on your server to use this method.

Step 1: Install the Package

To get started, install the “spatie/pdf-to-text” package via Composer by running the following command in your Laravel project directory:

composer require spatie/pdf-to-text

Step 2: Read Content from PDF

Next, you can create a Laravel route or a controller method to read the content from a PDF file. Here’s a basic example using a route:

use Spatie\PdfToText\Pdf;

Route::get('/read-pdf', function () {
    $pdfPath = storage_path('app/documents/sample.pdf');

    try {
        $text = Pdf::getText($pdfPath);
        return response($text, 200);
    } catch (\Exception $e) {
        return response('Error reading PDF: ' . $e->getMessage(), 500);
    }
});

In this example, we read the content from the “sample.pdf” file located in the “storage/app/documents/” directory. Make sure to replace this path with the actual path to your PDF file.

Option 2: Using “setasign/fpdi” Package

The “setasign/fpdi” package is another option to extract text from PDF files in Laravel. It allows you to import pages from existing PDF documents and use them as templates for creating new documents.

Step 1: Install the Package

Install the “setasign/fpdi” package via Composer by running the following command:

composer require setasign/fpdi

Step 2: Read Content from PDF

After installing the package, you can create a Laravel route or a controller method to read the content from a PDF file. Here’s an example using a route:

use setasign\Fpdi\Fpdi;

Route::get('/read-pdf', function () {
    $pdfPath = storage_path('app/documents/sample.pdf');

    try {
        $pdf = new Fpdi();
        $totalPages = $pdf->setSourceFile($pdfPath);
        $text = '';

        for ($pageNo = 1; $pageNo <= $totalPages; $pageNo++) {
            $template = $pdf->importPage($pageNo);
            $text .= $pdf->getText($template);
        }

        return response($text, 200);
    } catch (\Exception $e) {
        return response('Error reading PDF: ' . $e->getMessage(), 500);
    }
});

In this example, we iterate through each page of the PDF, import it as a template, and extract text using the “getText()” method.

Conclusion

Reading content from PDF files in Laravel can be achieved using various packages, each offering its own set of features and functionalities. In this article, we explored two options: “spatie/pdf-to-text” and “setasign/fpdi.” Depending on your requirements, you can choose the one that best suits your needs.

Remember to handle errors gracefully and implement proper error handling when working with PDF files. Now you can integrate PDF content extraction seamlessly into your Laravel application and process the data as needed. Happy coding!

Satpal

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago