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

I have never used PDF-API2 before. I am tring to create a two page document but can not write to the 2nd page. I would appreciate any commments. Here is my code:
#!c:/perl/bin/perl -w use strict; use PDF::API2; my $pdf=PDF::API2->new; # Create a new top-level PDF document my $font = $pdf->corefont('Helvetica'); my ($page, $text); $page=$pdf->page(); # Create 1st page $page->mediabox(500, 700); # Set the page dimensions $text=$page->text(); $text->translate(50, 650); # Position the text $text->font($font,12); $text->text("Page 1"); $page=$pdf->page(); # Create 2nd page $page->mediabox(500, 700); # Set the page dimensions $text->translate(50, 640); # Position the text $text->text("Page 2"); $pdf->saveas("file.pdf"); $pdf->end; exit;
Thanks for your help!

Replies are listed 'Best First'.
Re: PDF-API2 Basic Question
by edoc (Chaplain) on May 10, 2005 at 02:37 UTC

    When I ran your code it produced a document with two pages but the page labels were both on the first page. You need to get a text object from the 2nd page so you can add text to it. The text object you are using is from the first page so that's where the text goes.

    #!c:/perl/bin/perl -w use strict; use PDF::API2; my $pdf=PDF::API2->new; # Create a new top-level PDF document my $font = $pdf->corefont('Helvetica'); my ($page, $text); $page=$pdf->page(); # Create 1st page $page->mediabox(500, 700); # Set the page dimensions $text=$page->text(); $text->translate(50, 650); # Position the text $text->font($font,12); $text->text("Page 1"); $page=$pdf->page(); # Create 2nd page $page->mediabox(500, 700); # Set the page dimensions $text = $page->text(); $text->translate(50, 650); # Position the text $text->font($font,12); $text->text("Page 2"); $pdf->saveas("file.pdf"); $pdf->end; exit;

    cheers,

    J

      I agree with you. I need to creat a text object for the 2nd page, but when I try I receive this error:
      Can't call method "isvirtual" on an undefined value at C:/Perl/site/li +b/PDF/API2 /Content.pm line 1456.
      This is the code I am using:
      #!c:/perl/bin/perl -w use strict; use PDF::API2; my $pdf=PDF::API2->new; # Create a new top-level PDF document my $font = $pdf->corefont('Helvetica'); my ($page, $text); $page=$pdf->page(); # Create 1st page $page->mediabox(500, 700); # Set the page dimensions $text=$page->text(); $text->translate(50, 650); # Position the text $text->font($font,12); $text->text("Page 1"); $page=$pdf->page(); # Create 2nd page $page->mediabox(500, 700); # Set the page dimensions my $text2=$page->text(); $text2->translate(50, 640); # Position the text $text2->text("Page 2"); $pdf->saveas("file.pdf"); $pdf->end; exit;

        The following code works. You need to define the media size and the font on the 2nd page.

        #!/usr/bin/perl -w use strict; use PDF::API2; my $pdf=PDF::API2->new; # Create a new top-level PDF document my $font = $pdf->corefont('Helvetica'); my ($page, $text); $page=$pdf->page(); # Create 1st page $page->mediabox(500, 700); # Set the page dimensions $text=$page->text(); $text->translate(50, 650); # Position the text $text->font($font,12); $text->text("Page 1"); $page=$pdf->page(2); # Create 2nd page $page=$pdf->openpage(2); $page->mediabox(500, 700); # Set the page dimensions for 2nd page $text=$page->text(); $text->font($font,12); # Set font on the 2nd page $text->translate(50, 640); # Position the text $text->text("Page 2"); $pdf->saveas("file.pdf"); $pdf->end;
Re: PDF-API2 Basic Question
by scmason (Monk) on May 09, 2005 at 23:02 UTC
    Just a word of advise, when using a module that is not working as advertised, I always assume that the version of the module that I am using is broken. I then go to CPAN and get a couple of the most recent versions and try them. More often than not, there is a small mistake in the version that I am using.

    Let's say you are currently using the most recent version of PDF::API2 (If not, try that first). Maybe the author corrected a problem from the previous version, but never tested the fix on a multiple page document. Perhaps the previous version does everything you need it to, and it works on multple pages. Now, not only do you have a working version, but you have helped the author test. You should let them know what your problems/fix was so that they can fix the problem.

    Though it can be a waste of time, this has helped me (and module authors) more than once.