douba1067 2019-06-02 07:01
浏览 192

使用PHP查找PDF中的页数

I have researched this for a few days now and finally found something that seemed to work, but I am getting the wrong result. I need to count the number of pages in a PDF file on a remote server. My code opens the PDF, but it's not finding the correct number of pages and I'm not sure why.

Here is my code so far:

    $CI = &get_instance();
    $CI->load->library('Awss3', null, 'S3');
    $CI->load->library('Pdflib');
    $data = $CI->S3->readFile('uploads/225572/filename.pdf', false, 'bucket-name');
    $needle = 'Page';
    $positions = array();
    $lastPos = 0;
    while (($lastPos = strpos($data, $needle, $lastPos))!==false) {
        $positions[] = $lastPos;
        $lastPos = $lastPos + strlen($needle);
    }
    echo count($positions);
    foreach ($positions as $value) {
        echo $value . '<br />';
    }
    $test = strpos($data, 'Page');

If I echo out the $data, I get lots of symbols, etc. and some words, but the $test comes out to 0 when it should be 16. Does it depend on the type of PDF or do I need to decode it or something like that?

  • 写回答

1条回答 默认 最新

  • duanmu6231 2019-06-02 07:04
    关注

    Simplest of all is using ImageMagick here is a sample code

     $image = new Imagick();
     $image->pingImage('myPdfFile.pdf');
     echo $image->getNumberImages();
    

    otherwise you can also use PDF libraries like MPDF or TCPDF for PHP

    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000