如何通过修改错误处理来跳过PHP库TCPDF和FPDI的损坏文件?

Mic*_*ael 3 php pdf fpdf fpdi tcpdf

我使用PHP库TCPDFFPDI来组合PDF文档,我收到以下错误:

TCPDF错误:无法在预期位置找到对象(10,0)

我有FPDI的商业版本.

看来这个问题只发生在PDF版本1.3(Acrobat 4.x)文件中.以下是创建错误的文件文档属性的屏幕截图.http://imagebin.org/215041

我想跳过任何有错误的文件,而不是让脚本死掉.我已经使用新类修改了错误处理ErrorIgnoringTCPDF,但它无法正常工作.

有任何想法吗?

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');
Run Code Online (Sandbox Code Playgroud)

Har*_*ier 7

如果您使用的是Linux,则可以使用shell_exec来组合文件

function combine_pdf($outputName,$fileArray)
{


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

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

 }
Run Code Online (Sandbox Code Playgroud)