我正在使用PHP PDFMerger库:https://github.com/myokyawhtun/PDFMerger
我们经常使用它来下载大量客户文档。偶尔我会收到错误:
Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable|array, bool given in C:MyPathwwwPDFMergertcpdftcpdi_parser.php:503 Stack trace: #0 C:MyPathwwwPDFMergertcpdftcpdi_parser.php(373): tcpdi_parser->decodeXrefStream() #1 C:MyPathwww\PDFMergertcpdftcpdi_parser.php(212): tcpdi_parser->getXrefData() #2 C:MyPathwww\PDFMergertcpdftcpdi.php(121): tcpdi_parser->__construct() #3 C:MyPathwww\PDFMergertcpdftcpdi.php(88): TCPDI->_getPdfParser() #4 C:MyPathwww\PDFMergerPDFMerger.php(97): TCPDI->setSourceFile() #5 C:MyPathwww\offline downloadsdownload-DL100.php(75): PDFMergerPDFMerger->merge() #6 {main} thrown in C:MyPathwww\PDFMergertcpdftcpdi_parser.php on line 503
错误行的代码是:
$valid_crs = false;
$sarr = $xrefcrs[0][1];
$keys = array_keys($sarr);
$columns = 1; // Default as per PDF 32000-1:2008.
$predictor = 1; // Default as per PDF 32000-1:2008.
foreach ($keys as $k=>$key) {
$v = $sarr[$key];
if (($key == '/Type') AND ($v[0] == PDF_TYPE_TOKEN AND ($v[1] == 'XRef'))) {
$valid_crs = true;
} elseif (($key == '/Index') AND ($v[0] == PDF_TYPE_ARRAY AND count($v[1] >= 2))) {
// first object number in the subsection
$index_first = intval($v[1][0][1]);
// number of entries in the subsection
$index_entries = intval($v[1][1][1]);
} elseif (($key == '/Prev') AND ($v[0] == PDF_TYPE_NUMERIC)) {
// get previous xref offset
$prevxref = intval($v[1]);
} elseif (($key == '/W') AND ($v[0] == PDF_TYPE_ARRAY)) {
// number of bytes (in the decoded stream) of the corresponding field
$wb = array();
$wb[0] = intval($v[1][0][1]);
$wb[1] = intval($v[1][1][1]);
$wb[2] = intval($v[1][2][1]);
} elseif (($key == '/DecodeParms') AND ($v[0] == PDF_TYPE_DICTIONARY)) {
$decpar = $v[1];
foreach ($decpar as $kdc => $vdc) {
if (($kdc == '/Columns') AND ($vdc[0] == PDF_TYPE_NUMERIC)) {
$columns = intval($vdc[1]);
} elseif (($kdc == '/Predictor') AND ($vdc[0] == PDF_TYPE_NUMERIC)) {
$predictor = intval($vdc[1]);
}
}
} elseif ($filltrailer) {
switch($key) {
case '/Size':
case '/Root':
case '/Info':
case '/ID':
$xref['trailer'][1][$key] = $v;
break;
default:
break;
}
}
}
503行为:
} elseif (($key == '/Index') AND ($v[0] == PDF_TYPE_ARRAY AND count($v[1] >= 2))) {
我知道Count试图对非数组值进行计数。我试过使用is_countable((;并指定$v=array((;但收效甚微。
这只会在某些PDF中失败。通过阅读,我可以看到PDF合并并不是防弹的,失败确实会发生。然而,我想了解更多关于代码的信息,以确定是什么导致了错误;错误的代码或错误的PDF(导致意外情况(或两者兼有?
您正在尝试计算布尔值,即$v[1] >= 2
的结果
我想你可能需要编码
AND count($v[1]) >= 2