How to Read Content from PDF File in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Array Length Validation Example
- Laravel 10 Generate PDF and Send Email Example
- Laravel 10 REST API Authentication using Sanctum Tutorial
- Laravel TCPDF: Generate HTML to PDF File Example
- How to set CC And BCC Email Address In Laravel Mail?
- How to Write Text on Existing PDF File in Laravel?
- Laravel Install Tailwind CSS Example
- How to Merge Multiple PDF Files in Laravel 9?
- How to Add Password Protection for PDF File in Laravel?
- Laravel Convert PDF to Image Example
- Laravel Eloquent updateOrCreate Example
- How to Generate PDF and Send Email in Laravel?
- Laravel 11 Merge Multiple PDF Files Example