Error - when getting text from pdf file using smalot pdf parser in codeigniter-4

1k views Asked by At

I'm trying to upload a pdf file. It can be password protected or not.

But I receive this error:

Allowed memory size of 134217728 bytes exhausted on line ***print_r($pages);***

This however only happens on PDF files that aren't password protected. Although, the Smalot PDF parser works fine with password protected PDF files.

I already have a helper method removePdfPassword() for removing the password where necessary.

    removePdfPassword($dir . $_FILES["bsfile"]["name"][$i],   $_REQUEST['pdfpassword'][$i], $dir .$file_name);
    include 'public/pdfparser/vendor/autoload.php';
    $parser     = new \Smalot\PdfParser\Parser();
    $location   = $dir . $file_name;
        
    echo "<pre>";
    $pdf        = $parser->parseFile($location);
    $pages      = $pdf->getPages();
    print_r($pages);
    die;
1

There are 1 answers

1
steven7mwesigwa On

Instead of printing the whole file's pages at once, try doing it in bits. I.e:

Instead of:

// ...

 print_r($pages);

// ....

Use this:

// ...

foreach ($pages as $page) {
    echo "<div>" . $page->getText() . "</div>";
}

// ...