Problem with $pdf->page()

Brendon Oliver <[email protected]> Thu, 15 Nov 2007 08:31:28 +1100
Newsgroups gmane.comp.lang.perl.modules.pdfapi2
Message-ID <[email protected]>
Hi there,

Hopefully someone can shed some light on a problem I'm having adding pages to 
a PDF file. 

We're generating a PDF report and don't know ahead of time how many pages are 
going to be in the report.  So in the past, the code has simply called 
$pdf->page() to append each new page to the report.  When we're done, we then 
generate a contents page and call $pdf->page(1) to prepend the contents; then 
finally generate a cover page, again calling $pdf->page(1) to prepend the 
cover before the table of contents.  So we basically get:

	p1 = cover
	p2 = contents
	p3+ = report pages

That has been working fine on our production system which is PDF:API2 v0.0.57.  
However, the same code when run on machines which have been upgraded to 
v0.0.66 produce the following output:

	p1 = 1st 'report' page
	p2 = cover
	p2 = contents
	p3+ = remaining report pages

Here's some sample code which shows the problem:

>>>>> CUT HERE
#!/usr/bin/perl -w

use strict;

use PDF::API2;

our $h = 842;
our $w = 595;

my $pdf  = PDF::API2->new();
my $font = $pdf->corefont('Helvetica');

my $page_one = $pdf->openpage(1);
$pdf->preferences(-firstpage => [ $page_one, -xyz => [0,$h,1]],
		  -thumbs    => 1);

for my $i ( 1 .. 5 ) {
	my $page = $pdf->page(0);
	$page->mediabox(0, 0, $w, $h);
	
	my $txt = $page->text();
	$txt->font($font, 12);
	$txt->fillcolor('black');
	$txt->translate(100, 700);
	
	$txt->text("This is item #$i");
}

add_cover($pdf, $font);

$pdf->saveas("./test.pdf");

exit(0);

#--------------------------------------------
sub add_cover
{
	my ( $pdf, $font ) = @_;

	my $page = $pdf->page(1);
	$page->mediabox(0, 0, $w, $h);
	my $txt = $page->text();
	$txt->font($font, 12);
	$txt->fillcolor('black');
	$txt->translate(100, 700);
	$txt->text("Intended contents page");
	
	$page = $pdf->page(1);
	$page->mediabox(0, 0, $w, $h);
	$txt = $page->text();
	$txt->font($font, 12);
	$txt->fillcolor('black');
	$txt->translate(100, 700);
	$txt->text("Intended cover page");

	return 1;
}
<<<<< END CUT

NB: I'm not overly familiar with working with PDFs (been tasked with modifying 
this report program that wasn't written by me), and the above code produced 
the following error:

Can't call method "outobj" on an undefined value 
at /usr/local/lib/perl5/site_perl/5.8.8/PDF/API2/Basic/PDF/Array.pm line 85.

I couldn't figure out why, so I shut it up by adding an "if defined $obj" on 
the offending line, but that's a side issue. The "production" report code 
does not have that error when run with v0.0.66 - just the cover / contents 
pages are not ordered correctly.  Would be interested to know what I missed 
in the example code that caused that error - it's probably something 
bleedingly obvious to everyone else!   ;-)


Just to double-check, I also grabbed the 0.57 source tarball off the 
production machine and installed it locally (ie. downgraded from 0.66 to 
0.57), and aside from the complaint about calling 'outobj' on an undef value, 
the sample code above produces the pages in the expected order.

Thanks in advance for any help / suggestions as to what's going on.

Cheers,

- Brendon.

-- 
Certainly there are things in life that money can't buy,
But it's very funny -- did you ever try buying them without money?
		-- Ogden Nash

 08:17:52 up 19:17,  1 user,  load average: 2.99, 2.07, 1.62