使用PHP,如何检查PDF文件是否有错误 [英] With PHP, how can I check if a PDF file has errors

查看:276
本文介绍了使用PHP,如何检查PDF文件是否有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用PHP/MySql构建的数据库系统.我对此很陌生.该系统允许用户上载发票.其他人则允许支付发票.会计人员上载支票.支票上载后,它会生成一个PDF封面,然后使用PDFTK(使用Ben Squire的PDFTK-PHP-Library)将所有文件组合在一起,并向用户展示要下载的单个PDF.

I have a DB system built in PHP/MySql. I'm fairly new at this. The system allows the user to upload an invoice. Others give permission to pay the invoice. The accounting person uploads the check. After check is uploaded, it generates a PDF as a cover, then uses PDFTK (using Ben Squire's PDFTK-PHP-Library) to combine all of the files together and present the user with a single PDF to download.

某些用户上载PDF文件,当它尝试将PDF与其他文件合并时,PDFTK会无限期挂起(但大多数情况下可以正常工作).没有返回的错误,只是挂起.为了重新使用该系统,用户必须清除缓存并重新登录.服务器没有记录任何错误消息,它只是冻结.我可以在使用Acrobat进行查看的文件中发现可以使用或无法使用的文件的唯一区别是,坏文件的大小是合法的(8.5 x 14)...但是,如果我创建自己的合法大小的文件并尝试使用该文件,它工作正常.

Some users upload PDF files which cause PDFTK to hang indefinitely when it tries to combine the PDF with others (but most of the time it works fine). No returned error, just hangs. In order to get back onto the sytem, user must clear cache and re-log in. There are no error messages logged by the server, it just freezes. The only difference I can find in the files that do or do not work in looking at them with Acrobat is that the bad files are legal sized (8.5 x 14) ... but if I create my own legal sized file and try that, it works fine.

使用腻子我去了命令行并复制了相同的问题,PDFTK无法读取文件,它也挂在命令行上.我尝试使用PDFMerge,该PDFMerge使用FPDF合并文件并同时收到文件错误(我从中得到的错误是: FPDF错误:无法在预期位置找到对象(4,0)).在命令行上,我能够使用ImageMagick将PDF转换为JPG,但是它给了我一个错误:警告:文件具有无效的外部参照条目:2.重建外部参照表."然后将其转换为jpg,但给出其他一些不太有用的警告.

Using Putty I've gone to command line and replicated the same problem, PDFTK can't read the file, it hangs on the command line as well. I tried using PDFMerge which uses FPDF to combine the files and get an error with the file as well (The error I get back from this is: FPDF error: Unable to find object (4, 0) at expected location). On the command line I was able to use ImageMagick to convert PDF to JPG, but it gives me an error: "Warning: File has an invalid xref entry: 2. Rebuilding xref table." and then it converts it to a jpg but gives a few other less helpful warnings.

如果我可以使PHP在不挂起系统的情况下检查PDF文件以确定是否有效,则可以使用ImageMagick转换文件,然后将其转换回PDF,但是我不想这样做所有的文件.如何上传文件时检查文件的有效性,以查看是否需要转换而不会导致系统挂起?

If I could get PHP to check the PDF file to determine if is valid without hanging the system, I could use ImageMagick to convert the file and then convert it back to a PDF, but I don't want to do this to all files. How can I get it to check the validity of the file when uploaded to see if it needs to be converted without causing the system to hang?

这里是指向引起问题的文件的链接: http://www.cssc-testing.org/accounting/school_9/20130604-a1atransportation-1.pdf

Here is a link to a file that is causing problems: http://www.cssc-testing.org/accounting/school_9/20130604-a1atransportation-1.pdf

在此先感谢您提供的任何指导!

Thanks in advance for any guidance you can offer!

我的代码(我是新手,我猜这不是很干净):

My Code (which I'm guessing is not very clean, as I'm new):

$pdftk = new pdftk();
if($create_cover) { $pdftk->setInputFile(array("filename" => $cover_page['server'])); }

// Load a list of attachments
$sql = "SELECT * FROM actg_attachments WHERE trans_id = {$trans_id}";
$attachments = Attachment::find_by_sql($sql);
foreach($attachments as $attachment) {
    // Check if the file exists from the attachments
    $attachment->set_variables();
    $file = $attachment->abs_path . DS . $attachment->filename;
    if(file_exists($file)){
        // Use the pdftk tool to attach the documents to this PDF
        $pdftk->setInputFile(array("filename" => $file));
    }
}

$pdftk->setOutputFile($save_file);
$pdftk->_renderPdf();

它正在调用的$ pdftk类来自: https://github.com/bensquire/php-pdtfk-toolkit

the $pdftk class it is calling is from: https://github.com/bensquire/php-pdtfk-toolkit

推荐答案

我不会说这是适当/最佳的解决方法,但它可能会解决您的问题,

I wont say this is an appropriate/best fix, but it may resolve your problem,

在pdf_parser.php中,将以下行注释掉:

In: pdf_parser.php, comment out the line:

$this->error("Unable to find object ({$obj_spec[1]}, {$obj_spec[2]}) at expected location");

应该在544行附近.

您可能还需要更换:

    if (!is_array($kids))
        $this->error('Cannot find /Kids in current /Page-Dictionary');

具有:

    if (!is_array($kids)){
     //   $this->error('Cannot find /Kids in current /Page-Dictionary');
     return;
    }

在fpdi_pdf_parser.php文件中

in the fpdi_pdf_parser.php file

希望有帮助.它对我有用.

Hope that helps. It worked for me.

这篇关于使用PHP,如何检查PDF文件是否有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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