Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

PDF::API2 fails combining pdf 1.3 and 1.4

by Anonymous Monk
on Feb 27, 2013 at 11:25 UTC ( #1020851=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use PDF::API2; use Carp; my $pdfile = "out.pdf"; my $pdf = PDF::API2->new( -file => $pdfile) or croak "can't open $pdfi +le as output"; #open a blank page my $page = $pdf->page; $page->mediabox ('A4'); my %font = ( Helvetica => { Roman => $pdf->corefont( 'Helvetica', -encoding => ' +latin1' ), }, ); # put some text my $header = $page->text; $header->font( $font{'Helvetica'}{'Roman'}, 24 ); $header->fillcolor('darkblue'); $header->translate( 50, 750); my $ht = "Hello, this is the first page"; $header->text($ht); # add pages from other .pdf files; my $include; foreach $include ("1_3.PDF", "1_4.PDF") { $page = $pdf->page; $page->mediabox ('A4'); my $include_object = PDF::API2->open($include); my $sourcepage = $include_object->openpage(1); my $xo = $pdf->importPageIntoForm($include_object, 1); my $gfx = $page->gfx; $gfx->formimage($xo, 0, 0, 1.0); } $pdf->save; $pdf->end();

The above code produces a .pdf file that's perferctly readable by ghostview, but causes an error in Reader XI. I need to produce a .pdf files that collates pages from different sources, inserting text pages that include computed information. The report will be used in a Windows 7 office environment in which avoiding acrobat is not an option.

The problem arises, I believe, when combining .pdf of the 1.3 and 1.4 standard, but the reason could be another. The input files and result are available for download at .

I tried both formimage and importpage, sprinkled some save/restore in the code, the 'pageencaps' option, but it didn't cut it.

My environment is strawberry perl v5.16.2 on Windows 7 32 bits. My version of PDF::API2 is 2.020 MSWin32

Thank you for your help!

Replies are listed 'Best First'.
Re: PDF::API2 fails combining pdf 1.3 and 1.4
by Anonymous Monk on Feb 27, 2013 at 14:42 UTC

      I could, but I need also to include also jpeg and tiff images. After a quick look, I had the impression that CAM::PDF does not support jpeg and tiff, but I may be in error, of course.

      Besides, I am not sure that this is a bug of PDF::API2. It could be an Acrobat's problem or the result of a malformed .pdf file. Unfortunately, I have no control on my source files. Interestingly, I tried to combine the two source .pdf with Acrobat pro 9 and it also fails, but in a different way. I tried also pdftk and found that it behaves in the same way as Acrobat pro.

      I may have to convert the .pdf into bitmaps and include them as such, but it seems a pity.



        I learned that you should not combine pdf files with different pdf versions. They can be converted by reinterpreting them in ghostscript, as below.

        gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=clean.pdf original.pdf # In windows, replace 'gs' with 'gswin32c.exe'

        that made the trick for me. It's trivial to call ghostscrip with 'exec' or 'system' from perl.

        It wasn't a perl problem and I should not have posted it in perlmonks, probably, but I wanted to share the solution anyway.



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1020851]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2018-02-23 16:50 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (303 votes). Check out past polls.