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

如何通过修改错误处理跳过PHP库TCPDF和FPDI中损坏的文件?,php,pdf,tcpdf,fpdf,fpdi,Php,Pdf,Tcpdf,Fpdf,Fpdi,我正在使用PHP库和组合PDF文档,并得到以下错误: TCPDF错误:无法在预期位置找到对象(10,0) 我有商业版的 似乎只有PDF版本1.3(Acrobat 4.x)文件才会出现此问题。下面是创建错误的文件的文档属性的屏幕截图 我想跳过任何有错误的文件,而不是让脚本死掉。我已经用一个新类修改了错误处理ErrorIgnoringTCPDF,但是它不起作用 有什么想法吗 require_once('../../libraries/tcpdf/tcpdf.php'); require_once('

我正在使用PHP库和组合PDF文档,并得到以下错误:

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

我有商业版的

似乎只有PDF版本1.3(Acrobat 4.x)文件才会出现此问题。下面是创建错误的文件的文档属性的屏幕截图

我想跳过任何有错误的文件,而不是让脚本死掉。我已经用一个新类修改了错误处理
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');
require_once('../../libraries/tcpdf/tcpdf.php');
需要_once('../../libraries/fpdi/fpdi.php');
类ErrorIgnoringTCPDF扩展FPDI{
公共函数错误($msg){
//取消设置所有类变量
$this->\u销毁(真);
//退出程序和打印错误
//模具(“TCPDF错误:”.$msg);
}
}
$pdf=新的ErrorIgnoringTCPDF();
$pdf->setPrintHeader(假);
$prows=获取数据($id);
foreach($rows作为$row){
$irows=获取图像文档($row['pat\u id']);
foreach($irows作为$irow){
如果($irow['type']='application/pdf'){
$doc_id=$irow['id'];
$content=get\u pdf\u imaged\u docs($doc\u id);
$pagecount=$pdf->setSourceFile($content);
对于($i=1;$i进口页($i);
$s=$pdf->getTemplatesize($tplidx);
$pdf->AddPage('P',数组($s['w'],$s['h']);
$pdf->useTemplate($tplidx);
}    
}否则{
$pdf->AddPage();
$doc=fetch_document_content($irow['id'],$irow['filename']);
$img=base64_编码($doc);
$imgdata=base64炣decode($img);
$pdf->Image('@'.$imgdata);
}
}
}
$pdf->Output('documents.pdf',D');

这只是表示PDF文档有错误。它指向未找到预期对象的特定字节偏移位置。

您尝试过抑制错误吗

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

if (empty($pagecount))
    continue;  // or whatever you want to do, maybe set $is_invalid = true;

我不会说这是一个合适/最好的解决方案,但它可能会解决您的问题

在:pdf_parser.php中,注释掉以下行:

$this->error("Unable to find object ({$obj_spec[1]}, {$obj_spec[2]}) at expected location");
应该在544号线附近

您可能还需要更换:

    if (!is_array($kids))
        $this->error('Cannot find /Kids in current /Page-Dictionary');
与:

在fpdi_pdf_parser.php文件中


希望有帮助。它对我有用。

如果您使用Linux,您可以使用shell\u 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);

 }

我也有同样的问题,我正在使用此代码修复我的问题

class convertPDF extends FPDI {

   public function error($msg) {
      throw new Exception($msg); 
   }
   ...other stuff...
}

try {
    $convertPdf = new convertPDF();
} catch(Exception $e) {
    die($e->getMessage);
}

此答案适用于搜索此问题的用户。祝您好运!

如果没有代表性示例,将很难提供帮助…以下是导致问题的其中一个文件的PDF属性屏幕截图:缺少的“预期对象”是什么?
class convertPDF extends FPDI {

   public function error($msg) {
      throw new Exception($msg); 
   }
   ...other stuff...
}

try {
    $convertPdf = new convertPDF();
} catch(Exception $e) {
    die($e->getMessage);
}