Hello Geek, here are the steps on how to read content from a PDF file in Laravel 10:
- Install the
spatie/pdf-to-text
package.
Code snippet
composer require spatie/pdf-to-text
- Create a new controller.
PHP
php artisan make:controller PdfController
- Add the following code to the
PdfController
class.
PHP
use Spatie\PdfToText\Pdf;
class PdfController extends Controller
{
public function index()
{
$pdf = new Pdf('path/to/pdf.pdf');
$text = $pdf->getText();
return view('pdf', [
'text' => $text,
]);
}
}
- Create a new view file called
pdf.blade.php
.
Blade
<!DOCTYPE html>
<html>
<head>
<title>Read PDF File in Laravel</title>
</head>
<body>
<h1>PDF Content</h1>
<p>{{ $text }}</p>
</body>
</html>
- Run the
php artisan serve
command to start the Laravel development server. - Visit
http://localhost:8000/pdf
in your web browser.
The above code will extract the text from the PDF file and display it in the browser.
Here are some additional notes:
- The
path/to/pdf.pdf
path should be the absolute path to the PDF file that you want to extract text from. - You can use the
spatie/pdf-to-text
package to extract text from PDF files in any Laravel version.
All the best nerd!