Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Playing with PDFs

by BigLug (Chaplain)
on Feb 13, 2003 at 03:49 UTC ( [id://234893]=perlquestion: print w/replies, xml ) Need Help??

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

I've searched here and CPAN for an answer, but as yet I have no solution. Thus I ask for your wisdom, fellow monks.

I'm a designer who has layouts of business cards for many businesses. We get more and more orders for the same cards, just with new information.

I'd like to be able to create a PDF file of a business card but instead of people's details it will contain variables.

What happens next is that I run my yet-to-be-written perl program. It opens the given PDF file and finds all the variables. (They might look like '#firstname#'). The mythical program will then ask me for a firstname.

Once I enter the apropriate information, the script will then output a new PDF with the variables replaced with the data.

This has to be possible, I'm just not sure where to start. I've looked at the various PDF modules but they seem to be too low-level, or just for reading, or just for creating.

If anyone has any experience or suggestions for manipulating PDF files let me know.

Replies are listed 'Best First'.
Re: Playing with PDFs
by andreychek (Parson) on Feb 13, 2003 at 04:14 UTC
    Hi there,

    You are most definitely looking for PDF::Template. You would create a template using XML, and the program will fill in variables at run-time, much the same way that HTML::Template works.

    There is one issue you may run into -- there are certain page sizes predefined that will work. Things like Letter, Legal, A3, A4, etc. You may have to do a quick edit to the PDF::Template file to add the appropriate definition for your business card size. But that should be simple.

    PDF::Template does require PDFLib, a C library for manipulating PDF files. You'll want to grab the source at the very bottom of that page. It's released under the Aladdin license.

    Good luck!
    -Eric

    --
    Lucy: "What happens if you practice the piano for 20 years and then end up not being rich and famous?"
    Schroeder: "The joy is in the playing."
      I've looked at PDF::Template, but it seems to only create PDF files. I don't want to have to work out my design in XML just to put it all together. I want the PDF file to already exist (created from Quark) and the tool will just replace text in the file.

      If I'm wrong, and it can read PDF files as a base, let me know. I'd really rather just replace text WITHIN the PDF file rather than descripbing exactly where the text should go using XML.

        As PDF is just an output format for PostScript, I don't see why you don't just create a PostScript file, which is able to be read and written to by Perl or indeed any other application, then transform the .PS file into PDF?
        --
        “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
        M-J D
Re: Playing with PDFs
by simon.proctor (Vicar) on Feb 13, 2003 at 09:38 UTC
    Another alternative to the ones suggested is to use FOP from the command line. You can then populate an XML file of your choice and then process it, via XSLT, with FOP.

    This isn't a pure perl solution (fop is Java based) but it should meet your needs. Here is where you can find it.

    HTH

    SP
Re: Playing with PDFs
by dragonchild (Archbishop) on Feb 13, 2003 at 14:42 UTC
    There are ways of reading PDF files, modifying them, them printing it back as a PDF file. They are ugly, prone to issues (like randomly not working), and are extremely difficult to work with.

    It is EXTREMELY simply to work with (the soon-to-be-released version of) PDF::Template. Creating the XML template is actually easier than creating the PDF. If you already have the PDF, converting it to the XML layout is simple, too. (And, as a new co-author of PDF::Template, I would be delighted to help you convert your current PDFs to the appropriate XML.)

    Also, we are looking at (in the not-too-distant future) creating a DBI-like Document namespace which would handle reading from and writing to PDFs (as well as PS, TeX, and other similar document layout formats). That would definitely solve your issue. And, if you (or anyone else) wanted to help, we're just working through requirements right now. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Playing with PDFs
by nite_man (Deacon) on Feb 13, 2003 at 08:13 UTC
    Hi BigLug,
    I think that you can convert your PDF template into PostScript, than replace your variables by real data and convert PostScript into PDF again. Maybe it isn't very elegant way but it's one of many variants.
    Michael
      I agree with this. PostScript is fairly easy to read, is text based, etc. You should be able to take a template postscript file, make the text subsitutions in it, and then run it through some sort of PostScript to PDF (or gif or...) converter.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 07:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found