All formats

PDF (Portable Document Format)

Full namePortable Document Format
Extensions.pdf
MIME typeapplication/pdf
Typefixed-layout document (text, vector graphics, images, fonts, forms, annotations)
Starts with25 50 44 46 2D (%PDF-), followed by the version, e.g. %PDF-1.7
Ends with%%EOF (often followed by a newline)
SpecificationISO 32000-2:2020 (PDF 2.0), free from the PDF Association
Related standardsPDF/A (ISO 19005, archiving), PDF/UA (ISO 14289, accessibility), PDF/X (ISO 15930, print)
Maintained byISO TC 171 SC 2, with the PDF Association
Licenseopen ISO standard, royalty-free

PDF describes pages with a fixed layout: every glyph, line and image has an exact position, so a file looks the same on every screen and printer. A PDF file is a set of objects (dictionaries, arrays, streams) linked by references; pages draw text with embedded fonts, vector paths and images. On top of that PDF supports links, bookmarks, interactive forms, annotations, digital signatures, encryption, attachments, layers and a logical structure tree for accessibility.

PDF is the standard format for documents meant to be read, printed, signed or archived: invoices, contracts, papers, manuals, books, forms and print-ready artwork. It isn’t meant for editing: text is stored as positioned glyphs, not paragraphs, so reflowing or editing a PDF is hard and extracting text in reading order needs heuristics.

When to use PDF#

File structure#

A PDF file has 4 parts: a header, a body of numbered objects, a cross-reference table (xref) with the byte offset of every object, and a trailer that points to the root object and to the xref. A reader starts at the end of the file: it finds startxref, reads the xref, then loads objects on demand.

This is a complete, valid 1-page PDF (the %âãÏÓ comment marks the file as binary for transfer programs):

%PDF-1.7
%âãÏÓ
1 0 obj
<< /Type /Catalog /Pages 2 0 R >>
endobj
2 0 obj
<< /Type /Pages /Kids [3 0 R] /Count 1 >>
endobj
3 0 obj
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792]
   /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>
endobj
4 0 obj
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>
endobj
5 0 obj
<< /Length 41 >>
stream
BT /F1 24 Tf 72 720 Td (Hello, PDF) Tj ET
endstream
endobj
xref
0 6
0000000000 65535 f 
0000000015 00000 n 
0000000064 00000 n 
0000000121 00000 n 
0000000250 00000 n 
0000000320 00000 n 
trailer
<< /Size 6 /Root 1 0 R >>
startxref
411
%%EOF

The first line is %PDF- followed by the version: 1.0 to 1.7 or 2.0. Most readers accept up to 1024 bytes of junk before it. Since PDF 1.4 the catalog can override the header version with a /Version entry, which incremental updates use to upgrade a file without rewriting it. The second line is a comment with 4 bytes above 127, so FTP clients and mail gateways treat the file as binary.

Objects#

The body is a sequence of indirect objects, each N G obj ... endobj where N is the object number and G the generation (almost always 0). Other objects refer to it with N G R. Basic object types:

Type Example Notes
Boolean true, false
Number 42, -3.5 integers and reals
String (Hello), <48656C6C6F> literal or hex; text strings are PDFDocEncoding or UTF-16BE with BOM (UTF-8 with BOM in PDF 2.0)
Name /Type, /Helvetica identifiers, used as dictionary keys
Array [0 0 612 792] any mix of objects
Dictionary << /Type /Page /Count 1 >> name-keyed map; most PDF structures are dictionaries
Stream dictionary + stream ... endstream binary data with a /Length and optional /Filter
Null null
Reference 5 0 R points to indirect object 5, generation 0

Stream data is usually compressed. Common filters: FlateDecode (zlib/DEFLATE, the default for content and fonts), DCTDecode (a JPEG file as is), JPXDecode (JPEG 2000), JBIG2Decode and CCITTFaxDecode (bilevel scans), LZWDecode, RunLengthDecode, ASCIIHexDecode, ASCII85Decode.

Cross-reference table and trailer#

The classic xref table has one 20-byte line per object: a 10-digit byte offset, a 5-digit generation and n (in use) or f (free). Object 0 is always the head of the free list. The trailer dictionary follows: /Size (number of entries), /Root (the catalog), and optionally /Info (document information dictionary), /ID (two file identifiers), /Encrypt and /Prev (offset of the previous xref section). startxref gives the offset of the xref, and %%EOF ends the file.

Cross-reference streams and object streams#

Since PDF 1.5 the xref can be a binary, compressed cross-reference stream (a stream object with /Type /XRef) and its dictionary replaces the trailer. Small objects can be packed into object streams (/Type /ObjStm) and compressed together, which makes files 10-40% smaller. Most modern writers use both. “Hybrid” files have a classic table plus an /XRefStm entry so that PDF 1.4 readers still work.

Incremental updates#

A PDF can be changed by appending new and changed objects, a new xref section and a new trailer whose /Prev points to the old xref. The original bytes stay untouched. This is how annotations and form fill-ins are saved quickly, and it’s required for digital signatures: a signature covers a byte range, and later signatures or changes are appended after it. A file can contain several %%EOF markers; everything after the first one is the history of edits. Rewriting the whole file (a “full save”) drops the history and unused objects.

Linearization#

A linearized (“fast web view”) file starts with a linearization dictionary and stores the first page’s objects first, plus hint tables, so a viewer can show page 1 before the whole file is downloaded. qpdf --linearize in.pdf out.pdf creates one.

Document structure#

Object Purpose
Catalog (/Type /Catalog) the root: points to the page tree, outlines (bookmarks), names (named destinations, attachments, JavaScript), AcroForm, structure tree, metadata, viewer preferences
Page tree (/Type /Pages) balanced tree of page nodes. /Kids lists children, /Count the number of leaf pages. Attributes such as /Resources, /MediaBox, /Rotate are inherited by children
Page (/Type /Page) /MediaBox (page size in points, 1/72 inch; [0 0 612 792] is US Letter, [0 0 595 842] A4), optional /CropBox, /Rotate (multiple of 90), /Resources, /Contents, /Annots
Resources named fonts (/Font), images and form XObjects (/XObject), graphics states (/ExtGState), color spaces, patterns, shadings, used by the content stream
Info dictionary /Title, /Author, /Subject, /Keywords, /Creator, /Producer, /CreationDate, /ModDate. Deprecated in PDF 2.0 in favor of the XMP /Metadata stream in the catalog
Outlines the bookmark tree shown in a viewer’s sidebar
AcroForm interactive form fields; each field has a widget annotation on a page

Content streams#

A page’s content stream is a list of operators in postfix notation: operands first, then the operator. The coordinate origin is the bottom-left corner of the page, y grows upward.

Operators Meaning
q / Q save / restore graphics state
cm concatenate a transformation matrix
m, l, c, re, h path: move, line, curve, rectangle, close
S, f, B, n, W stroke, fill, fill and stroke, end path, clip
rg, RG, k, cs, scn set fill / stroke color (RGB, CMYK, color spaces)
BT / ET begin / end a text object
Tf, Td, Tm, TL set font and size, move text position, set text matrix, leading
Tj, TJ, ' show a string, show strings with kerning adjustments, next line and show
Do paint an XObject (image or form)
BDC / EMC marked content, links content to the structure tree (tagged PDF) and to layers

Text operators show glyph codes in the font’s encoding, not Unicode. Text extraction maps codes back to Unicode through the font’s /ToUnicode CMap or its encoding; when a PDF lacks both, extracted text is garbage even though it renders fine. Words and lines aren’t stored either: extractors rebuild them from glyph positions.

Fonts#

Font type Notes
Standard 14 Times, Helvetica, Courier (4 styles each), Symbol, ZapfDingbats: can be used without embedding. PDF/A forbids non-embedded fonts
Type 1 PostScript fonts (deprecated in PDF 2.0 for new files)
TrueType simple font, 1-byte codes, at most 256 glyphs used per font object
Type 0 (composite) 2-byte (or variable) codes via a CMap, with a CIDFontType0 (CFF) or CIDFontType2 (TrueType) descendant. Used for Unicode text and CJK
Type 3 glyphs defined by PDF content streams (used by TeX for bitmap fonts)

Writers usually embed a subset of the font (only the glyphs used); subset font names start with 6 capital letters and +, e.g. ABCDEF+Arial.

Encryption#

The standard security handler encrypts strings and streams (not the structure) with a key derived from passwords:

Revision Algorithm Status
R2, R3, R4 RC4 40-128 bit, AES-128 (R4) insecure, deprecated in PDF 2.0
R6 AES-256 the current choice (PDF 2.0, also in Acrobat X and later)

Public-key (certificate) encryption also exists. PDF/A forbids encryption.

PDF/A, PDF/UA and other subsets#

ISO publishes profiles that restrict PDF for a purpose. Validate them with veraPDF, the open-source reference validator.

Standard ISO Purpose Main rules
PDF/A-1, -2, -3, -4 19005 long-term archiving all fonts embedded, device-independent color or output intent, XMP metadata, no encryption, no JavaScript, no external content. Conformance levels b (visual), a (also tagged), u (Unicode text). PDF/A-3 allows any embedded file (used by ZUGFeRD/Factur-X e-invoices); PDF/A-4 is based on PDF 2.0
PDF/UA-1, -2 14289 accessibility tagged PDF with a correct structure tree, logical reading order, alternate text for images, real text with Unicode mapping, document title and language
PDF/X 15930 print production embedded fonts, defined output intent (CMYK printing condition), trim and bleed boxes
PDF/E 24517 engineering documents 3D and interactive content
PDF/VT 16612 variable and transactional printing reusable objects for mass personalized printing

Tagged PDF adds a structure tree (/StructTreeRoot) of elements like H1, P, Table, Figure, linked to content with marked-content IDs. Screen readers, reflow on phones and good text extraction depend on it.

Debugging a PDF#

Specifications and documentation#

Software that opens PDF files#

SoftwareWindowsmacOSLinuxWebCanNotes
SumatraPDF✓viewfree, open source, fast; also opens .ai files saved with PDF compatibility
Web browsers✓✓✓viewChrome and Edge (PDFium), Firefox (pdf.js), Safari (PDFKit) have built-in viewers; Edge and Firefox fill forms and add annotations. Also on Android and iOS
Microsoft Edge✓✓view, editdefault PDF viewer on Windows: forms, highlights, ink, read aloud
macOS Preview✓view, edit, convertbuilt into macOS: annotate, fill forms, sign, merge, reorder pages
Adobe Acrobat Reader✓✓viewfree; the reference viewer, forms, signatures. Acrobat Pro (commercial) edits. Also on Android and iOS
Okular✓✓viewfree, open source, annotations and forms
GNOME Papers / Evince✓viewfree, open source, default GNOME viewer (Poppler)
MuPDF✓✓✓view, convertsmall, fast viewer and mutool command line; AGPL or commercial. Also on Android
PDF-XChange Editor✓view, edit, createfree viewer with annotations; editing features are commercial
Foxit PDF Reader✓✓✓viewfree reader; Foxit PDF Editor is commercial. Also on Android and iOS
LibreOffice Draw✓✓✓view, edit, create, convertfree, open source; edits PDF text boxes, exports PDF/A and PDF/UA from Writer
Stirling-PDF✓✓✓✓edit, convertopen source, self-hosted web app: merge, split, compress, OCR, convert
PDF24 Creator✓✓edit, create, convertfree: merge, split, compress, convert; also online tools
qpdf✓✓✓edit, convertcommand line: merge, split, rotate, encrypt, decrypt, linearize, repair
Ghostscript✓✓✓view, convertcommand line: render to images, convert PostScript to PDF, compress, PDF/A; AGPL or commercial
Xodo✓✓view, editfree viewer and annotator, online tools. Also on Android and iOS

Libraries#

Open-source libraries that read, render, create and modify PDF files. See all PDF libraries for a comparison.

C/C++libHaru, MuPDF, PDFium, PoDoFo, Poppler, QPDF
C#iText, PdfPig, PDFsharp, QuestPDF
JavaiText, OpenPDF, PDFBox
Gogo-fitz, ledongthuc/pdf, pdfcpu
Pythonpdfminer.six, pdfplumber, pikepdf, PyMuPDF, pypdf, ReportLab
RubyCombinePDF, HexaPDF, pdf-reader, Prawn
Rustlopdf, pdf (pdf-rs), pdfium-render, printpdf
PHPDompdf, FPDF and FPDI, mPDF, PdfParser, TCPDF