Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip over corrupt files with PHP libraries TCPDF and FPDI by modifying error handling?

I am using the PHP libraries TCPDF and FPDI to combine PDF documents, and am getting the following error:

TCPDF ERROR: Unable to find object (10, 0) at expected location

I have the commercial version of FPDI.

It appears that the issue is only happening with PDF Version 1.3 (Acrobat 4.x) files. Here is a screenshot of a file's document properties that is creating the error. http://imagebin.org/215041

I'd like to skip over any files with errors instead of letting the script die. I have modified the error handling with a new class ErrorIgnoringTCPDF, however, it is not working.

Any ideas?

require_once('../../libraries/tcpdf/tcpdf.php');
require_once('../../libraries/fpdi/fpdi.php');

class ErrorIgnoringTCPDF extends FPDI {

   public function Error($msg) {
       // unset all class variables
       $this->_destroy(true);

       // exit program and print error
       //die('<strong>TCPDF ERROR: </strong>'.$msg);
   }

}

$pdf = new ErrorIgnoringTCPDF();
$pdf->setPrintHeader(false);

$prows = fetch_data($id);

foreach ($prows AS $row) {

    $irows = get_imaged_docs($row['pat_id']);

    foreach($irows AS $irow){

        if ($irow['type'] === 'application/pdf'){

            $doc_id = $irow['id'];

            $content = get_pdf_imaged_docs($doc_id);

            $pagecount = $pdf->setSourceFile($content);

            for ($i = 1; $i <= $pagecount; $i++) {
                 $tplidx = $pdf->ImportPage($i);
                 $s = $pdf->getTemplatesize($tplidx);
                 $pdf->AddPage('P', array($s['w'], $s['h']));
                 $pdf->useTemplate($tplidx);
            }    

        } else {

            $pdf->AddPage();

            $doc  = fetch_document_content($irow['id'], $irow['filename']);
            $img = base64_encode($doc);

            $imgdata = base64_decode($img);

            $pdf->Image('@'.$imgdata);

        }

    }

}

$pdf->Output('documents.pdf', 'D');
like image 366
Michael Avatar asked Jan 17 '23 12:01

Michael


1 Answers

If you are using Linux you can use shell_exec to combine files

function combine_pdf($outputName,$fileArray)
{


         $cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outputName ";

         foreach($fileArray as $file)
         {
           $cmd .= $file." ";
         }
         $result = shell_exec($cmd);

 }
like image 121
Harish U Warrier Avatar answered Jan 25 '23 15:01

Harish U Warrier