Is there any PHP PDF library that can replace placeholder variables in an existing PDF, ODT or DOCX document, and generate a PDF file as the end result, without screwing up the layout?


  • Needs no 3rd party web service

  • Ability to run on shared web hosting would be ideal (no binary installations / packages required)

Mind you, a library that is able to load an existing PDF file and insert text programmatically at a specific position is not enough for my use case.

As far as my research shows, there is no library that can do this:

  • TCPDF can only generate documents from scratch

  • FPDI can read existing PDF templates, but can only add contents programmatically (no template variable replacement)

  • There are various DOCX/ODT template libraries out there but they don't output PDF

PHPDOCx claims to be able to do exactly what I need - but they don't offer a trial version and I'm not going to buy a cat in a bag, especially not when there seems to be no other product on the web that does this. I find it hard to believe they can do this without problems - if you have successfully done this using the product, please drop a line here.

Am I overlooking something?

Is there a way to do this using PDF forms? I am creating the source documents in OpenOffice 3.

I may be able to use standard Linux commands (pdftk is available for example, trying that out right now.)

Update: *Argh!* I was called out of the office and the bounty expired in the meantime. Starting a new bounty: As far as my testing shows, no solution works for me perfectly yet.

Update II: I will be looking the pdftk approach soon, but I am also starting another bounty for one more round of collecting additional input. This question has now seen 1300 rep points in bounties, must be some kind of a record :)


Which size of placeholder variables are at stake? If it's just single words (or < 30 bytes) and not lengthy paragraphs, then placeholer preparation and a simple search and replace approach would do.

Written by mario

@mario they may be multi-line paragraphs but I could see to it that they are split into separate lines.

Written by Pekka

Is there any reason why you need to parse out placeholders? What if there was just space in the PDF for the generated content. Probably wouldn't work if you don't know the length of the content though.

Written by Jeremy

@Pekka, why can't you use a cheap VPS ( and remove the limitation of not able to install packages/libraries? That would ease out the stuff. Is there a reason for shared hosting?

Written by shamittomar

@shamittomar it's for a client, and I want to keep the technical and bookkeeping overhead as small as possible. A VPS would indeed make this easy but add a whole new parameter to the equation (A server needs administration, installation, continuous security updates, .... It can be down while the site needing the PDF services is still up...) that I want to avoid at all costs

Written by Pekka

Pekka, if the PDF is a big one and text replacement takes too long, won't the server kill your PHP script. You are better off offloading the job to a daemon running on the server.

Written by VSU

@Pekka - Small note, FPDI can sit on top of TCPDF as well as FPDF IIRC, so via that TCPDF is capable of working with templates in the FPDI fashion.

Written by Orbling

Accepted Answer


I looked in to this previously, I think you can use pdftk (a command line utility), to fill in a PDF form using FDF/XFDF data files, which you could easily generate from within PHP. That was the best option I've seen so far, though there may well be a native library.

pdftk is quite useful in general, worth having a look at.

Update: Have a look here:

Written by Orbling
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the community.
It is licensed under cc-wiki