rguedes / PDFMerger

PDFMerger compatible with PHP5 for laravel
34 stars 43 forks source link

Unable to find object (4,0) -> PDF above or equal to 1.7 #10

Closed Arachnos closed 6 years ago

Arachnos commented 6 years ago

Hello, I have a problem with some PDFs when merging : an exception error is returning "Unable to find object (4,0)".

What can you do to ignore this message ? Some sites are telling that it appears when a file is corrupted : https://stackoverflow.com/questions/26874989/fpdi-fatal-error-uncaught-exception-exception-with-message-unable-to-find-ob

Thanks for solving this issue.

Arachnos commented 6 years ago

I've found the error : the file is in PDF 1.7

I've sold this using xthiago/pdf-version-converter

use Xthiago\PDFVersionConverter\Guesser\RegexGuesser;

use Symfony\Component\Filesystem\Filesystem,
    Xthiago\PDFVersionConverter\Converter\GhostscriptConverterCommand,
    Xthiago\PDFVersionConverter\Converter\GhostscriptConverter;

class GSConverterCommand extends GhostscriptConverterCommand
{
    public function __construct()
    {
        if(preg_match('/darwin/i', php_uname('s')))
        {
            $this->baseCommand = '/usr/local/bin/gs -sDEVICE=pdfwrite -dCompatibilityLevel=%s -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -dColorConversionStrategy=/LeaveColorUnchanged -dEncodeColorImages=true -dEncodeGrayImages=true -dEncodeMonoImages=true -dDownsampleMonoImages=false -dDownsampleGrayImages=false -dDownsampleColorImages=false -dAutoFilterColorImages=false -dAutoFilterGrayImages=false -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode -sOutputFile=%s %s';
        } else {
            $this->baseCommand = 'gs -sDEVICE=pdfwrite -dCompatibilityLevel=%s -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -dColorConversionStrategy=/LeaveColorUnchanged -dEncodeColorImages=true -dEncodeGrayImages=true -dEncodeMonoImages=true -dDownsampleMonoImages=false -dDownsampleGrayImages=false -dDownsampleColorImages=false -dAutoFilterColorImages=false -dAutoFilterGrayImages=false -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode -sOutputFile=%s %s';
        }
    }
}

class GSConverter extends GhostscriptConverter
{
    function clean()
    {
        $tmp_files = sys_get_temp_dir().'/pdf_version_changer_';
        foreach (glob($tmp_files . '*\.pdf') as $tmpFile) {
            unlink($tmpFile);
        }
    }
}

And for the conversion

                // Get version of the PDF
                $guesser = new RegexGuesser();
                $pdf_version = $guesser->guess($pdf_file);

                $cmd = new GSConverterCommand();
                $fs = new Filesystem();
                $converter = new GSConverter($cmd, $fs);

                // Copy file (if you don't want to overwrite)
                $new_file = preg_replace('/.pdf/i', '_converted.pdf', $pdf_file);
                copy($pdf_file, $new_file);

                $converter->convert(realpath($new_file), '1.5');
                $converter->clean(); // Clean temporary copy in tmp/ folder
                $pdf_file = $new_file;