Search code examples
javascriptacrobat

Is it possible to generate a dynamic stamp serial number on PDFs that increases after every download?


We have pdf applications that people download from our website and mail in physical copies. We would like to add dynamic, self-generating serial numbers to these files so that we can better understand things like how long people take to mail them back to us after downloading, and % of downloads that result in conversions.

I have tried the built in Dynamic Stamp tool in Adobe Acrobat, I have found Javascript (see below) that increases incrementally each time the pdf is opened, but it has to be saved locally in order for the number to increase, so that doesn't work. Is there a way to have a physical number that ticks up with every download? Here is the javascript code I have so far:

var f = this.getField("Number");
f.value = Number(f.value)+1;
f.defaultValue = f.value;

Ideally, this would be simple code that adds 1 number to a serial number stamped on the cover of my PDF every time it is downloaded(or a text box with a number in it that increases by 1).


Solution

  • Unfortunately it's not possible to generate serial numbers like this purely within the document because the documents are immutable and have no knowledge of being downloaded.

    To do this, you will need to add a server-side process to alter the PDF content and serve a different file each time it is downloaded. If you store the serial number in the PDF's metadata, you may be able to use exiftool or gs to update it (see this related question).

    Alternatively, it might be possible to use a simple global replace of placeholder text in the file. E.g. if you include the text SERIAL_NUMBER_PLACEHOLDER, you might be able to replace that with the actual number before you serve the file to the client.