简体   繁体   English

保存文件之前上传时,PDF文件上的php计数页数

[英]Php count number of pages on PDF file upon upload prior to saving file

I have a function that uploads a file into a web storage and prior to saving the file on the storage system if the file is a pdf file i would like to determine how many pages a pdf file has. 我有一个函数可以将文件上传到网络存储中,如果文件是pdf文件,则在将文件保存到存储系统之前,我想确定pdf文件有多少页。

Currently i have the following: 目前我有以下内容:

    $pdftext = file_get_contents($path);
    $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
    return $num;

Where $path is the temporary path that i use with fopen to open the document $ path是我与fopen一起使用以打开文档的临时路径

This function works at times but is not reliable. 此功能有时会起作用,但并不可靠。 I know theres also this function 我知道这里也有这个功能

exec('/usr/bin/pdfinfo '.$pdf_file.' | awk \'/Pages/ {print $2}\'', $output);

But this requires the file to donwloaded on the server. 但这需要将文件下载到服务器上。 Any ideas or suggestions to accomplish this? 有什么想法或建议可以做到这一点?

PHP is a server-side language, meaning all processing happens on your server. PHP是一种服务器端语言,这意味着所有处理都在您的服务器上进行。 There's no way for PHP to determine details of a file on the client side, it has no knowledge of it neither the required access to it. PHP无法在客户端确定文件的详细信息,既不了解文件,也不了解对文件的访问权限。

So the answer to your question as it is now is: It's not possible. 因此,您现在的问题的答案是:不可能。 But you probably have a goal in mind why you want to check this, sharing this goal might help to get more constructive answers/suggestions. 但是您可能会想到一个目标,为什么要检查这一点,分享这个目标可能有助于获得更具建设性的答案/建议。

As Oldskool already explained this is not possible with PHP on the client side. 正如Oldskool已经解释的那样,客户端PHP不可能做到这一点。 You would have to upload the PDF file to the server and then determine the amount of pages. 您将必须将PDF文件上传到服务器,然后确定页面数。 There are libraries and command line tools that could accomplish this. 有一些库和命令行工具可以完成此任务。

In case you don't want to upload the PDF file to the server (which seems to be the case here) you could use the pdf.js library. 如果您不想将PDF文件上传到服务器(此处似乎是这种情况),则可以使用pdf.js库。 Now the client is able to determine the amount of pages in a PDF document on its own. 现在,客户可以自行确定PDF文档中的页面数量。

PDFJS.getDocument(data).then(function (doc) {
    var numPages = doc.numPages;
}

There are other libraries as well but I'm not certain about their browser support ( http://www.electronmedia.in/wp/pdf-page-count-javascript/ ) 也有其他库,但我不确定它们对浏览器的支持( http://www.electronmedia.in/wp/pdf-page-count-javascript/

Now you just submit the amount of pages from javascript to your php file that needs this information. 现在,您只需将javascript的页面数量提交到需要此信息的php文件即可。 In order to achive this you simply use ajax. 为了达到这个目的,您只需使用ajax。 In case you don't know ajax, just google it there are enough examples out there. 如果您不了解Ajax,只需在Google上搜索就可以了。

As a side note; 作为旁注; Always remember to not trust the client. 永远记住不要信任客户。 The client is able to modify the page count and send a completely different one. 客户端能够修改页数并发送完全不同的页数。

For those of you running linux servers this actually is possible. 对于那些运行linux服务器的人来说,这实际上是可行的。 You need the pdfinfo extension installed and using the function 您需要安装pdfinfo扩展名并使用该功能

  $pages = exec('/usr/bin/pdfinfo '.$pdf_file.' | awk \'/Pages/ {print $2}\'', $output);

outputs the correct page number where $pdf_file is the temporary path on the server upon upload. 输出正确的页码,其中$ pdf_file是上传时服务器上的临时路径。

The reason it wasnt working for me was because i didnt have the PDFinfo installed. 它对我不起作用的原因是因为我没有安装PDFinfo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM