http://www.perlmonks.org?node_id=878572

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:

~fullname~
~phonenumber~
~websiteurl~

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,
Richard

Replies are listed 'Best First'.
Re: PDF File Merging Data
by LanX (Saint) 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.

    UPDATE:

    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.

    UPDATE:

    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.

      thx,
      Richard
        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...