CAM-PDF

 view release on metacpan or  search on metacpan

bin/pdfinfo.pl  view on Meta::CPAN


   # prompt for password
   my $doc = CAM::PDF->new($file, q{}, q{}, 1) || die "$CAM::PDF::errstr\n";

   if ($file eq q{-})
   {
      $file = 'STDIN';
   }
   my $size = length $doc->{content};
   my $pages = $doc->numPages();
   my @prefs = $doc->getPrefs();
   my $pdfversion = $doc->{pdfversion};
   my $pdfinfo = $doc->{trailer}->{Info};
   $pdfinfo &&= $doc->getValue($pdfinfo);

   my @pagesize = (0,0);
   my $p = $doc->{Pages};
   my $box = $p->{MediaBox};
   if ($box)
   {
      $box = $box->{value};

bin/pdfinfo.pl  view on Meta::CPAN

            $timegm += $tzshift;
            $val = localtime $timegm;
         }
         printf "%-13s %s\n", $key.q{:}, $val;
      }
   }
   print 'Page Size:    '.($pagesize[0] ? "$pagesize[0] x $pagesize[1] pts" : 'variable')."\n";
   print 'Optimized:    '.($doc->isLinearized()?'yes':'no')."\n";
   print "PDF version:  $pdfversion\n";
   print "Security\n";
   if ($prefs[0] || $prefs[1])
   {
      print "  Passwd:     '$prefs[0]', '$prefs[1]'\n";
   }
   else
   {
      print "  Passwd:     none\n";
   }
   print '  Print:      '.($prefs[2]?'yes':'no')."\n";
   print '  Modify:     '.($prefs[3]?'yes':'no')."\n";
   print '  Copy:       '.($prefs[4]?'yes':'no')."\n";
   print '  Add:        '.($prefs[5]?'yes':'no')."\n";
   if (@ARGV > 0)
   {
      print "---------------------------------\n";
   }
}


__END__

=for stopwords pdfinfo.pl

bin/rewritepdf.pl  view on Meta::CPAN

use Getopt::Long;
use Pod::Usage;

our $VERSION = '1.60';

my %opts = (
            decode      => 0,
            cleanse     => 0,
            clearannots => 0,
            filters     => [],
            newprefs    => 0,
            prefs       => [],
            newpass     => 0,
            pass        => [],
            decrypt     => 0,
            newversion  => 0,

            verbose     => 0,
            order       => 0,
            help        => 0,
            version     => 0,

bin/rewritepdf.pl  view on Meta::CPAN

            );

Getopt::Long::Configure('bundling');
GetOptions('1|2|3|4|5|6|7|8|9' => sub {$opts{newversion} = '1'.$_[0]},
           'c|cleanse'     => \$opts{cleanse},
           'd|decode'      => \$opts{decode},
           'f|filter=s'    => \@{$opts{filters}},
           'C|clearannots' => \$opts{clearannots},
           'X|decrypt'     => \$opts{decrypt},
           'p|pass'        => sub { @{$opts{pass}}=(); $opts{looking}='pass'; $opts{state}=2; $opts{newpass}=1; },
           'P|prefs'       => sub { @{$opts{prefs}}=(); $opts{looking}='prefs'; $opts{state}=4; $opts{newprefs}=1; },
           'v|verbose'     => \$opts{verbose},
           'o|order'       => \$opts{order},
           'h|help'        => \$opts{help},
           'V|version'     => \$opts{version},
           '<>'            => sub {
              if ($opts{looking})
              {
                 push @{$opts{$opts{looking}}}, $_[0];
                 if (--$opts{state} == 0)
                 {

bin/rewritepdf.pl  view on Meta::CPAN

      if ($opts{decode})
      {
         $doc->decodeObject($objnode);
      }
      foreach my $filtername (@{$opts{filters}})
      {
         $doc->encodeObject($objnode, $filtername);
      }
   }
}
if ($opts{newprefs} || $opts{newpass})
{
   my @p = $doc->getPrefs();
   if ($opts{newpass})
   {
      $p[0] = $opts{pass}->[0];
      $p[1] = $opts{pass}->[1];
   }
   if ($opts{newprefs})
   {
      $p[2] = $opts{prefs}->[0];
      $p[3] = $opts{prefs}->[1];
      $p[4] = $opts{prefs}->[2];
      $p[5] = $opts{prefs}->[3];
   }
   $doc->setPrefs(@p);
}
if ($opts{decrypt})
{
   $doc->cacheObjects();
   $doc->{crypt}->{noop} = 1;
   if ($doc->{crypt}->{EncryptBlock})
   {
      $doc->deleteObject($doc->{crypt}->{EncryptBlock});

bin/rewritepdf.pl  view on Meta::CPAN

   -C --clearannots    remove all annotations (including forms)
   -d --decode         uncompress any encoded elements
   -f --filter=name    compress all elements with this filter (can use more than once)
   -X --decrypt        remove encryption from the document
   -o --order          preserve the internal PDF ordering for output
   -v --verbose        print diagnostic messages
   -h --help           verbose help message
   -V --version        print CAM::PDF version

   -p --pass opass upass              set a new owner and user password
   -P --prefs print modify copy add   set boolean permissions for the document

The optional password arguments are needed to open password-protected
PDF files.  Here's an example of password-protecting and then
unprotecting it in sequence:

  rewritepdf.pl --pass SecretPass SecretPass orig.pdf passworded.pdf
  rewritepdf.pl --decrypt passworded.pdf unprotected.pdf SecretPass

If you want to prevent people from being able to perform the latter
step, then tighten your permissions:

lib/CAM/PDF.pm  view on Meta::CPAN

    
    my $page1 = $pdf->getPageContent(1);
    [ ... mess with page ... ]
    $pdf->setPageContent(1, $page1);
    [ ... create some new content ... ]
    $pdf->appendPageContent(1, $newcontent);
    
    my $anotherpdf = CAM::PDF->new('test2.pdf');
    $pdf->appendPDF($anotherpdf);
    
    my @prefs = $pdf->getPrefs();
    $prefs[$CAM::PDF::PREF_OPASS] = 'mypassword';
    $prefs[$CAM::PDF::PREF_UPASS] = 'mypassword';
    $pdf->setPrefs(@prefs);
    
    $pdf->cleanoutput('out1.pdf');
    print $pdf->toPDF();

Many example programs are included in this distribution to do useful
tasks.  See the C<bin> subdirectory.

=head1 DESCRIPTION

This package reads and writes any document that conforms to the PDF

lib/CAM/PDF.pm  view on Meta::CPAN

Note: any omitted booleans default to false.  So, these two are
equivalent:

    $doc->setPrefs('password', 'password');
    $doc->setPrefs('password', 'password', 0, 0, 0, 0);

=cut

sub setPrefs
{
   my ($self, @prefs) = @_;

   my $p = $self->{crypt}->encode_permissions(@prefs[2..5]);
   $self->{crypt}->set_passwords($self, @prefs[0..1], $p);
   return;
}

=item $doc->setName($object, $name)

I<For INTERNAL use>

Change the name of a PDF object structure.

=cut

t/pdf.t  view on Meta::CPAN

   is($doc->canModify(), $initial_permissions[1], 'canModify');
   is($doc->canCopy(),   $initial_permissions[2], 'canCopy');
   is($doc->canAdd(),    $initial_permissions[3], 'canAdd');
   $doc->setPrefs(@passwords);
   is_deeply([$doc->getPrefs()], [@passwords, 0,0,0,0], 'getPrefs');
   ok(!$doc->canPrint(),  'canPrint');
   ok(!$doc->canModify(), 'canModify');
   ok(!$doc->canCopy(),   'canCopy');
   ok(!$doc->canAdd(),    'canAdd');

   my @prefs = (1,0,1,0);
   $doc->setPrefs(@passwords, @prefs);
   $doc->setPrefs(@passwords, @prefs);
   is_deeply([$doc->getPrefs()], [@passwords, @prefs], 'getPrefs');

 SKIP:
   {
      skip 'optional memory leak test skipped', 1 if (!$can_test_leaks);
      Test::Memory::Cycle::memory_cycle_ok($doc, 'memory leak test');
   }

   {
      my $doc2;
      my $serialized = $doc->toPDF();

t/pdf.t  view on Meta::CPAN

      $doc2 = CAM::PDF->new($serialized, 'wrong', 'password');
      is($doc2, undef, 'open encrypted PDF, wrong password');
      
      $doc2 = CAM::PDF->new($serialized, '', '', {fault_tolerant => 1});
      isnt($doc2, undef, 'open encrypted PDF, fail gently');
      
      clearerr();
      $doc2 = CAM::PDF->new($serialized, @passwords);
      isnt($doc2, undef, 'open encrypted PDF, right password');
      checkerr();
      is_deeply([$doc2 ? $doc2->getPrefs() : ()], [@passwords, @prefs], 'getPrefs');

    SKIP:
      {
         skip 'optional memory leak test skipped', 1 if (!$can_test_leaks);
         Test::Memory::Cycle::memory_cycle_ok($doc2, 'memory leak test');
      }
   }
}




( run in 0.470 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )