File signatures for document and image formats
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.
| Format | Offset | Hex | ASCII | Extension | MIME type |
|---|---|---|---|---|---|
| 0 | 25 50 44 46 2D | %PDF- | application/pdf | ||
| PostScript | 0 | 25 21 50 53 | %!PS | .ps | application/postscript |
| EPS (ASCII) | 0 | 25 21 50 53 2D 41 64 6F 62 65 2D | %!PS-Adobe- then EPSF-3.0 on the same line | .eps | application/postscript |
| EPS (DOS binary, with preview) | 0 | C5 D0 D3 C6 | .eps | application/postscript | |
| PNG | 0 | 89 50 4E 47 0D 0A 1A 0A | .PNG.... | .png | image/png |
| JPEG | 0 | FF D8 FF | .jpg .jpeg | image/jpeg | |
| JPEG/JFIF | 0 | FF D8 FF E0 ?? ?? 4A 46 49 46 00 | JFIF at 6 | .jpg | image/jpeg |
| JPEG/Exif | 0 | FF D8 FF E1 ?? ?? 45 78 69 66 00 00 | Exif at 6 | .jpg | image/jpeg |
| JPEG 2000 (JP2 container) | 0 | 00 00 00 0C 6A 50 20 20 0D 0A 87 0A | ....jP .... | .jp2 .jpx | image/jp2 |
| JPEG 2000 (raw codestream) | 0 | FF 4F FF 51 | .j2k .j2c | image/jp2 | |
| GIF | 0 | 47 49 46 38 37 61 or 47 49 46 38 39 61 | GIF87a / GIF89a | .gif | image/gif |
| TIFF, little-endian | 0 | 49 49 2A 00 | II*. | .tif .tiff | image/tiff |
| TIFF, big-endian | 0 | 4D 4D 00 2A | MM.* | .tif .tiff | image/tiff |
| BigTIFF | 0 | 49 49 2B 00 or 4D 4D 00 2B | II+. / MM.+ | .tif | image/tiff |
| BMP | 0 | 42 4D | BM | .bmp | image/bmp |
| WebP | 0 and 8 | 52 49 46 46 ?? ?? ?? ?? 57 45 42 50 | RIFF....WEBP | .webp | image/webp |
| HEIF / HEIC | 4 | 66 74 79 70 then a brand such as 68 65 69 63 | ftypheic, ftypmif1 | .heic .heif | image/heic |
| MP4 (ISO base media) | 4 | 66 74 79 70 then a brand such as 69 73 6F 6D | ftypisom | .mp4 | video/mp4 |
| SVG and other XML | 0 (after an optional BOM) | 3C 3F 78 6D 6C or 3C 73 76 67 | <?xml / <svg | .svg .xml | image/svg+xml |
| UTF-8 byte order mark | 0 | EF BB BF | any text | ||
| RTF | 0 | 7B 5C 72 74 66 31 | {\rtf1 | .rtf | application/rtf |
| ZIP (also DOCX, XLSX, PPTX, ODT, ODS, ODP, EPUB, XPS, JAR) | 0 | 50 4B 03 04 | PK.. | .zip and others | application/zip or the specific type |
| ZIP, empty archive | 0 | 50 4B 05 06 | .zip | application/zip | |
| OLE2 compound file (DOC, XLS, PPT, MSG, MSI) | 0 | D0 CF 11 E0 A1 B1 1A E1 | .doc .xls .ppt | application/msword and others | |
| gzip | 0 | 1F 8B | .gz | application/gzip | |
| 7-Zip | 0 | 37 7A BC AF 27 1C | 7z | .7z | application/x-7z-compressed |
| TrueType | 0 | 00 01 00 00 | .ttf | font/ttf | |
| OpenType with CFF outlines | 0 | 4F 54 54 4F | OTTO | .otf | font/otf |
| TrueType collection | 0 | 74 74 63 66 | ttcf | .ttc | font/collection |
| WOFF | 0 | 77 4F 46 46 | wOFF | .woff | font/woff |
| WOFF2 | 0 | 77 4F 46 32 | wOF2 | .woff2 | font/woff2 |
| Type 1 font, PFB | 0 | 80 01 | .pfb | application/x-font-type1 | |
| Type 1 font, PFA | 0 | 25 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 | .pfa | application/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].xmlat 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 namedmimetype, stored uncompressed, containing the type, for exampleapplication/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
mimetypeconvention withapplication/epub+zip. - XPS: a
[Content_Types].xmlplusFixedDocumentSequenceparts.
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:
# 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.pdfFormat-Hex -Path .\document.pdf -Count 8def 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 tailimport { 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
- ISO 32000-1:2008, §7.5.2 File header and §7.5.5 File trailer free copy distributed by Adobe
- Adobe, PDF Reference sixth edition (PDF 1.7), Appendix H implementation notes 13 and 18
- The National Archives, PRONOM fmt/276 (PDF 1.7) and fmt/1129 (PDF 2.0) signatures
- Wikipedia, "List of file signatures"
- W3C, Portable Network Graphics (PNG) Specification (Third Edition), §5.2 PNG signature
- Google, WebP Container Specification (RIFF header)
- Microsoft, OpenType specification, "The OpenType Font File" (sfnt version, OTTO, ttcf)
- W3C, WOFF File Format 1.0 and WOFF File Format 2.0
- Adobe Technical Note 5040, "Supporting Downloadable PostScript Language Fonts" (PFB segment headers)
- Microsoft, [MS-CFB] Compound File Binary File Format
- OASIS, OpenDocument v1.2 Part 3, Packages (mimetype stream)
- W3C, EPUB 3.3, Open Container Format (mimetype file)
- RFC 1952, GZIP file format
- Library of Congress format descriptions for TIFF, JPEG 2000 and EPS