简体   繁体   中英

perl pdf::api2 checking if a pdf file is encrypted

I have a website using a perl script for customers to upload a pdf file for me to print and post the printed pages to them. I am using PDF::API2 to detect the page size and number of pages in order to calculate the printing costs. However, if the pdf file is password protected this does not work and I get this error -

Software error:

Objind 9 does not exist at index 0 at /home5/smckayws/public_html/hookincrochet.com/lib//PDF/API2/Basic/PDF/File.pm line 758.

I am trying to use the isEncrypted feature in the pdf::api2 module to catch that the file is encrypted in order to direct the customer to a different page so they can enter the page size and page number manually, but it is not working for me. I just get the same error message as above.

I have tried the following code snippets found elsewhere.

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();
}

Any help would be greatly appreciated.

My guess is that the PDFs might use a feature that your version of PDF::API2 doesn't support. This is a workaround for the problem.

Wrap the call to isEncrypted in an eval , catch the error and handle it .

This will only work if the error does not occur on unencrypted files.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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