Add a Transparent Stamp or Signature to a PDF

Quickly add a transparent image to a PDF file from the command-line on Linux using qpdf or PDFtk.

I stamp my paid paper invoices with one of those self-inking rubber stamps. It helps to quickly see what invoices are paid so I don’t pay twice and… it’s weirdly satisfying. I miss my rubber stamp in my paperless PDF office. I needed a digital rubber stamp to stamp PDF invoices!

The Preview application on macOS lets you fill out and sign PDFs. Very convenient to quickly sign a document and send it back. It’s a simple image, not a digital cryptographically secure signature, but I still need to meet someone who asks for a digital signature instead of a picture of my signature. Anyway, I digress. I looked but it’s surprisingly hard to find a great Preview alternative for Ubuntu/Linux.

A digital rubber stamp? A picture of my signature? What I need is a way to overlay a PDF with a transparent image!

PDF invoice with green PAID stamp in the bottom right corner
PDF overlayed with a transparent image

Designing a custom PDF stamp

I “designed” my custom stamp template in Inkscape and saved it as an A4 PDF file. The stamp needs to be a transparent PDF as both CLI tools below can only overlay PDFs with other PDFs. Be aware that most PDF viewers will show the PDF with a white background, even if the background isn’t there. Don’t ask, the world of PDF parsing is deep and dark.

Custom stamp as SVG, icon from Feather

Why A4? By making the canvas of the image the same size as the source document I can choose the size and position of the stamp on the page.

The same technique can be used to create a whole collection of stamp templates: a ‘draft’ stamp, a ‘sign here’ stamp, a ‘top secret’ stamp, etc.

Overlaying a PDF with a transparent image in the CLI

What is the quickest way to stamp an A4 invoice with a digital rubber stamp in the same location? The command-line! Install QPDF (available from the Ubuntu 20.04 archives) and stamp the invoice with your newly created digital rubber stamp!

  $ qpdf invoice.pdf --overlay stamp.pdf -- outvoice.pdf
  

Quick and easy enough? Download the resulting PDF with an example invoice. It even works for PDFs with editable fields but, the editable fields will be on top of the image (in the PDF viewers I tested).

We could also use PDFtk to achieve the same result. Also available from the Ubuntu archives and you might already have it installed for some other PDF magic.

  $ pdftk invoice.pdf multistamp stamp.pdf output outvoice.pdf
  

We need the ‘multistamp’ and not the ‘stamp’ command here as the later will overlay each page of the input PDF with the first page of the stamp PDF. Meaning each page would have the same overlayed image. I only want to stamp the first page. This can be solved by creating a PDF with the stamp on the first page followed by a lot of blank pages. Hence why the QPDF solution is slightly cleaner.

Adding PDF Keyword Tags

The custom image is a great visual aid but doesn’t help the computer to know if the invoice has been paid. I use ExifTool to add a ‘paid’ keyword to the PDF to easily filter (un)paid invoices.

  $ exiftool -keywords-=paid -keywords+=paid -overwrite_original invoice.pdf
  

The ‘-=’ removes the keyword if it already exists, the ‘+=’ appends the keyword, this to prevent duplicated keywords. This is all wrapped in a little shell function for convenience.

  pay() {
    input_pdf=$1
    qpdf $input_pdf --replace-input --overlay /path/to/stamp.pdf --
    exiftool -keywords-=paid -keywords+=paid -overwrite_original $input_pdf
  }
  

Invoking $ pay invoice.pdf will now add a visual stamp and ‘paid’ keyword to the PDF.

Stamping a PDF in a GUI

What if you prefer a graphical user interface?

LibreOffice Draw comes pre-installed with most Linux distros and can edit PDFs. Don’t use the PDF version of the stamp as LibreOffice Draw does add a white background to the backgroundless PDF, defeating the whole purpose. Luckily, an SVG will just work fine. Make sure to check the result as there is the odd case where Draw can’t fully render the PDF.

Okular is another popular option, it even has a stamp feature! Open the PDF file, choose ‘Tools’ → ‘Review’ and ‘Stamp’ from the newly opened toolbar. Right-click the stamp and choose Properties where you can choose the custom stamp we designed earlier. Note that this feature is experimental and the stamp will not be visible in other PDF viewers!

There is even an Adobe Reader version for Linux! Careful though, the Acrobat Adobe Reader for Linux hasn’t been updated since , and given the security nightmare of the Windows and macOS versions I wouldn’t touch and old and unmaintained Adobe Reader, even if it does exist. One can only hope Evince will one day be as powerful as Preview on macOS.

This post is open source. Did you spot a mistake? Ideas for improvements? Contribute to this post via Github. Thank you!