Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Perl to PDF

by CColin (Scribe)
on Nov 19, 2009 at 22:37 UTC ( #808277=perlquestion: print w/replies, xml ) Need Help??
CColin has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Am about to try and write some output to PDF for the first time. I see a bewildering array of modules, with lots of kudos for PDF::API2 but it seems only for those who already know how to use it rather than learn it. I've also looked at PDF::Create - where I'm struggling with all are some decent code examples that show how to write lots of text output. Can't writing a PDF be like writing a file? Is there not some example code that shows how to do this? Thanks for any pointers

Replies are listed 'Best First'.
Re: Perl to PDF
by biohisham (Priest) on Nov 19, 2009 at 23:16 UTC
    Maybe you wanna get familiar with PDF::API2::Lite first, I've faced a similar situation with PDF::API2 so I used PDF::API2::Lite and familiarized myself with it and when I read the PDF::API documentation it became less arcane, here are the steps for PDF::API::Lite:
    1. call the 'new' method.
    2. create an image object.
    3. define page height and width for the image object.
    4. define the image coordinates for the image object.
    5. call the 'saveas' and pass it 'filename.pdf'.
    Here is my code example, for the methods 'new' and 'saveas', if you call them outside the loop, you get a multiple-page pdf file, if you call them from within the loop you got different pdf files for every image.
    #!/usr/local/bin/perl use strict; use warnings; use File::Find; use PDF::API2::Lite; my $dir = "."; #my $pdf = PDF::API2::Lite->new; in case you want one pdf file find(\&find_convert, "$dir"); sub find_convert{ if(/.*\.jpg/){ my $image = $File::Find::name; print "$image\n"; convert(); } } sub convert{ #the are repeated for every match my $image=$_; my $pdf = PDF::API2::Lite->new; my $imageObject=$pdf->image_jpeg("$image"); $pdf->page($imageObject->width, $imageObject->height); $pdf->image($imageObject,0,0); $pdf->saveas("$image.pdf"); }; #$pdf->saveas("$image.pdf"); images are included in the same file...
    Here are some examples using PDF::API2:
    1. PDF Concatenation and Extraction Tool.

    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: Perl to PDF
by graff (Chancellor) on Nov 19, 2009 at 23:00 UTC
    I feel your pain -- getting started with PDF modules in Perl is an uphill battle. I don't know if this will help, but here's a little snippet (posted in the dimly-lit "Snippets" wing of the Monastery): One PDF file for a set of images.

    You might also try putting "PDF::Create" or "PDF::API2" into Super Search, to find whatever nodes (in whatever sections) might contain other code examples using these modules.

      Thanks - unfortunately this is for an image. What I can't figure out is why all the examples deal with single lines or images and neglect to deal with what is surely the most common case? - a text based document. I'd just like to be able to write out lines to a PDF rather than a file... Will also try your supersearch suggestions. Meantime if anyone has any code they'd be willing to share for printing out a few lines to a PDF file that would be awesome!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://808277]
Approved by graff
Front-paged by biohisham
[choroba]: Monopoli looks nice, but a bit far from here.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2018-01-23 12:58 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (245 votes). Check out past polls.