Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

PDF File Merging Data

by Anonymous Monk
on Dec 22, 2010 at 15:33 UTC ( #878572=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

We have a flier that we had created professionally, and we want to give it to our members for free.

We would like to 'swap' a few placeholders with their information, such as:


those fields can be put in the PDF as form fields or just in the page as the rest of the data is, non editable in PDF without a full version of the adobe pdf editor.

Is there a simple way to just open the file and do a swap of those fields?

Thank you,

Replies are listed 'Best First'.
Re: PDF File Merging Data
by LanX (Bishop) on Dec 22, 2010 at 16:11 UTC
    > We would like to 'swap' a few placeholders with their information, such as:

    IIRC PDF::Reuse is meant for this.


    not sure if you can replace placeholders, but you can position where to put your additional text, so you maybe have to locate those placeholders on your own.


    AFAI see fields are supported, but IIRC you have to care yourself about line breaks.

    Cheers Rolf

Re: PDF File Merging Data
by bcrowell2 (Friar) on Dec 22, 2010 at 20:06 UTC

    There's a very handy tool called pdftk that I use on linux. Try something like this:

    pdftk foo.pdf output foou.pdf uncompress

    This will convert a binary pdf into an uncompressed pdf that you can open in a text editor. The uncompressed one is still a valid pdf that can be opened in Adobe Reader, etc. As a test, you could just try hand-editing the file to see if the result is acceptable. If that works, then automating the process in perl should be pretty trivial.

      Even better, if you're going to use pdftk: look at the docs. They claim that it does fill-in forms, providing the PDF was created correctly to start with. I've never used it, but a colleague has used this functionality in a previous incarnation.
Re: PDF File Merging Data
by petecm99 (Pilgrim) on Dec 22, 2010 at 16:32 UTC
    Another option could be to have your template in an easily-readable format, such as a text file. Then the reading part becomes trivial, can replace your placeholders as they are encountered, and write to a pdf using pdf::create.

    The reason I offer this solution as an alternative is due to the fact that I must be quite dense - I tried figuring out how pdf::reuse works, even looked at the PDF::Reuse::Tutorial, and have no clue how you could find the tags and replace them on the fly using that. I could just be a little low on IQ points, but I find pdf::create very easy to use...
      >I tried figuring out how pdf::reuse works, even looked at the PDF::Reuse::Tutorial, and have no clue how you could find the tags and replace them on the fly using that.

      Can't say much about this, the docs seem to talk about form fields and mention JS, so it might not be what the OP wanted.

      Anyway the OP was talking about "replacing placeholders".

      With a PDF version where allowed fields are completely filled with blind text in a dedicated font, pdftohtml -xml could easily be used to parse and grep the absolute positions for these text boundaries.

      And plotting text by absolute positions is trivial with PDF::Reuse. (of course into a PDF without blind text)

      But as I already said, one has to care about line breaks and staying within bounding boxes, cause PDF is not HTML, it's a print format!

      see also: Parsing PDFs by text position?

      Cheers Rolf

      the flier is mostly images, it is a professionally designed flier for our members to share a free giveaway website we built for them to build their organization.

      So, if I had it done in HTML would that work? or does it have to be text? I have looked over the module you suggested, but I don't see an easy way to test it.

        Ahh, images. I don't see anything in pdf::create that allows for embedding images.

        In that case, you could try the module Rolf suggested (pdf::reuse), using the prText method to write to the existing template. But instead of replacing tags, set up the position of each dynamic data element in your Perl script (e.g. ~fullname~ has a position of {10,50}). Then, store/read the values for replacement separately. Doesn't have to be text - could get fancy and read them out of an Excel file, or database...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://878572]
Approved by Corion
Front-paged by Corion
[james28909]: you know, i had a thought a few mnths back. what if this universe were just a puzzle, waiting for life to figure it out!
[james28909]: what if one day we are able to grab the curtain that is "spacetime" and pull it back to reveal <insert imagination here> pulling at levers and ropes xD
[holli]: like dorothy?
[james28909]: see ya later holli
[james28909]: yeah like wizard of oz lol. its just a joke though
[james28909]: i cannot understand how someone cant see that a cloud of gas that evolves into a solar system or galaxies. it just makes sense to me. everythign that has happened, had to have happened for anything on this planet to be.
[james28909]: /gas/gas and debris/
[james28909]: another thing, i used to be atheist. life experiences, personal evidence. and my own perception is the reason why i believe the things i do.

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2017-12-15 14:46 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (433 votes). Check out past polls.