smalot / pdfparser

PdfParser, a standalone PHP library, provides various tools to extract data from a PDF file.
GNU Lesser General Public License v3.0
2.41k stars 537 forks source link

Fatal error: Cannot declare class Smalot\PdfParser\Parser, because the name is already in use in \libs\pdfparser-master\src\Smalot\PdfParser\Parser.php on line 49 #728

Closed bhaktvanti closed 3 months ago

bhaktvanti commented 3 months ago

Description:

I included the file alt_autoload.php-dist without composer. It throws below error

Fatal error: Cannot declare class Smalot\PdfParser\Parser, because the name is already in use in \libs\pdfparser-master\src\Smalot\PdfParser\Parser.php on line 49

PDF input

Expected output & actual output

Code

require dirname(__FILE__).'/libs/pdfparser-master/alt_autoload.php-dist';
require dirname(__FILE__) . "/libs/pdfparser-master/src/Smalot/PdfParser/Parser.php";

$parser = new Parser(); 
// Parse the PDF file
$pdf = $parser->parseFile('PS Spotlight Newsletter Design.pdf');

// Retrieve PDF details
$details = $pdf->getDetails();

// Get the custom properties
$customProperties = [];
foreach ($details as $property => $value) {
    // Custom properties usually start with "Custom"
   // if (strpos($property, 'Searchable') === 0) {
    if (strpos(strtolower($property), 'searchable') !== false) {
        $customProperties[$property] = $value;      
    }
} 
k00ni commented 3 months ago

When using alt_autoload.php-dist there is no need to manually include Parser.php too, because the autoload-file already includes all required PDFParser classes.

Remove the following line in your code and it should work:

require dirname(__FILE__) . "/libs/pdfparser-master/src/Smalot/PdfParser/Parser.php";
k00ni commented 3 months ago

Reopen if the problem persists.