AbstractYou want to add a watermark to all PDF created from a PDFprinter. The issueI got a request a few days ago from someone who needed to send invoices and other company documents over email. The problem was that all official documents were printed over pre-printed forms and for the emailed document to bear the company-imposed background, it had first to be printed, then each page scanned. The main issue was, apart from the poor quality of the result, the time spent doing all the printing, scanning and converting manually. There had to be a better solution. While the main application runs under Windows, the printer server is a Linux box. That allowed the creation of a modified PDFprinter to allow documents printed through it to automatically include a watermark. The setupFirst, we will need to create a virtual PDFprinter under Samba on the Linux box. Although all the details are shown below, check the PDFprinter article on this topic for more details on the various ways to do this. You will also need to install Installing pdftk on an old RedHat machine worked well straight from the source tar ball provided at the bottom of the Build page. Note that you may have to run as # wget http://www.pdfhacks.com/pdftk/pdftk-1.12.tar.gz # tar xzvf pdftk-1.12.tar.gz # cd pdftk-1.12 # cd pdftk # make -f Makefile.RedHat Be patient while the software is being made as it takes a little while.
Once compiled, you end-up with a single executable # strip pdftk # chmod 755 pdftk # mv pdftk /usr/bin # pdftk --help The watermarkThe watermark can be any PDF file. If you don't have a clean original source file for your company forms, consider re-making a clean one from a vector package like CorelDraw, Illustrator or even your simple office application rather than scanning an existing printed form: the scanned form will add a lot of weight to your final documents and if you need to use it as a foreground rather than a background watermark, it will completly cover the page underneath. The first testsNow that you have a PDF of your watermark, print PDF of a sample document from your invoicing application (for instance): Then use # pdfdk in.pdf background watermark.pdf output invoice.pdf If you're lucky, the resulting As you can see on the magnified detail, we have a problem: the alignment of the 2 images is off, resulting in the invoice data not being correctly aligned with the pre-printed table or fields present on the watermark (you should also first check that the paper size of your invoice is the same as the watermark to avoid creating more problems). Basic server setup using SambaFor the sake of simplicity, and for those who are lucky enough that they are getting what they need, let' s see how we should declare our PDFWatermark printer.
Make sure that you have a world-writable ( Create the virtual printer under samba by editing your [pdf_out] comment = PDF output path = /tmp/pdf_out read only = No guest only = Yes guest ok = Yes <:vspace> [PDFWatermark] comment = PDF for pre-printed forms path = /tmp/pdf_out printable = Yes guest ok = Yes create mask = 0755 use client driver = Yes print command = fixPS "%s";\ gs -dNOPAUSE -dBATCH -q -sDEVICE=pdfwrite -sOutputFile="%s.tmp.pdf" "%s"; \ pdftk "%s.tmp.pdf" background watermark.pdf output "%s.pdf"; \ rm "%s"; rm "%s.tmp.pdf"; lpq command = The Now add a printer from your Windows box and choose the PDFWatermark on the network. For the printer driver, I get very good results from the HP Color Laserjet 5500 PS. Unfortunately, its drivers are not included by default on Windows XP or even Windows Server 2003 and you may need to download them from the HP website.Another alternative is the already included Apple Laserwriter 12/640, although I generally prefer the former as it allows the creation of large colour PDF files (up to A3 size). Note:I also usually make sure that the printer options in the Advanced and General tabs of the printer properties set the correct paper size and that True type fonts are downloaded to the device rather than substituted. This ensures that the created PDF will look exactly as intended. Now you can print to your virtual PDF printer from any application: the watermark will be systematically added to the background of every page. Fixing the page offsetsNow, for those like me who had the problem the problem with the offset, between the overlayed documents, there is a small fix: the document needs to go though a small filter that will modify the PostScript file passed to Samba from Windows. Copy and paste the following script into a file called #!/usr/bin/perl -w my $file = $ARGV[0]; my $notDone = 1; open IN, $file or die $!; open OUT, ">TMP$file" or die $!; while (<IN>) { print OUT $_; next unless $notDone && $_ =~ /%Page:/; print OUT "10 -13 translate\n"; # modify to suit your offset $notDone = 1; # Change to 0 if effect is cumulative } close OUT; close IN; unlink $file; rename TMP$file $file; This is not a state-of-the-art optimised Perl script, but it does the job nicely:
Now update your Samba configuration file: [PDFWatermark] ... print command = fixPS "%s";\ gs -dNOPAUSE -dBATCH -q -sDEVICE=pdfwrite -sOutputFile="%s.tmp.pdf" "%s"; \ pdftk "%s.tmp.pdf" background watermark.pdf output "%s.pdf"; \ rm "%s"; rm "%s.tmp.pdf"; You should now get a properly aligned PDF: Overlaying the WatermarkYou may want or need to add the watermark on top of the document instead of the background:
To implement this feature is a bit more tricky than previously done: we need to swap the To solve this, we need to break the original PDF into single pages, then apply the overlaid watermark to each page before re-assembling them into a single PDF. Just update your Samba configuration file: [PDFWatermark] ... print command = fixPS "%s";\ gs -dNOPAUSE -dBATCH -q -sDEVICE=pdfwrite -sOutputFile="%s.tmp.pdf" "%s"; \ pdftk "%s.tmp.pdf" burst output PAGE%02d; \ for N in PAGE*; \ do pdftk watermark.pdf background $N output $N.TMP; \ done; \ pdftk PAGE*.TMP cat output "%s.pdf"; \ rm PAGE*; rm doc_data.txt; \ rm "%s"; rm "%s.tmp.pdf"; Consult the Links
Comments peteratbeetleboltdotcomFriday 04 December 2009, at 16:38 GMT+8 [X] wow. that's neat. I was looking for how to watermark with pdftk and now I have watermark *printer* possibilities to think about. Thanks! KapilFriday 21 June 2013, at 12:24 GMT+8 [X] Од 28 November, 2012 - 11:33 pmBuy Oxycontin Online I FeedBurner this in is all as but Added take to in some we further see will look I are to well and There ptinos them heart want thanks article know article more hold but until There it interesting t validity Good time into I opinion don of if some I c |