Wednesday, October 12, 2011

Watermarking PDF files with GIT commit id

Do you have a pile of old print-outs of PDF documents on your desk that correspond to different versions of the same file, but you can't see which printout corresponds to which version? And the file in question sits in a git repository?

Well, your prayers shall be heard. Here's the answer to that mess: Just watermark each PDF before you print it with the git commit id. You can find out the current git commit id with git describe --always.

How to get that onto you PDF? Either use a pen or pdflatex with the pdfinput and tikz packages. For those of us who don't like to use pens the script below automates that task. Just say (assuming you have named the script commitid and put it to some place in your PATH)

commitid filename.pdf
and the file will be watermarked with the current git commit id (well, something like that).

Enjoy!

#!/bin/bash
# Time-stamp: <2011-10-12 13:45:23 rueffer>
# (c) Bjoern Rueffer 2011

# check parameters for validity:
if [ ! -f "$1" -o $# -ne 1 ]; then
    cat <<EOF
Usage: $0 <pdf file>

This command will take a pdf file located in a git repository and
watermark it with the current commit id.

(c) Bjoern Rueffer 2011
EOF
    exit 1
elif ! git describe --always >/dev/null 2>&1; then
    cat <<EOF
Error: This command must be invoked in a git repository!
(Otherwise I cannot know which commit ID to use.)
EOF
    exit 1
elif !(echo "$1" | grep -i 'PDF$' >/dev/null 2>&1); then 
cat <<EOF
Error: The file appears not to be PDF. I can only process PDF files.
EOF
    exit 1
fi

echo -n Processing "$1" with git commit ID `git describe --always` ...

COMMITID=`git describe --always`
cp "$1" /tmp/pdf_to_stamp.pdf

cat >/tmp/commitid.tex <<EOF
\documentclass{scrlttr2}
\usepackage{pdfpages,pgf,tikz}
\pagestyle{empty}
\begin{document}
\includepdf[fitpaper,pages=-,%
picturecommand={},%
pagecommand={%
  \begin{tikzpicture}[remember picture, overlay]
    \node [xshift=1cm,yshift=-1cm,below right, fill=yellow!50, rounded corners, opacity=.5] at (current page.north west) {%
      commit id: $COMMITID \qquad \today \qquad p.\thepage
    };
  \end{tikzpicture}%
}%
]{/tmp/pdf_to_stamp.pdf}
\end{document}
EOF
(cd /tmp; pdflatex commitid && pdflatex commitid) >/dev/null 2>&1
cp /tmp/commitid.pdf "$1"
rm /tmp/commitid*
echo \ done.

1 comments:

  1. I've enhanced the features even more: now multiple files can be watermarked and also tex and bbl files are supported. Here's a gist: https://gist.github.com/1292802
    ReplyDelete

Comments are moderated to prevent abuse and may not be made public immediately.


Björn Rüffer, Copyright 20092011
Disclaimer: The author of this page cannot accept any responsibility for content of pages that this page provides hyperlinks to. Also the author cannot accept responsibility or liability for any damage caused by following or not following instructions given on this page. This page, its contents and style, are the responsibility of the author and do not represent the views, policies or opinions of any other party. The author will not be held responsible for comments posted by third parties on this site. Inappropriate comments will be removed. There is no warranty for any program or source code provided here, to the extent permitted by applicable law. Except when otherwise stated in writing the copyright holders and/or other parties provide the program “as is” without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program is with you. Should the program prove defective, you assume the cost of all necessary servicing, repair or correction.