Pattern matching text in the body of a PDF and adding hyperlinks with PHP

The situation is as follows: I have a series of big, fat PDF files, full of imagery and randomly distributed text - these are the sections of a huge promotional pricelist for a vast array of products. What I need is to pattern-match all the catalogue codes in the text of each PDF file and to wrap it with a hyperlink that will point to the respective page in an online store.

So the task is very simple - scan a PDF file for all plain-text 10 digits sequences, and convert those into links whose href is http://something?code=[match].

I would also prefer to put this together in a PHP script if possible, but any language would do. I have a gut feeling that maybe even flash could be an option.

Any ideas? Thanks in advance.


Some answers coming in are teaching me pcre syntax. The problem here is that I need to search and replace in a PDF file. So the problem is twofold. Say we'll do this in PHP:

  • How do you read / write to a PDF in PHP?
  • As PDFs aren't plaintext files, I can't just regex against them, and I also believe that PDF links are not bundled together with the text but come separate as regions. Which also means that I could maybe overlay an active rectangle over the coordinates of the catalogue code's characters, if I only knew where a matched code resides on a page.

What do you think? Other languages are also an option.



Replacing text in a PDF is difficult and none of the open source PDF solutions support this capability.

Apago ( has a developed commercial solution for replacing text in PDF files. It's used by greeting card manufacturer to modify pricing, "MADE IN" text, product numbers, etc.

$s=" asdf text asdf
echo preg_replace('/(http:\/\/something\.com\?code=(\d+))/s', '<a href="$1">$2</a>',$s);

output 3000 asdf text

5000 asdf

Need Your Help

How to create client socket using domain name

java sockets

Hi I am using java for socket programming. To create client socket I have to pass IP and port. But I want to create client socket by passing domain name and port whether its possible.

Change RadWindow style

silverlight silverlight-4.0 telerik

How do I modify the style or color of RadWindow? Im using SL4 and Telerik's latest binaries which is compatible with SL4.