Home  >  Article  >  php教程  >  PHP获取PDF文件页数

PHP获取PDF文件页数

WBOY
WBOYOriginal
2016-06-07 11:42:232208browse

这个函数虽然看起来好像没多大用处,不过在有些时候还是能用的上的。正在建立自己函数库的可以收藏一下。


提示:这个函数不是我原创的,原创的地址在这里http://hi.baidu.com/luanxian/item/06ab9d24be2b4e1409750879,我稍微添加了两行判断错误的代码。
下面给出代码: /**
 * 获取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             }
        }
        fclose($fp);
        // 返回页数
        return array(true,$max);
    }
}
    /**
     * 测试代码
     */
    $results=getPdfPages("demo.pdf");
    if($results[0]){
        // 在这里放置成功读取后的处理代码
    }else{
        // 在这里放置失败的处理代码
    }
?>
性能方面,在我的ubuntu机器上读取1个600页左右的文件大概耗时1秒,应该是不会有太大问题。

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn