确定PDF文件是否可打开且未损坏 [英] determine if PDF file is openable and not corrupt

查看:117
本文介绍了确定PDF文件是否可打开且未损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在徘徊,如果有人能够可靠地确定PDF文档是否实际上是PDF文档,并且它没有损坏.

I am wandering if anybody has a reliable way of determine whether a PDF document is actually a PDF document, and that it isn't corrupted.

我在系统上生成报告,并且希望确定另一个系统返回的数据包含可打开的PDF文档(并且该数据没有损坏).

I generate reports on my system and I want to be certain that the data returned by another system contains an openable PDF document (and that the data is not corrupt).

目前,我基本上是在看字符串的长度(PDF被存储到变量中,而不是物理文件中). 任何在PHP中执行此操作的建议都很棒.

At the moment, I am basically looking at string length (the PDF gets stored into a variable, not a physical file). Any recommendations to do this in PHP would be great.

推荐答案

如果您只想确定文件是PDF文件,而无需检查它是否是完整无缺的pdf文件,则可以阅读第一个文件.文件的5个字节,对于PDF文件,它们将完全等于字符串%PDF-"

If you just want to make sure the file is a PDF file, without checking that it is a completely intact pdf file with no issues, you can read the first 5 bytes of the file and for a PDF file they will be exactly equal to the string "%PDF-"

这是Linux中的file程序识别PDF文件的方式.

This is how the file program in linux identifies PDF files.

但是,如果您要绝对确保文件中的任何地方都没有错误,则可以运行一个处理整个文件的程序,然后查看该程序是否返回成功.

But if you want to make absolutely sure there are no errors anywhere in the file, you can run a program that processes the entire file, and see if that program returns success.

在Linux中,您可以使用ghostscript("gs")将PDF文档呈现为任何格式.

In linux you can use ghostscript ("gs") to render the PDF document to any format.

或者您可以安装acrobat Reader,然后将acroread作为命令行程序运行以将其转换为后记:

Or you can install acrobat reader, and run acroread as a command line program to convert it to postscript:

acroread -print -toPostScript [your_file.pdf]

要执行上述任一操作,您将需要使用 system PHP函数.要检查程序是否成功运行,您需要在第二个参数中将一个变量传递给system,它将接收返回状态.

To do either of these you will need to use the system PHP function. To check of the program ran successfully, you need to pass a variable in the second parameter to system that will receive the return status.

这篇关于确定PDF文件是否可打开且未损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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