用PHP找出PDF,Docx,Doc,Ppt,Pptx文件的页码 [英] Find out page numbers of PDF, Docx, Doc, Ppt, Pptx files with PHP

查看:457
本文介绍了用PHP找出PDF,Docx,Doc,Ppt,Pptx文件的页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要在我的PHP应用程序中使用此功能:

I want this functionality in my PHP application:

当用户上传文档(PDF,DOCX,DOC,PPT,PPTC扩展名)时,上传后,用户将获得文档的总页数.

When user upload a document (PDF, DOCX, DOC, PPT, PPTC extensions) then after uploading user get the total number of pages of document.

但不使用exec()函数.

推荐答案

可以在PHP中直接执行某些格式. DOCx和PPTx很简单:

It is possible to do some formats right in PHP. The DOCx and PPTx are easy:

对于Word文件:

function PageCount_DOCX($file) {
    $pageCount = 0;

    $zip = new ZipArchive();

    if($zip->open($file) === true) {
        if(($index = $zip->locateName('docProps/app.xml')) !== false)  {
            $data = $zip->getFromIndex($index);
            $zip->close();
            $xml = new SimpleXMLElement($data);
            $pageCount = $xml->Pages;
        }
        $zip->close();
    }

    return $pageCount;
}

和PowerPoint

and for PowerPoint

function PageCount_PPTX($file) {
    $pageCount = 0;

    $zip = new ZipArchive();

    if($zip->open($file) === true) {
        if(($index = $zip->locateName('docProps/app.xml')) !== false)  {
            $data = $zip->getFromIndex($index);
            $zip->close();
            $xml = new SimpleXMLElement($data);
            print_r($xml);
            $pageCount = $xml->Slides;
        }
        $zip->close();
    }

    return $pageCount;
}

旧版Office文档是另一回事.您将在此处找到有关执行此操作的讨论:

Older Office documents are a different story. You'll find some discussion about doing that here: How to get the number of pages in a Word Document on linux?

对于PDF文件,我更喜欢使用FPDI,即使它需要许可才能解析较新的PDF文件格式.您可以像这样简单地使用它:

As for PDF files, I prefer to use FPDI, even though it requires a license to parse newer PDF file formats. You can use do it simply like this:

function PageCount_PDF($file) {
    $pageCount = 0;
    if (file_exists($file)) {
        require_once('fpdf/fpdf.php');
        require_once('fpdi/fpdi.php');
        $pdf = new FPDI();                              // initiate FPDI
        $pageCount = $pdf->setSourceFile($file);        // get the page count
    }
    return $pageCount;
}

这篇关于用PHP找出PDF,Docx,Doc,Ppt,Pptx文件的页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆