Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Putting text and images in PDFs using PDF::API2

by leighsharpe (Monk)
on Jan 17, 2006 at 05:22 UTC ( [id://523671]=perlquestion: print w/replies, xml ) Need Help??

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

Hello esteemed monks,
I'm trying to create a PDF using PDF::API2. The resultant PDF needs to contain both text and a graphic on the same page, with the text printed over the top of the graphic. I'm using the following code, taken essentially straight from the PDF::API2 manpage:
use strict; use warnings; use DBI; use DBD::mysql; use PDF::API2; my $font_size=10; my $pdf = PDF::API2->new(-file => "mypdf.pdf"); $pdf->mediabox(595,842); my $page = $pdf->page; my $fnt = $pdf->corefont('Helvetica',-encode => 'latin1'); my $boldfont=$pdf->corefont('Helvetica-Bold',-encode => 'latin1'); my $txt = $page->hybrid; my $gfx=$page->gfx; $txt->textstart; $txt->font($boldfont,$font_size); $txt->translate(100,800); $txt->text("Heading here."); my $image=$pdf->image_jpeg('santa.jpg'); $gfx->image( $image, 100, 100 ); $txt->font($fnt, $font_size); $txt->translate(100,750); my $y=750; $txt->font($fnt, $font_size); my $dbh=DBI->connect("DBI:mysql:database=voice:host=localhost","userna +me","password") or die "$!\n"; my $sth=$dbh->prepare("select column1,column2,column3,column4,column5 +from test") or die "$!\n"; $sth->execute() or die "$!\n"; while (my @row=$sth->fetchrow_array()) { my $string=join(",",@row)."\n"; $txt->translate(100,$y); $txt->text("$string"); $y-=45; if ($y < 100 ) { $txt->textend; $page = $pdf->page(0); $txt = $page->hybrid; $txt->textstart; $txt->font($fnt, $font_size); $txt->translate(100,800); $y=750; $txt->textstart; $txt->font($boldfont,$font_size); $txt->text("Heading here."); $txt->font($fnt, $font_size); } } $sth->finish(); $dbh->disconnect(); $txt->textend; $pdf->save; $pdf->end( );
However, this puts the image on top of the text, and the text becomes unreadable. I need to be able to read the text. Given that I don't really understand the way PDFs work, can anybody point me in the right direction?

Replies are listed 'Best First'.
Re: Putting text and images in PDFs using PDF::API2
by jbrugger (Parson) on Jan 17, 2006 at 10:01 UTC
    To be honest, i don't like PDF::API2 a lot..
    It gave me unsolvable problems, so i changed to XSL-FO and FOP.
    It's relatively easy and offers great control for your layout.
    See this node for more information.

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
      I wrote a PDF module a long time ago and have been using it for personal use. Recently I added html-like tables( has column and row spanning) to it and a few other items. Actually, I keep adding to it as I see reason to. A friend wanted to use it to create bi-lingual Biblical text and so I added unicode support and embeded TrueType fonts(which was lots of fun). You see I had to use iText (java) extensively at work, but really wanted something vastly easier to use for Perl, so I developed my own both as an exercise in learning the PDF Spec better and some Perl OOP. It's only real dependancy is Compress::Zlib. If you want to try it let me know. I call it MoosePdf, silly I know. But I gave it a really terse yet simple and IMHO intuitive syntax. For instance, one way to do the above program with just the raw text and image operators (with a table it would simpler) would be thus:
      use strict; use DBI; use MoosePdf; my $pdf = new MoosePdf('myPdf.pdf'); my $page = $pdf->addPage(size=>'595x852', margins=> 72); my $f = $page->addFont("Helvetica"); my $fb = $page->addFont("Helvetica-Bold"); my $image = $page->addImage(file=>"santa.jpg"); #path my $height = $image->getHeight; $page->showImage( image=>$image, x=>100, y=> 100+$height); $page->addText( text=>"Heading Here", x=>100, y=>100, font=>$fb, size= +> 10 ); my $dbh=DBI->connect("DBI:mysql:database=voice:host=localhost","userna +me","password") or die "$!\n"; my $sth=$dbh->prepare("select column1,column2,column3,column4,column5 +from test") or die "$!\n"; $sth->execute() or die "$!\n"; my $y = 750; while (my @row=$sth->fetchrow_array()) { $y -= 45; my $string=join(",",@row)."\n"; $page->addText( text=>$string, x=>100, y=>$y, font=>$f, size=> 10 ); if( $y < 100 ){ $page = $pdf->AddPage(); #inherits from existing page $page->addText(text=>"Heading Here", font=>$fb, size=>10, x=>100, y +=>800 ); $y = 750; } } $pdf->save();

      Anyway, if you want to try it, msg me with your email and I will send it and some examples to you if you will agree to not redistribute it as it needs more documentation.

      JamesNC
        I know this is an old post but I am interested to use ant test the module for PDF you had created, how could I get a copy and samples of this module like you said?
Re: Putting text and images in PDFs using PDF::API2
by chargrill (Parson) on Jan 17, 2006 at 07:27 UTC
    The resultant PDF needs to contain both text and a graphic on the same page, with the text printed over the top of the graphic.
    ...
    However, this puts the image on top of the text,
    Huh?

    Do you mean you want the text higher on the page (above) than the graphic, but what you're getting is literally the the text superimposed over the graphic?

    ... just a little confused.

      sounds likes the user wants the image to be a background image and have text painted over it, but the image is being rendered after the text instead of before it.
        Correct. That's exactly what I was after.
Re: Putting text and images in PDFs using PDF::API2
by RandomWalk (Beadle) on Dec 23, 2007 at 19:46 UTC
    Though this is an old thread, I got stuck on the same problem, so I'll leave a note for the next passer-by.

    The critical variable is the order in which the objects are instantiated. That is, "$gfx=$page->gfx" should precede "$txt=$page->hybrid" if you want $txt on top.

      Thankyou!

      cheers,

      J

      Yeah-- big thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://523671]
Approved by jbrugger
Front-paged by jbrugger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-20 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found