PDF-Tk

 view release on metacpan or  search on metacpan

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

      print $fh $$input;
      close $fh;
    } elsif (ref $output eq "SCALAR") {
      my $fh;
      open($fh,"-|",$self->{pdftk},(ref $input eq "ARRAY" ? @$input : $input),@args,"output","-")
       or die "pdftk $input @args - failed: $?";
      $$output=<$fh>;
      close $fh;
    } else {
      system($self->{pdftk},(ref $input eq "ARRAY" ? @$input : $input),@args,"output",$output) == 0 
       or die "pdftk $input @args $output failed: $?";
    }
}

sub document {
    my ($self,$doc)=@_;
    if ($doc) { $self->{document}=$doc; }
    else      { return $self->{document}; }
}

sub pages {
    my $self=shift;
    my $tmpdir=tempdir;
    my ($pdftk,@pages);
    chdir $tmpdir;
    $self->call_pdftk(\($self->{document}),'%d.pdf','burst');
    my $page=1;
    while (-f "./$page.pdf") {
        push @pages,io(cwd."/$page.pdf")->binary->all;
	unlink (cwd."/$page.pdf");
	$page++;
    }
    unlink ("doc_data.txt");
    chdir "/";
    rmdir $tmpdir;
    return (wantarray ? @pages :\@pages);
}

sub page {
    my ($self,$page)=@_;
    my $tmpdir=tempdir;
    chdir $tmpdir;
    $self->call_pdftk(\($self->{document}),'%d.pdf','burst');
    my $res=io(cwd."/$page.pdf")->binary->all;
    unlink <*.pdf>;
    unlink ("doc_data.txt");
    chdir "/";
    rmdir $tmpdir;
    return $res;

}

sub docinfo {
    my ($self,$arg)=@_;
    unless ($self->{documentinfo}) {
        my $documentinfo;
        $self->call_pdftk(\($self->{document}),\$documentinfo,"dump_data");
        my @lines=split "\n",$documentinfo;
        my %documentinfo;
        while (my $line=shift @lines) {
            my ($key,$val)=split m/\:\s*/,$line;
            if ($key eq "InfoKey") {
                $key=$val;
                $line=shift @lines;
                ($val)=$line=~m/InfoValue\:\s*(.+)/;
            }
            $documentinfo{lc($key)}=$val;
        }
        $self->{documentinfo}=\%documentinfo;
   }
   return $self->{documentinfo}->{$arg}if ($arg);
   return $self->{documentinfo};
}
            
 
1;

=head1 NAME

PDF::Tk - Perl integration for the pdf toolkit (pdftk)

=head1 SYNOPSIS

  use PDF::Tk;
  my $doc=PDF::Tk->new(file=>["/tmp/my1.pdf","/tmp/my2.pdf"]);
  my @parts=$doc->pages();

=head1 DESCRIPTION

This module is a interface for the command line pdftk command. 

=head1 METHODS

=over 4

=item new

The constructor for the pdftk module. Takes a hash of arguments

    document - a scalar containing a PDF document,
    file - either a PDF filename or a arrayref of filenames.
    pdftf - path to the pdftk binary, defaults to "/usr/bin/pdftk"

note that document and file are mutually exclusive!

=item call_pdftk

Calls up pdftk command, takes input, output and pdftk operation as arguments
input and output can either be files or scalar refs. input can also be an
array ref of files

=item pages

returns an array in list context, or arrayref, containing the content of all
pages in the document.

=item page

Takes a page as an argument, and returns the contents of that page.

=item docinfo



( run in 1.648 second using v1.01-cache-2.11-cpan-71847e10f99 )