29 August 2014

Many people include an image of their signature in electronic documents to make them look traditional, and because putting a PGP signature block in a PDF file would be weird and not really that useful. Most of these images don't look much like signatures—they are bitmaps that don't scale well and look blocky and not really like a signature at all. We can do better.

The Bitmap to Vector Miracle called potrace

A while ago Alec Ten Harmsel and I were talking about how cool it would be if you could take a bitmap (a PNG or JPG file) and convert it into a vector file (SVG or PostScript or PDF), and also how hard it would be do to that. A little Google'ing turned up a program that magically does this.

Peter Selinger's potrace (polygon trace) takes a bitmap (a file in PBM, PGM, PPM, or BMP format), applies a lot of math, and writes out a file in one of several formats (the most useful of which, for our purposes, are Encapsulated PostScript (EPS) and PDF).

You will need to download potrace (and maybe some other tools) to follow along, but the results will be worth it. If you are using an Apple computer running OS X, you can use Mac Ports or Brew to install potrace and its dependancies.

Making an image file of your signature

There are three big steps to getting a scalable image file with your signature in it.

  1. Get a digital copy of your signature.
  2. Convert the file to one of potrace's input formats.
  3. Use potrace to make an EPS or PDF file of your signature.

Getting a digital copy of your signature

There are countless ways to do this, but the four easiest ways are:

  1. sign your name in a drawing app on a tablet, and email yourself the image file.
  2. draw your signature in a drawing program on your computer, and save the image file.
  3. sign your name on a piece of plain white paper and take a photo of it with your camera phone and email the photo to yourself. If you choose this way, make sure your signature is as dark as possible and the paper is a white as possible; put the paper by a window or other bright light source to take the photo.
  4. sign your name on a piece of white paper, scan it with a document scanner, and email it to yourself. Even though the scanner may send you a PDF, it isn't a scalable PDF, but a bitmap wrapped in a PDF file.

At the end of this process, you should have a file in PDF, JPG (or JPEG), or PNG format that has your signature in it.

Once you have that file, you should crop it so that your signature has a tight box around it. On a Mac, you can do this using Preview.

Converting the file to one of potrace's input format

potrace takes a limited number of input formats (for the good reason why, see the potrace FAQ list), so the next step is to convert your signature file into one of them. On a Mac, you can use the appropriate command line tool:

If your file is Convert it to  
in this format this format By typing this
PNG PNM pgntopnm file.png > file.pnm
JPG / JPEG PNM jpegtopnm file.jpg > file.pnm
PDF PPM pdftoppm file.pdf > file.ppm

these tools are available on Macs via MacPorts or Brew, on Linux via your preferred package manager, and probably also on Windows.

At the end of this step, you should have a PNM or PPM version of the file with the image of your signature in it.

Use potrace to get a PDF file

potrace is a command-line tool (although graphical interfaces are available) that takes many parameters; the two that matter for our purposes are:

  • -b pdf to specify that the output is to be in PDF format
  • -o signature.pdf to specify that the output file is to be called signature.pdf; you can change that to anything you want

The complete command, assuming you have a PNM file called signature.pnm and you want your output file to be called signature.pdf is:

potrace -b pdf -o signature.pdf signature.pnm


You should now have a PDF file with a scalable version of your signature in it. If you open the original PNG file and the resulting PDF file and zoom in on them, you should see a big difference:


Figure 1: Comparing PNG input (top) and PDF output (bottom) files

Extra Bonus

All in one command line

The command below will skip the generation of the PNM file and go straight from the PNG file to the PDF file.

pngtopnm signature.png | potrace -b pdf -o signature.pdf

On my system, this generates the warning

libpng warning: iCCP: known incorrect sRGB profile

but that doesn't seem to have any effect on the output, so if you see it, don't panic.

Using this signature in a LaTeX letter

If you are using a standard LaTeX letter format and pdflatex, you should include the graphicx package and your signature line should look like:

\signature{\vspace{-3em}\includegraphics[width=10em]{sig.pdf}\\Your Name}

where sig.pdf is the PDF file containing the image of your signature. You may have to adjust the vspace and the width depending on the size of your signature and how you want it to look.

If you are using the NewLFM LaTeX package and pdflatex for your letters, you also need the graphicx package and your signature line looks the same as above, but without the negative vspace:

\signature{\includegraphics[width=10em]{sig.pdf} \\ Your Name}