Watermark Script

This is designed to be used in conjunction with an AssignmentSolutionPHPScript. It is meant to take a source PDF document and modify it to include information about the requesting student.

Use Requirements

This script should be in its own directory, such as /u/csXXX/protectPDF, with permissions 700 to disallow access to anybody except the course account. Any PDFs to request should be individually placed named subdirectories, such as /u/csXXX/protectPDF/assign/assign.pdf. Upon the first request, these PDFs will be split into individual pages; these individual pages will also be placed in the same directory. No further modification of these directories should be needed.

Known issues

This program will leave behind PDFs in the same directory as the watermarking program so they can be retrieved by the requesting program. However, if they are not retrieved, they are left behind as permanent garbage. It is not currently known what to do about this; a subdirectory of /tmp was considered, but this will not work if the calling server is different than the server doing the work (and, in the case of requests from the web server, this really should be the case).

Source Code

While posting the full code here would optimize maximum transfer of knowledge between courses, it's not clear how public the modification details should be. So, for now, the modification details are left out.

#!/bin/bash

#
# Hacked together by bwbecker 20100218
# with extensive help from the examples at www.imagemagick.org
# Thanks to Dan Roche for tipping me off to imagemagick.
#
# Edited by tavaskor 20100224
# primarily to remove a parameter, whitespace-protect variables 
# and other minor changes.

readonly file="$1"
readonly username="$2"

# Hardcode paths to get around deficiencies in Ubuntu server not providing a way
# to get standardized paths.
# Have to use that server to get a recent version of imagemagick; Adobe Reader
# rejects PDFs created by older versions.
readonly CONVERT=/usr/bin/convert
readonly COMPOSITE=/usr/bin/composite
readonly MOGRIFY=/usr/bin/mogrify
readonly READLINK=/bin/readlink
readonly DIRNAME=/usr/bin/dirname
readonly BASENAME=/usr/bin/basename

if [ $# -ne 2 ]; then
   echo "Usage:  $("$BASENAME" "$0") file username" >&2
   exit 2
fi


# Operate in the same directory as this executable.
# Readlink is not really necessary here, but would be if we were
# to save the directory name instead.
cd "$("$READLINK" -f "$("$DIRNAME" "$0")")"

# Convert the original PDF to individual images, one per page.  Put them 
# in a directory with the same name as the file we're delivering but
# minus the ".pdf"  We assume that directory exists and already has the pdf.
# This only needs to be done once.
if [ -d "$file" ]; then
   cd "$file"
   if [ ! -f "$file"-0.png ] || [ "$file".pdf -nt "$file"-0.png ]; then
      "$CONVERT" -density 150 "$file".pdf "$file"-%d.png
   fi
   cd ..
else
   exit -1
fi

readonly tmpdir="/tmp/.watermarking.$UID.$PPID.$$"
mkdir -p "$tmpdir"
chmod 700 "$tmpdir"

#### ...
#### Many modification details here omitted
#### ...

# Convert the watermarked pages back to a single PDF
# Note that we *cannot* just leave this PDF in tmpdir because the 
# temporary directories on the web server and this work server
# will be different; it must be in a normal NFS directory on the
# course account, such as the one that houses this script.
destpdf="$username.$file.$PPID.$$.pdf"
"$CONVERT" "$tmpdir"/"$file"-wm-*.png "$destpdf"
echo $("$READLINK" -f "$destpdf")

# Remove the temporary files
rm -r "$tmpdir"

exit 0
Topic revision: r1 - 2010-03-02 - TerryVaskor
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback