LLION/ PDF, print & document engineering

File signatures for document and image formats

LLION Editorial · Updated

A file signature, or magic number, is a fixed byte sequence at a known offset that identifies a format regardless of the file's name. For PDF it is the five ASCII bytes %PDF- at offset 0, hex 25 50 44 46 2D, followed by the version. Checking the bytes is the only reliable way to know what an upload really is.

Signature table#

Offsets are from the start of the file unless stated. ?? is a byte that varies.

Signatures for the formats that turn up in document workflows. ZIP-based formats share one signature and are told apart by their contents (see below).
FormatOffsetHexASCIIExtensionMIME type
PDF025 50 44 46 2D%PDF-.pdfapplication/pdf
PostScript025 21 50 53%!PS.psapplication/postscript
EPS (ASCII)025 21 50 53 2D 41 64 6F 62 65 2D%!PS-Adobe- then EPSF-3.0 on the same line.epsapplication/postscript
EPS (DOS binary, with preview)0C5 D0 D3 C6.epsapplication/postscript
PNG089 50 4E 47 0D 0A 1A 0A.PNG.....pngimage/png
JPEG0FF D8 FF.jpg .jpegimage/jpeg
JPEG/JFIF0FF D8 FF E0 ?? ?? 4A 46 49 46 00JFIF at 6.jpgimage/jpeg
JPEG/Exif0FF D8 FF E1 ?? ?? 45 78 69 66 00 00Exif at 6.jpgimage/jpeg
JPEG 2000 (JP2 container)000 00 00 0C 6A 50 20 20 0D 0A 87 0A....jP .....jp2 .jpximage/jp2
JPEG 2000 (raw codestream)0FF 4F FF 51.j2k .j2cimage/jp2
GIF047 49 46 38 37 61 or 47 49 46 38 39 61GIF87a / GIF89a.gifimage/gif
TIFF, little-endian049 49 2A 00II*..tif .tiffimage/tiff
TIFF, big-endian04D 4D 00 2AMM.*.tif .tiffimage/tiff
BigTIFF049 49 2B 00 or 4D 4D 00 2BII+. / MM.+.tifimage/tiff
BMP042 4DBM.bmpimage/bmp
WebP0 and 852 49 46 46 ?? ?? ?? ?? 57 45 42 50RIFF....WEBP.webpimage/webp
HEIF / HEIC466 74 79 70 then a brand such as 68 65 69 63ftypheic, ftypmif1.heic .heifimage/heic
MP4 (ISO base media)466 74 79 70 then a brand such as 69 73 6F 6Dftypisom.mp4video/mp4
SVG and other XML0 (after an optional BOM)3C 3F 78 6D 6C or 3C 73 76 67<?xml / <svg.svg .xmlimage/svg+xml
UTF-8 byte order mark0EF BB BFany text
RTF07B 5C 72 74 66 31{\rtf1.rtfapplication/rtf
ZIP (also DOCX, XLSX, PPTX, ODT, ODS, ODP, EPUB, XPS, JAR)050 4B 03 04PK...zip and othersapplication/zip or the specific type
ZIP, empty archive050 4B 05 06.zipapplication/zip
OLE2 compound file (DOC, XLS, PPT, MSG, MSI)0D0 CF 11 E0 A1 B1 1A E1.doc .xls .pptapplication/msword and others
gzip01F 8B.gzapplication/gzip
7-Zip037 7A BC AF 27 1C7z.7zapplication/x-7z-compressed
TrueType000 01 00 00.ttffont/ttf
OpenType with CFF outlines04F 54 54 4FOTTO.otffont/otf
TrueType collection074 74 63 66ttcf.ttcfont/collection
WOFF077 4F 46 46wOFF.wofffont/woff
WOFF2077 4F 46 32wOF2.woff2font/woff2
Type 1 font, PFB080 01.pfbapplication/x-font-type1
Type 1 font, PFA025 21 50 53 2D 41 64 6F 62 65 46 6F 6E 74 or 25 21 46 6F 6E 74 54 79 70 65 31%!PS-AdobeFont / %!FontType1.pfaapplication/x-font-type1

HTML has no reliable signature. A document may start with a byte order mark, whitespace, a comment or <!DOCTYPE html in any case, so detection is by parsing, not by bytes.

Telling ZIP-based formats apart#

Office Open XML, OpenDocument, EPUB and XPS are all ZIP archives and share 50 4B 03 04. The reliable second check is the archive's own contents:

  • DOCX, XLSX, PPTX: the archive contains [Content_Types].xml at its root; the specific type comes from the main part it declares (word/document.xml, xl/workbook.xml, ppt/presentation.xml).
  • OpenDocument (.odt, .ods, .odp): the first entry in the archive must be a file named mimetype, stored uncompressed, containing the type, for example application/vnd.oasis.opendocument.text. Because it is first and uncompressed, the type string is readable at offset 38 of the file without unzipping.
  • EPUB: the same mimetype convention with application/epub+zip.
  • XPS: a [Content_Types].xml plus FixedDocumentSequence parts.

A detector that stops at PK reports every one of these as a ZIP file, which is why libraries such as libmagic look inside.

PDF: what the rules say and what readers accept#

ISO 32000-1 §7.5.2 requires the header on the first line: %PDF- followed by 1.N, with %PDF-2.0 for PDF 2.0. §7.5.5 requires the last line of the file to contain only %%EOF, preceded by startxref and the byte offset of the last cross-reference section.

Readers are more tolerant than the text, and two Acrobat implementation notes from the 1.7 reference are effectively the real-world rule: Acrobat requires only that the header appear somewhere within the first 1024 bytes, and that %%EOF appear somewhere within the last 1024 bytes. The UK National Archives' PRONOM signature for PDF 2.0 encodes the same tolerance (header within 4096 bytes, %%EOF within the last 1024), while its PDF 1.7 signature expects the header at offset 0. Consequences:

  • A file with a few junk bytes, an HTTP error page, or a mail header stuck in front of %PDF- will still open in Acrobat and Chrome but will fail strict validators, PRONOM-based identification, and most upload filters.
  • Trailing bytes after %%EOF (a newline, a null, a web server's footer) are common and harmless to readers.
  • Files that contain binary data should have a second line that is a comment with at least four bytes above 127, typically %âãÏÓ or similar, so that transfer tools treat the file as binary (§7.5.2). Its absence is not an error.
  • The version in the header is a claim. The versions reference explains how the catalog can override it.

For validation, check offset 0 strictly, then confirm that %%EOF occurs in the final 1024 bytes. For recovery, search the first 1024 bytes for %PDF- and strip whatever precedes it; qpdf, mutool and pdf.js all do this when opening a damaged file.

Checking signatures#

From a shell:

Shell
# first 8 bytes as hex and ASCII
xxd -l 8 document.pdf
# 00000000: 2550 4446 2d31 2e37 0a                   %PDF-1.7.

# last 16 bytes
tail -c 16 document.pdf | xxd

# libmagic's verdict, which also looks inside ZIP containers
file --mime-type document.pdf
PowerShell
Format-Hex -Path .\document.pdf -Count 8
check_pdf.pyPython
def is_pdf(path: str) -> bool:
    with open(path, "rb") as f:
        head = f.read(8)
        f.seek(0, 2)
        size = f.tell()
        f.seek(max(0, size - 1024))
        tail = f.read()
    return head.startswith(b"%PDF-") and b"%%EOF" in tail
check-pdf.mjs (Node.js 20+)JavaScript
import { open } from 'node:fs/promises';

export async function isPdf(path) {
  const fh = await open(path, 'r');
  try {
    const { size } = await fh.stat();
    const head = Buffer.alloc(8);
    await fh.read(head, 0, 8, 0);
    const tail = Buffer.alloc(Math.min(1024, size));
    await fh.read(tail, 0, tail.length, size - tail.length);
    return head.subarray(0, 5).equals(Buffer.from('%PDF-')) && tail.includes('%%EOF');
  } finally {
    await fh.close();
  }
}

In the browser, the PDF inspector reads the header, the version and the trailer of a file without uploading it.

Why extensions and declared types lie#

An extension is a naming convention; nothing enforces it, and renaming a JPEG to .pdf changes nothing inside. The Content-Type of an upload is whatever the client sent, usually the operating system's guess from that same extension. Mail gateways, scanners and export tools produce PDFs with .PDF, .pdf.txt or no extension at all. So the order of trust for an incoming file is: the bytes, then an actual parse by a PDF library, and only then the name. Serving it back with the right type is the subject of the MIME type reference.

Sources