[英]perl pdf::api2 checking if a pdf file is encrypted
我有一个网站使用 perl 脚本供客户上传 pdf 文件供我打印并将打印的页面发布给他们。 我正在使用 PDF::API2 来检测页面大小和页数以计算打印成本。 但是,如果 pdf 文件受密码保护,这不起作用,我收到此错误 -
软件错误:
Objind 9 在 /home5/smckayws/public_html/hookincrochet.com/lib//PDF/API2/Basic/PDF/File.pm 第 758 行的索引 0 处不存在。
我正在尝试使用 pdf::api2 模块中的 isEncrypted 功能来捕获文件已加密,以便将客户引导到不同的页面,以便他们可以手动输入页面大小和页码,但它不适用于我。 我只是收到与上面相同的错误消息。
我尝试了在其他地方找到的以下代码片段。
my $pdf = PDF::API2->open( "$customer_directory/$filename" );
if ( defined $pdf && $pdf->isEncrypted )
{
print "$pdf is encrypted.\n";
exit;
}
while (glob "*.pdf") {
$pdf = PDF::API2->open($_);
print "$_ is encrypted.\n" if $pdf->isEncrypted();
}
任何帮助将不胜感激。
我的猜测是 PDF 可能使用您的 PDF::API2 版本不支持的功能。 这是该问题的解决方法。
isEncrypted
的调用包装在eval
中,捕获错误并处理它。
这仅在未加密文件上未发生错误时才有效。
my $pdf = PDF::API2->open( "$customer_directory/$filename" );
if ( defined $pdf ) {
eval { $pdf->isEncrypted };
if ($@) {
# there was some kind of error opening the file
# could abort now, or look more specific, like this:
if ($@ =~ m/Objind 9 does not exist at index 0/) {
print "$pdf is encrypted.\n";
exit;
}
}
# file is not encrypted, opening worked, continue reading it
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.