PDF watermark from Meta data printed on the laser...

Is it possible to print a PDF file to a laser, capturing the file name to use as a watermark?

Apple’s Developer Tools contain an excellent little Python script, which can add watermarks to PDF files. With only one small addition to its code*, we can easily use it for our own purposes.

To show you how to do it, I have created a small sample script named Markoprint. The droplet will first add the PDF’s file name as a watermark and then print it using the «lpr» command to the default printer. If you open the script in the Script editor, you can also change the used font, font size & font color. Please note that the script will not modify the original PDF. The watermark is only added to a temporary file, which is removed after prnting.

You can download the script right here.

The script is by far not perfect yet, but might be an interesting starting point.

Happy Scripting!

*decoding/encoding the text string accordingly

The following code won’t execute, as the script relies on an external Python script. Please download the ready-to-use script above.


property mytitle : "Markoprint"

property fontname : "Courier"
property fontsize : "24"
property fontcolor : "1,0,0" -- RGB values

on open finderitems
	try
		-- searching fro dropped PDF files
		set pdffilepaths to {}
		repeat with finderitem in finderitems
			set finderiteminfo to info for finderitem
			if not folder of finderiteminfo then
				set finderitemname to name of finderiteminfo
				set finderitemkind to kind of finderiteminfo
				if (finderitemname ends with ".pdf") or (finderitemkind contains "PDF") then
					set pdffilepaths to pdffilepaths & (quoted form of POSIX path of finderitem)
				end if
			end if
		end repeat
		-- no PDF files found :-(
		if pdffilepaths is {} then
			set errmsg to "You did not drop any PDF files onto the script."
			my dsperrmsg(errmsg, "--")
			return
		end if
		-- processing found PDF files
		set pyscriptpath to quoted form of POSIX path of (((path to me) as text) & "Contents:Resources:watermark.py")
		repeat with pdffilepath in pdffilepaths
			-- getting the PDF file name
			set command to "/usr/bin/basename " & pdffilepath
			set pdfname to quoted form of (do shell script command)
			-- the path to the temporary PDF file showing the watermark
			set tmpfilepath to quoted form of POSIX path of (my gettmpfilepath())
			-- creating the watermark
			set command to "/usr/bin/python " & pyscriptpath & " --text=" & pdfname & " --font-name=" & quoted form of fontname & " --font-size=" & quoted form of fontsize & " --color=" & fontcolor & " " & pdffilepath & " " & tmpfilepath
			do shell script command
			-- printing the PDF to the default printer
			do shell script "lpr " & tmpfilepath
			-- removing the temporary file
			do shell script "rm " & tmpfilepath
		end repeat
	on error errmsg number errnum
		my dsperrmsg(errmsg, errnum)
	end try
end open

-- I am returning the path to an unused temporary PDF file
on gettmpfilepath()
	set tmpfolderpath to ((path to temporary items folder from user domain) as Unicode text)
	repeat
		set randnum to random number from 10000 to 99999
		set tmpfilepath to tmpfolderpath & randnum & ".pdf"
		try
			set tmpfilealias to tmpfilepath as alias
		on error
			exit repeat
		end try
	end repeat
	return tmpfilepath
end gettmpfilepath

-- I am displaying error messages to the user, hopefully only few
on dsperrmsg(errmsg, errnum)
	tell me
		activate
		display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop with title mytitle
	end tell
end dsperrmsg

Thank you, thank you
But the script assigns a random number as the file name and I need it to pick up the original PDF file name? Is there control in the script for watermark placement? And how can I make sure it will “Fit” to the default page size of Tabloid?

This is going to save so much time. If you could only know.

No, the script creates a temporary PDF file for printing only, but this temporary file contains a watermark stating the name of the original file.

If you have some experience with programming, you can easily adjust the Python script. The placement of the watermark can be controlled in the code and of course you can also make sure that the watermark fits by checking the length of the string against the page size. But it needs some very basic programming skills.

Maybe you will need to find someone to add the final touches, unfortunately I am currently quite busy with my own work. But it is absolutely not difficult.

Just HTML and some Action script 3
Thank you for your time…
But I’m trying to learn:|