DEV Community

Cover image for How to read content from PDF document in Laravel 8

Posted on • Updated on

How to read content from PDF document in Laravel 8

In some of the cases there are requirement to read contents from PDF document and store it into our database, so in this blog post we are going to see how we can extract data from pdf document and store it.

I am going to use smalot/pdfparser package for content reading.
First we will create a migration file

php artisan make:migration File -mc
Enter fullscreen mode Exit fullscreen mode

It will Create 3 files
1 - Migration file
2 - File Model
3 - FileController

In migration file add below fields

Schema::create('files', function (Blueprint $table) {
            $table->string('orig_filename', 100)->nullable();
            $table->string('mime_type', 50)->nullable();
Enter fullscreen mode Exit fullscreen mode

Now we will install it using composer - run this command

composer require smalot/pdfparser
Enter fullscreen mode Exit fullscreen mode

Last thing we will create blade file file.blade.php to upload PDF file. I am using Tailwind Css.

    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Upload File') }}
    {{-- ex using component x-component_name --}}
    @if (Session::has('success'))
            {{ session()->get('success') }}
    @if (Session::has('error'))
            {{ session()->get('error') }}

    <div class="py-12">
        <div class="max-w-5xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 bg-white border-b border-gray-200">
                    <form action="{{ route('') }}" enctype="multipart/form-data"
                        <div class="mb-2"> <span>Attachments</span>
                                class="relative h-40 rounded-lg border-dashed border-2 border-gray-200 bg-white flex justify-center items-center hover:cursor-pointer">
                                <div class="absolute">
                                    <div class="flex flex-col items-center "> <i
                                            class="fa fa-cloud-upload fa-3x text-gray-200"></i>
                                        <span class="block text-gray-400 font-normal">Attach
                                            you files here</span> <span
                                            class="block text-gray-400 font-normal">or</span>
                                        <span class="block text-blue-400 font-normal">Browse
                                <input type="file" class="h-full w-full opacity-0" name="file" >
                        <div class="mt-3 text-center pb-3">
                            <button type="submit"
                                class="w-full h-12 text-lg w-32 bg-blue-600 rounded text-white hover:bg-blue-700">
Enter fullscreen mode Exit fullscreen mode


use App\Http\Controllers\FileController;
Route::get('file', [FileController::class, 'index'])->name('file');
Route::post('file', [FileController::class, 'store'])->name('');
Enter fullscreen mode Exit fullscreen mode

We have now our migration and blade file ready. Now we will see the actual logic in our FileController

use Smalot\PdfParser\Parser;
use App\Models\File;

class FileController extends Controller
   public function index() { 
      return view('file');
   public function store(Request $request) {

        $file = $request->file;

            'file' => 'required|mimes:pdf',

        // use of pdf parser to read content from pdf 
        $fileName = $file->getClientOriginalName();

        $pdfParser = new Parser();
        $pdf = $pdfParser->parseFile($file->path());
        $content = $pdf->getText();

       $upload_file = new File;
       $upload_file->orig_filename = $fileName;
       $upload_file->mime_type = $file->getMimeType();
       $upload_file->filesize = $file->getSize();
       $upload_file->content = $content;
       return redirect()->back()->with('success', 'File  submitted');
Enter fullscreen mode Exit fullscreen mode

Hooray, we have read and stored contents from our PDF document to database.

Happy Reading 😃😍

Discussion (3)

mnaderian profile image
Mahdi Naderian • Edited on

Dude, the first command should have been like this:
php artisan make:model File -mc

And when a column is nullable() you don't need to give it default(null).

snehalk profile image
Snehal Author • Edited on

Thank you, I have changed it 🙂

gene222arch profile image

This is pretty handy