Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello , I am trying to extract some pages from the pdf using PDF:API2 importpage function.

When I view the output pdf( generated via above method )
using acrobat reader it works for most but for some
pdf and for some pages it gives the error "An
unrecognised token was found" or "Illegal operation q
inside a text object" .

The importpage function do not return any error it
is only when we view the output pdf document using
acrobat reader it shows an error .
Please help.


Here is the code am using :

use strict; use warnings; use PDF::API2 ; my $pdfFlname= "./pdf/1405460.pdf" ; my $pdf ; eval { $pdf = PDF::API2->open( $pdfFlname ) ; } ; if( $@ ) { print "error in opening the pdf $@ \n" ; exit ; } my $noOfPages = $pdf->pages; print "noofpage=" . $noOfPages . "\n" ; if ( $noOfPages > 24 ) { $noOfPages = 5 ; } #my $preview_pdf = PDF::API2->new( -file => 'out_104665.pdf' ); my $preview_pdf = PDF::API2->new(); my $i = 1 ; while ( $i <= $noOfPages ) { $preview_pdf->importpage($pdf,$i) ; $i = $i + 1; } $preview_pdf->saveas("./out_pdf_api2/out_1405460_new.pdf") ; print "done.." ;

Thanks and Regards
Sona.

Replies are listed 'Best First'.
Re: PDF::API2 importpage
by Khen1950fx (Canon) on May 15, 2007 at 10:54 UTC
    This worked for me. I used KPDF instead of Adobe
    #!/usr/bin/perl use strict; use warnings; use PDF::API2; my $pdfFlname = "./pdf/1405460.pdf"; my $pdf; eval { $pdf = PDF::API2->open( $pdfFlname ); }; if ( $@ 0 { print "error in opening the pdf $@ \n"; exit; } my $noOfPages = $pdf->pages; print "noofpage=" . $noOfPages . "\n"; if ( $noOfPages .24 ) { $noOfPages =5; } my $preview_pdf = PDF::API@->new( file => 'out_1046665.pdf'); $preview_pdf = PDF::API2->new(); my $i = 1; While ( $i <= $noOfPages ) { $preview_pdf->importpage($pdf, $i); $i = $i + 1; } $pdf->saveas("out_1405460_new.pdf"); print "done..";
    #Note: Start KPDF in the shell. The standalone can't do it. Fixed typo...$1 should have been $i.
Re: PDF::API2 importpage
by snoopy (Curate) on May 16, 2007 at 04:48 UTC
    Just to give you an alternative.

    Rather than doing a full import, the following creates a blank page then overlays text and graphics from the source page:

    #!/usr/bin/perl use warnings; use strict; use PDF::API2; sub importPageGraphics { my ($pdf,$sourcepdf,$sourceindex,$targetindex) = @_; $sourceindex ||= 1; $targetindex ||= 0; # create new page my $sourcepage = $sourcepdf->openpage($sourceindex); my $targetpage = $pdf->page($targetindex); $targetpage->mediabox($sourcepage->get_trimbox); my $gfx = $targetpage->gfx; my $xoform = $pdf->importPageIntoForm($sourcepdf, $sourceindex); $gfx->formimage($xoform, 0, 0, 1.0); } my $pdf = PDF::API2->new(); my $src_pdf = PDF::API2->open('/tmp/overlay.pdf'); importPageGraphics($pdf,$src_pdf); $pdf->saveas('/tmp/test.pdf');

    I found importPageIntoForm and friends to be more reliable and lightweight than the importpage method.