首页 文章

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

提问于
浏览
3

我使用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');

5 回答

  • 0

    如果您使用的是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);
    
     }
    
  • 1

    你试过压制错误吗?

    $pagecount = @$pdf->setSourceFile($content);
    
    if (empty($pagecount))
        continue;  // or whatever you want to do, maybe set $is_invalid = true;
    
  • 0

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

  • 7

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

    在: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');
    

    有:

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

    在fpdi_pdf_parser.php文件中

    希望有所帮助 . 它对我有用 .

  • 0

    我有同样的问题,我正在使用此代码来解决我的问题 .

    class convertPDF extends FPDI {
    
       public function error($msg) {
          throw new Exception($msg); 
       }
       ...other stuff...
    }
    
    try {
        $convertPdf = new convertPDF();
    } catch(Exception $e) {
        die($e->getMessage);
    }
    

    这个答案适用于搜索此问题的人 . 祝你好运!

相关问题