PHP 讀取PDF文檔頁數

<?php
/**
 * 獲取PDF文件頁數的函數獲取
 * 文件應當對當前用戶可讀(linux下)
 * @param  [string] $path [文件路徑]
 * @return [array]        [數組第一位表示成功與否,第二位表示提示信息]
 */
function getPdfPages($path){
    if(!file_exists($path)) return array(false,"文件\"{$path}\"不存在!");
    if(!is_readable($path)) return array(false,"文件\"{$path}\"不可讀!");
    // 打開文件
    $fp=@fopen($path,"r");
    if (!$fp) {
        return array(false,"打開文件\"{$path}\"失敗");
    }else {
        $max=0;
        while(!feof($fp)) {
            $line = fgets($fp,255);
            if (preg_match('/\/Count [0-9]+/', $line, $matches)){
                preg_match('/[0-9]+/',$matches[0], $matches2);
                if ($max<$matches2[0]) $max=$matches2[0];
            }
        }
        fclose($fp);
        // 返回頁數
        return array(true,$max);
    }
}
    /**
     * 測試代碼
     */
    $results=getPdfPages("demo.pdf");
    if($results[0]){
        // 在這裏放置成功讀取後的處理代碼
    }else{
        // 在這裏放置失敗的處理代碼
    }
?>
相關文章
相關標籤/搜索