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.42k stars 538 forks source link

Usage of the "@" character in the code - error suppression #129

Open rgozdzialski opened 8 years ago

rgozdzialski commented 8 years ago

Long story short, one of the PDF files I'm trying to parse throws an error - it basically cannot be parsed. Thing is that I cannot catch it with the standard try-catch block, because the parseFile() method from your package suppresses any errors thrown while parsing:

public function parseFile($filename)
{
    $content = file_get_contents($filename);
    return @$this->parseContent($content);
}

And the line in the Parser.php file: https://github.com/smalot/pdfparser/blob/master/src/Smalot/PdfParser/Parser.php#L74

I would say remove the @. And I would do it everywhere else you might have used it. What do you think?

smalot commented 8 years ago

Warnings cleaned by "@" are mainly generated by "TCPDF" library. I'm surprised you can't catch warning with a standard try-catch block. You are not supposed to catch warning or notice using try/catch. Are you sure everything is solved when you remove "@" from the API ?

rgozdzialski commented 8 years ago

Hm, you are right, it goes deeper. Removing the @ did not resolve the problem. The stack looks like this:

in tcpdf_filters.php line 357
at FatalErrorException->__construct() in HandleExceptions.php line 133
at HandleExceptions->fatalExceptionFromError() in HandleExceptions.php line 118
at HandleExceptions->handleShutdown() in HandleExceptions.php line 0
at gzuncompress() in tcpdf_filters.php line 357
at TCPDF_FILTERS::decodeFilterFlateDecode() in tcpdf_filters.php line 94
at TCPDF_FILTERS::decodeFilter() in tcpdf_parser.php line 781
at TCPDF_PARSER->decodeStream() in tcpdf_parser.php line 702
at TCPDF_PARSER->getIndirectObject() in tcpdf_parser.php line 123
at TCPDF_PARSER->__construct() in Parser.php line 88
at Parser->parseContent() in Parser.php line 74

And the line 357 in tcpdf_filter.php:

public static function decodeFilterFlateDecode($data) {
    // initialize string to return
    $decoded = @gzuncompress($data); // <= Breaks here
    if ($decoded === false) {
        self::Error('decodeFilterFlateDecode: invalid code');
    }
    return $decoded;
}

As you can see, the line is also suppressed, but it's not your doing of course. I think you can close the issue, however I still have no idea how I am supposed to catch this (apparently silenced) error.