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

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
[marto]: William Gibson was right
[moritz]: a classical problem with machine learning
[1nickt]: moritz isn;t the difference today that the machine learning algos are in charge of most stuffs?
[moritz]: depends on what you mean by "most stuff"
[1nickt]: I am sure I don't know about most of the stuff that is no longer controlled by humans. I do know that it's past the point of determining consumer pricing and into things that can kill me with blunt force!
[holli]: but is this really a problem 1nickt? Nobody knows how your human taxi drivers brain works either
[holli]: at least for cars, if the ai works better than a human on average, by all means let the machines drive

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2017-11-21 13:06 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (301 votes). Check out past polls.