Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

PDF with Background Image VIA Perl

by Hammy (Scribe)
on Jan 12, 2007 at 02:23 UTC ( #594291=perlquestion: print w/replies, xml ) Need Help??
Hammy has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have searched CPAN, your FAQ, and every other source I could think of and I can not find a solution to my problem. I need to build a PDF Page with a background image. I have used Image::Magick and it nicely creates a page with an image. This looks exactly as I need it to. I have also used PDF::API2:Simple to create a page with text on it. What I need is the text sitting on top of the image. PDF::API2::Simple has a method called image, but that does not seem to do what I need it to. I almost need a template that I can fill in. I am putting a name on a certificate. Thanks in advance.

Replies are listed 'Best First'.
Re: PDF with Background Image VIA Perl
by almut (Canon) on Jan 12, 2007 at 15:59 UTC

    You might have shown us what you've tried so far :)

    (You know, your fellow Monks are always more inclined to point out deficiencies in your code, than write the code from scratch themselves...   OK, just kidding).

    Anyway, here are two examples using PDF::API2 that should get you started:

    #!/usr/bin/perl use strict; use warnings; use PDF::API2; my $text = "Some text on top of the image"; my $pdf = PDF::API2->new(); my $page = $pdf->page; # note, the order of the next two statements is IMPORTANT # (otherwise the image will be rendered on top of the text, hiding it. +..) my $gfx = $page->gfx; my $txt = $page->text; my $img = $pdf->image_jpeg("background.jpg"); $gfx->image( $img, 30, 100, # position (by default in points (=1/72 inch)) 0.5 # scaling (i.e. size) # (you'll probably want to play with those...) ); my $font = $pdf->corefont('Helvetica-Bold', -encoding => 'latin1'); # alternatively, use TrueType fonts: # my $font = $pdf->ttfont('Verdana-Bold.ttf', -encoding => 'latin1'); $txt->font($font, 25); $txt->translate(100, 150); $txt->fillcolor('white'); $txt->text($text); $pdf->saveas("Test1.pdf");

    The next example makes use of the textlabel() method, which might be more convenient if you have several short text fragments you'd like to position / configure individually.

    #!/usr/bin/perl use strict; use warnings; use PDF::API2; my $text = "Some text on top of the image"; my $pdf = PDF::API2->new(); my $page = $pdf->page; my $gfx = $page->gfx; my $img = $pdf->image_jpeg("background.jpg"); $gfx->image( $img, 30, 100, # position (by default in points (=1/72 inch)) 0.5 # scaling (i.e. size) # (you'll probably want to play with those...) ); my $font = $pdf->corefont('Helvetica-Bold', -encoding => 'latin1'); $gfx->textlabel( 100, 100, # position (in points) $font, 30, # font and size $text, # guess what... # options -color => '#ff0080', # in pink ;) -rotate => 30, # angle ); $pdf->saveas("Test2.pdf");

    Also, be sure to read the docs of PDF::API2::Content (in addition to PDF::API2). It contains the interesting stuff :)

      Well said - I spent a lot of time writing code for this one and did not have any success. PDF::Reuse looks to be my best bet (although, out of curiosity I will try what you have provided). Thank you very much.
Re: PDF with Background Image VIA Perl
by marto (Archbishop) on Jan 12, 2007 at 09:16 UTC
    Hammy,

    Perhaps I have misunderstood what your requirements are, if you are going to be mass producing PDFs you may want to create a template PDF file, and populate it with data using PDF::Reuse. Check it out, it may be exactly what you need.

    Hope this helps.

    Martin
      As Jim Carrey said in Bruce Almighty - B E A utiful! This looks to be exactly what I was looking for. I have learned so much for the Monks suggestions!
Re: PDF with Background Image VIA Perl
by cosimo (Hermit) on Jan 12, 2007 at 16:19 UTC

    Take a look at Daniel Kasak's PDF::ReportWriter and examples. I worked heavily on it, and it can do amazing things, like template PDF pages, gif/jpg/png images, barcodes, and it's XML driven.

    Look at the template feature: you can use an existing PDF file as background (put your image in a PDF, for example) and start writing on it.

    You can check out the code to see how it's implemented (some cool PDF::API2 use)...

Re: PDF with Background Image VIA Perl
by starX (Chaplain) on Jan 12, 2007 at 03:32 UTC
    Someone else might have a better suggestion, but have you stumbled across this? From the sound of it, you should be able to use it to create an HTML page with whatever background image you want, and then pdfitize it.
      Thanks starX. I will do a little research with this one. Another Monk gave me the PDF::Resuse suggestion and that one looks to be exactly what I was hoping for.
Re: PDF with Background Image VIA Perl
by ikkon (Monk) on Jan 15, 2007 at 23:48 UTC
    I have used PDF::API2, and have gotten pretty far with it, however I have noticed some problems with it, as in I am having troubles creating paragraph text elements, but I have a descent amount working like I want it to if you need any examples with PDF::API2 let me know I will post some.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://594291]
Approved by GrandFather
Front-paged by cosimo
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-07-16 05:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (332 votes). Check out past polls.

    Notices?