Search code examples
laravelpdftotext

Extract text from pdf not working in Laravel


I am trying to extract some text from a PDF file in Laravel using Spatie-PdfToText. However, I get the error no such file or directory. The function is in this path app\Http\Controllers\AddRecordsController and the PDF in this path \app\Http\Controllers\book.pdf.

I've also tried changing the path in getText() to the path written above but same error.

Code to extract:

    public function readPDF(){

        echo Pdf::getText('book.pdf'); //returns the text
    }

Can anyone assist me?


Solution

  • in first you need instal binary library. if you use Ubuntu or Debian:

    apt-get install poppler-utils
    

    or if use RedHat or CentOS:

    yum install poppler-utils
    

    After it install library via composer:

    composer require spatie/pdf-to-text
    

    Then use it in your controller:

    use Spatie\PdfToText\Pdf;
    ...
    echo Pdf::getText('/path/to/file.pdf');