簡體   English   中英

perl pdf::api2 檢查 pdf 文件是否已加密

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM