Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Generating PDF

by Anonymous Monk
on Feb 19, 2001 at 23:51 UTC ( #59446=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

If there an easy way to generate PDF files on a Unix system using Perl and a CGI interface. I'd like to be able to read records from a database and generate a work order outputing it to the PDF format which can then be e-mailed to vendors. Is there a module for this? or some other pre-existing code?? Thanks.

Replies are listed 'Best First'.
Re: Generating PDF
by clintp (Curate) on Feb 20, 2001 at 01:48 UTC
    Okay, I've actually USED PDF::Create that others are suggesting and it's okay. In fact, this short program took some existing text and overlaid a grid on top of it to make nice column rules that the data lacked.
    #!/usr/bin/perl -w use strict; use PDF::Create; my $font; my $height=792; my $width=612; my $left=20; my $topmargin=20; my $pointsize=8; my $spacing=$pointsize+3; sub addtext { my($page, $fh)=@_; my $l=0; my $hpos=$height-$topmargin; while(<$fh>) { $page->stringl($font, $pointsize, $left, $hpos-=$spacing, $_); last if ++$l > 66; } } sub boxpage { my($page)=@_; $page->line($left, $topmargin, $left, $height-$topmargin, ); $page->line( $left, $height-$topmargin, $width-$left, $height-$topmargin); $page->line( $width-$left, $height-$topmargin, $width-$left, $topmargin); $page->line( $width-$left, $topmargin, $left, $topmargin); $page->line($left+42, $topmargin, $left+42, $height-$topmargin); $page->line($left+81, $topmargin, $left+81, $height-$topmargin); $page->line($left+145, $topmargin, $left+145, $height-$topmargin); $page->line($left+183, $topmargin, $left+183, $height-$topmargin); } my $pdf=new PDF::Create('filename' => 'outfile.pdf', 'Version' => '1.2', 'Author' => 'Clinton Pierce', 'Title' => 'Test Report',); my $root=$pdf->new_page('MediaBox' => [ 0, 0, 612, 792 ]); $font=$pdf->font('Subtype' => 'Type1', 'Encoding' => 'WinAnsiEncoding', 'BaseFont' => 'Courier'); open(FH, $0) || die; while(not eof(FH)) { my $page=$root->new_page(); boxpage($page); addtext($page, \*FH); } $pdf->close;
    Now the caveat is, there's not a whole lot PDF::Create can do. No colors. No bitmaps. Not much more than lines, circles and other polygons. No width control on the lines either.

    And PDF::Create hasn't been updated in a long, long time and I got a bounce from the e-mail address of the maintainer. But I got enough out of it for my needs...

      For anybody coming across this thread much much later, please note PDF::Create is being updated again with the most recent version 1.02 being released on 10 Jul 2008.

      CPAN Search - PDF::Create
Re: Generating PDF
by Trinary (Pilgrim) on Feb 19, 2001 at 23:57 UTC
    A search on cpan turned up PDF::Create, which looks to me like your best bet.

    I have no idea how mature/usable this module is, but at thevery least it looks like a good place to start. Frankly, the README looks a lil confusing, but then again I know little about the internals of PDFs. Maybe a Postscript generator might be better, as far as I know the two formats are interchangeable through external converters (ps2pdf), and there seem to be a fair amount of decent Postscript modules out there.


Re: Generating PDF
by Hot Pastrami (Monk) on Feb 19, 2001 at 23:56 UTC
    Check out this module on CPAN: Text::PDF

    Hot Pastrami

      Although the original post is a few years old, I feel compelled to respond to it. Recently I had to develop a few dynamic PDFs from data in a database. After looking at the avaliable options (including writing out the pdf by hand - yuck), I found PDF::API2 to be the most useful.

      The biggest drawback I found to PDF::API2 was that the perldoc left much to be desired. Although it very much has the capabilities to go beyond "simple text" figuring out how to draw curves or use barcodes from the perldoc was difficult at best. If you use PDF::API2 I recommend using for documentation.

      Other than the documentation drawback, expect to spend about a half a day learning the API. Once you get the hang of it, its pretty easy and well done.

Re: Generating PDF
by jeroenes (Priest) on Feb 20, 2001 at 11:09 UTC
    Instead of directly writing PDFs, you also might want consider to write a TeX/LaTeX file, which easily can be converted to a pdf with LaTeX. If you're on gn*x, I would check out the tetex package (find it on

    The advantage of such an approach is, that you can have quite high-level control (ie, just write your text). However, learning about all the handy packages can take quite a while. A sample perl script might be something like this:

    #get the data first $a = <DATA>; chomp $a; $b = <DATA>; chomp $b; open TEX, ">file.tex" or die "Could not open file.tex"; print TEX <<'_END'; \documentclass{article} \begin{document} \title{TeX-report} \author{Me} \maketitle \section{Some data} _END print TEX "I have $a and $b. That's all for now.\n"; print TEX <<'_END'; \section{Some table} \begin{table} \caption{A table} \begin[|l|c|c|]{tabular} \hline _END while(<DATA>){ chomp; print TEX join('&', split ).'\\ \hline'."\n"; } print TEX <<'_END'; \end{tabular} \end{table} \end{document} _END system("latex file.tex"); system("latex file.tex"); system("dvipdf file") if -e "file.dvi"; __DATA__ 129873 12315 1 8 2 1 3 100 8 3 4

    Here I'm also using the dvipdf package, (, but you could use 'dvips file -o' and 'ps2pdf' as well. Perl code is checked. I'm using TeX daily, so you always can /msg me with further questions.

    Hope this helps,

    "We are not alone"(FZ)

      Another option might be to generate XSL:FO, which can be converted into PDF by e.g. FOP (c/o; this is an XML dialect, so you can actually just output XML and use an XSL:Transform stylesheet to convert the same XML into HTML, WML, or XSL:FO (for conversion to PDF)... Big points for Laziness :-)

Re: Generating PDF
by foogod (Friar) on Feb 20, 2001 at 06:16 UTC
    I completed a very similar project with a relational flat file database system, and used a third party program called PDFever ( that allowed much more flexibility, and integrated very well with my perl program.

    Also the creator of the program (Zhigang Li) is very helpful with PDF troubleshooting.

    For a hacker such as myself, programming for PDF (which IMHO is very obscure), it was a relief to find a program that allowed me lots of flexibility and very little time required to learn the PDF aspect.

    The Perl::CreatePDF mod is good, but I found the flexibility was lacking, and there was no support. (Just my 2 cents)
Re: Generating PDF
by boo_radley (Parson) on Feb 19, 2001 at 23:57 UTC
    Magic 8 ball says "yes".
    specifically the PDF::create module may be what you're after.
Re: Generating PDF
by Beatnik (Parson) on Apr 30, 2001 at 19:08 UTC
    Image::Magick also has PDF support but it's not superb (it is, after all, an image manipulation program). It basically rasterizes the layers, outputting pages with a single image, breaking the search features & fancy layers.

    ... Quidquid perl dictum sit, altum viditur.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://59446]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2017-01-19 17:59 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (170 votes). Check out past polls.