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

Hi monks,

I'm trying to produce a PDF with an image and many paragraphs of text. The code I've written has the same problem with both modules for the text, presumably because I'm making the same mistake.

First is there any good online documentation for either of these ? I've looked but can't find it.

The main problem is that long strings of text do not wrap, but instead disappear off the RHS of the page. escaped characters also make no difference. I've included my code below to demonstrate.

With PDF:API2 I also can't get the graphic to appear (see podded out code below). I get the following error

Can't call method "val" on an undefined value at ...../PDF/API2/Resource/XObject/Image.pm line 99

Finally, is there a way to centralise/align the heading (not included in CreateSimple version of PDF) ?

Thanks.
use PDF::CreateSimple; use strict; use warnings; my $filename = "./test"; my $string = "hello there. This is a fairly long line containing lots +of utterly meaningless long-winded text that I would really realy rea +lly like to see wrapping onto the line below rather than just disapp +earing off the edge of the page.\nThis is still more on a second line +"; my $font_size=10; my $pdf = PDF::API2->new(-file => "mypdf.pdf"); my $page = $pdf->page; my $fnt = $pdf->corefont('Helvetica',-encode => 'latin1'); my $boldfont=$pdf->corefont('Helvetica-Bold',-encode => 'latin1'); my $txt = $page->text; #Heading needs to be centralised $txt->textstart; $txt->font($boldfont,$font_size); $txt->translate(100,770); $txt->text("Heading here."); $txt->textend; =pod #commented out graphic code $pdf->mediabox(595,842); my $gfx=$page->gfx; my $image=$pdf->image_jpeg('./logo.gif'); $gfx->image( $image, 100, 100 ); =cut #String does appear but incorrectly formatted $txt->textstart; $txt->font($fnt, $font_size); $txt->translate(50,670); $txt->text("$string"); $txt->textend; $pdf->save; $pdf->end( ); #------------------------------------------------------ # Try it with PDF::CreateSimple #------------------------------------------------------ my $pdfFile = PDF::CreateSimple->new("./pdfcs.pdf"); $pdfFile->drawText("$string",'Verdana',12,200,760,'black'); $pdfFile->drawImage('./logo.gif',150,650,1.75); $pdfFile->closeFile;

Replies are listed 'Best First'.
Re: PDF::API2 and PDF::CreateSimple
by MidLifeXis (Monsignor) on Feb 20, 2009 at 15:22 UTC

    Perhaps CAM::PDF->wrapString() might work for you.

    --MidLifeXis

      Thanks for pointing that one out. I'd prefer automatic wrapping given the page is A4 if that's possible. If not then I always thought PDF was an unneccessary complication for my project. Problem is convincing "the management". Cheers.
Re: PDF::API2 and PDF::CreateSimple
by mr_mischief (Monsignor) on Feb 20, 2009 at 16:07 UTC
    My preferred module for PDF creation is PDF::API2::Simple. Its text method has an autoflow property to automatically wrap lines even across pages. Despite being a simplified wrapper for most of your work, it also gives you access to the underlying PDF::API2 object so you have all the power of that when you need it.

    I can't say it's better than the CAM::PDF family of modules, because I haven't worked with those much. From what I've read and heard from other monks, either should be a suitable route to take.

      Great, thanks for that pointer as well. I think i'll start with that as it builds on the modules I've already loaded.

      Thanks everyone.