Adding stamp to PDF documents
December 2021 (241 Words, 2 Minutes)
Recently I ran into a website that wanted me to upload several documents but it asked me to write my ID on each page of those documents in order to “make processing faster.” I’m not going to debate the intelligence of this system (given especially the fact that the during the upload I am already logged into the system, so the backend ostensibly knows the ID of the person uploading a document), but there was no chance I would “make processing faster” by…
- Printing all documents (some spanning several dozens of pages).
- Writing an ID on top of each freakin’ page.
- Scanning those pages back.
- shredding all those newly printed pages.
Why? Not only it is stupid, it insults my intelligence! Most importantly, I am lazy.
So the challenge is to create such an ID on each page programmatically, which is actually not that hard in Linux.
Ingredients
pdftk
ImageMagick
Inkscape
(in case of Ubuntu, all three are a simple sudo apt install
away).
Create a stamp page
If you want to control the positioning of the stamp, you’ll have to create a PDF page of the same size as pages in the original document. This stamp page should have transparent background and the image of the stamp positioned properly (e.g. at the top center).
The easiest way to create such a stamp PDF page is in Inkscape. Create a new document in Inkscape, then go to File → Document properties and select the page size that corresponds your page size in the document you plan to manipulate. Unless you have changed your settings, Inkscape will use transparent background by default.
Use Inkscape to create stamp graphic of whatever sophistication you need and place it in the proper location of your “page.”
Now, when exporting to PDF, Inkscape doesn’t offer much control over the area to export, so we’ll have to do it in two stages. First the page has to be exported as PNG (File → Export PNG Image). This opens a sidebar with export options. In the “Export area” section, choose “Page,” then export to a PNG file.
Once exported to PNG, convert it to PDF using ImageMagick’s convert
tool:
$ convert watermark.png watermark.pdf
Apply the new stamp page over the original document
This is the easy part:
$ pdftk mydoc.pdf stamp watermark.pdf output mydoc-new.pdf