In this chapter…

…we will discuss extending StampPDF Batch using scripts.

As a command-line driven application, StampPDF Batch may be used within any script or program that can call a command line. When StampPDF Batch is used within CGI scripts it can provide a world wide web interface. CGI, Common Gateway Interface, allows a web server and browser to interact. When a user clicks a web link, the CGI script associated with the link runs. Scripts let you fully automate the process of stamping documents users request from your web site.

Some of the more popular languages for writing CGI scripts include Visual Basic, Perl, Java, PFP, ASP and C.

Sample Scripts

We make sample ASP, Perl, Java, PHP and Visual Basic scripts available for you to review on our web site. The scripts are written for FDFMerge, but can be easily translated for use with StampPDF Batch. The scripts can be found on the Appligent website.

How a Date/Time Stamp Script Might Work

The following steps briefly summarize how you might use a script to stamp the date and time on a PDF document a user downloads from your web site:

  • A user requests a document
  • The Perl CGI module prints a HTML header
  • The script opens and writes parameters and values to a text file, stampfile.txt, that contains the following information:
    — page margins
    — pages to stamp
    — text size and color
    — date and time variables
    — date/time stamp position
  • The resulting stamp file shown in the figure below stamps the current date and time formatted as “Sept 1, 2007 12:30:01” and positions it at the top right of the first page in the PDF document.

The stamp file below stamps the current date and time in a document:

# — Options — Comment
Begin_Options Begin Options Block
Version (2) Stamp file format version 2
TopMargin (2) Top Margin
BottomMargin (2) Bottom Margin
LeftMargin (8) Left Margin
RightMargin (8) Right Margin
End_Options End Options
# — Stamp Item — Comment
Begin_Messge Begin Stamp Item
Name (Demo Item1) Named Demo Item 1
StartPage (1) Start on first page
EndPage (1) Stamp to the first page (stamp only first page)
Size (10) Font Size 10 points
Gray (0) Black text
Text (%b %d, %Y %X) Insert month name, day, year with century and local time
Position (top) Position the top of the text along the top margin
Justification (right) Right justify the text
Underlay (no) Stamp on top of existing text and graphics
End_Message End Stamp Item

The values entered for the Text parameter (%b %d, %Y %X) automatically stamp the current date and time on a document. It is unnecessary for you to retrieve the system date. You may use variables to create a date and time stamp in a variety of formats. For more information, refer to Using variables in stamp files.

The figure below shows an example of a flowchart for an automated CGI script for StampPDF Batch.

Example of CGI script for calling StampPDF Batch

The example flow of the script shown in the figure runs StampPDF Batch with the -o <output file> option so that it creates a new output file and does not overwrite the original file. The new PDF document is stamped with the current date and time in the upper right corner of the first page.

HTML and JavaScript code can be used in the Perl script to return the stamped file to the user’s browser.

Stamping PDF documents using Perl code

Below are example commands for using a Perl script to run StampPDF Batch. 

Scripting the StampPDF Batch command in UNIX

"/usr/local/share/stamppdfapp ", "/usr/local/share/stampdemo.txt", "/usr/local/share/stampdemo.pdf";

This is a sample command. Your flavor of UNIX may require a slight variation.

Scripting the StampPDF Batch command in Windows NT

"c:\\stamppdfapp.exe c:\\stamptext c:\\sample.pdf";

Considering date/time stamp applications

To stamp documents once a day or once a week, you may set up your system to run a Perl script automatically. For example, to create a new stamped PDF document on a UNIX system, you might use cron to run the script on a specific date at a specific time.