Portál AbcLinuxu, 30. dubna 2025 15:44
Zpracovat a zobrazit počet stránek v PDF dokumentu se může občas hodit, třeba pokud chceme u odkazu zobrazit nejen velikost daného PDF souboru, ale i počet stran. Jak tuto situaci řešit na úrovni PHP znázorňuje následující kód.
<?php function getPDFPages($file) { if(file_exists($file)) { if($handle = @fopen($file, "rb")) { $i = 0; while(!feof($handle)) { if($i > 0) $contents .= fread($handle, 8152); else { $contents = fread($handle, 1000); if(preg_match("/\/N\s+([0-9]+)/", $contents, $found)) { return $found[1]; } } $i++; } fclose($handle); if(preg_match_all("/\/Type\s*\/Pages\s*.*\s*\/Count\s+([0-9]+)/", $contents, $capture, PREG_SET_ORDER)) { $count = max($capture); return $count[1]; } } } return 0; } echo(getPDFPages("foo.pdf")); ?>
Tiskni
Sdílej:
return
a ten fclose()
až po něm ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.