Using pdf2swf to display a watermarked PDF

I had a need to display a PDF on a website the other day. I also needed to add a watermark to the PDF. Here's how I dealt with the problem. Starting with the source pdf I made use of pdftk to add the watermark. The watermark was created as a transparent png and then converted to pdf so it would work as input to pdftk. {{{ pdftk original.pdf stamp watermark.pdf output watermarked_original.pdf }}} Now since I'd never used pdf2swf I assumed that would be it and all I'd have to do is convert the pdf and I'd be done... but unfortunately pdf2swf doesn't really know how to deal with transparent layers in your pdf (or if it does... I couldn't figure out how to make it work). Either way I then had to figure out a process for getting the pdf converted to images and then back into pdf. To convert the pdf to images I employed [http://www.imagemagick.org/ ImageMagick] {{{ convert -density 100 watermarked_original.pdf watermarked_original.pdf }}} And then converted the results back into a pdf using pdfimage {{{ pdfimage -o output.pdf $(ls .png | sort -t "-" -k 2 -n) }}} '''Note:''' The way I used !ImageMagick the file names come out in a sequence which is potentially out of order... that ls and sort command deals with that. This could have been compensated for by using "original_watermarked-%02d.png" as the output name in the convert command (I looked this up while writing this post). {{{ convert -density 100 watermarked_original.pdf watermarked_original-%02d.png }}} '''End Note''' Now all that's left is to convert the pdf to swf using pdf2swf (part of the [http://www.swftools.org/ swftools] package). {{{ pdf2swf -b -l -t output.pdf -o output.swf }}} This is then ideally displayed at 850x1100. You can play around with the !ImageMagick density setting to adjust the resolution. For my needs this worked best since the default resulted in the pdf having too low a quality to be easily readable. Hope this helps someone. '''Update:''' I didn't like pdf2swf's built in viewer... so I went and found another one. I ended up using [https://github.com/netjunki/cfviewer cfviewer] but I forked it and made a couple of changes (most importantly... I made it so it could actually compile... the repositories defined in the pom weren't correct). For use with cfviewer the pdf2swf call needs to be in this form: {{{ pdf2swf -T9 -s insertstop output.pdf -o output.swf }}} For quick processing... if you run this script in a directory where the original pdf is called original.pdf and the watermark is in watermark.pdf: {{{ pdftk original.pdf stamp watermark.pdf output watermarked_original.pdf convert -density 100 watermarked_original.pdf watermarked_original-%02d.png pdfimage -o output.pdf .png pdf2swf -T9 -s insertstop output.pdf -o output.swf }}} '''Update:''' I needed to add a copyright notice to every page as well... I guess I could have done this in the watermark... but opted for using [http://www.graphicsmagick.org/ GraphicsMagick] instead. For some reason (bugs or approach) there are some differences between the behavior of Image and Graphics Magick that have been no end of trouble for me over the years. (There might be a project here at some point to get it so one of the tools does all the things I need properly.) Anyway... to add the copyright notices to each page I used the following command: {{{ gm mogrify -font serif -pointsize 14 \ -draw "gravity southwest fill black text 12,12 'Copyright'" \ watermarked_original-.png }}} So the new quick script looks like: {{{ COPYRIGHT="Copyright" pdftk original.pdf stamp watermark.pdf output watermarked_original.pdf convert -density 100 watermarked_original.pdf watermarked_original-%02d.png gm mogrify -font serif -pointsize 14 \ -draw "gravity southwest fill black text 12,12 '$COPYRIGHT'" \ watermarked_original-.png pdfimage -o output.pdf *.png pdf2swf -T9 -s insertstop output.pdf -o output.swf }}}