我正在使用PHP库TCPDF和FPDI来合并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');
如果你使用的是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);
}
您是否尝试过仅抑制错误?
$pagecount = @$pdf->setSourceFile($content);
if (empty($pagecount))
continue; // or whatever you want to do, maybe set $is_invalid = true;
这仅表示 PDF 文档有错误。它指向找不到预期对象的特定字节偏移位置。
我不会说这是一个合适/最好的解决方案,但它可能会解决您的问题,
在: 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文件中
希望有帮助。它对我有用。
我有同样的问题,我正在使用这段代码来解决我的问题。
class convertPDF extends FPDI {
public function error($msg) {
throw new Exception($msg);
}
...other stuff...
}
try {
$convertPdf = new convertPDF();
} catch(Exception $e) {
die($e->getMessage);
}
这个答案适用于搜索此问题的人。祝你好运!