SUBMIT A FILE TYPE CONTACT US SUPPORT ABOUT US

What Is a PDF File? The Document Format Adobe Made, and How to Check, Fix and Convert One

A PDF file is a document file that keeps its pages the same on every screen and printer. Six readers gave one answer on only 9 of the 21 damaged copies we made of one. Jump to your question below, or work through it in order.

38 min read Updated November 2026 Checked against sources and tests

What Is a PDF File? The document file that keeps its pages the same

Short answer

A PDF file is a document file that keeps its pages the same on every screen and printer. Adobe released the format in 1993, and ISO has kept it since 2008. A PDF file starts with the five characters %PDF- and ends with %%EOF.

We tested 300 public PDF files with six readers, pdfinfo, qpdf, pypdf, pikepdf, pdfminer and pdfium. Of the 286 files without encryption, 238 opened in all six readers. We then damaged one PDF file in 21 ways, and the six readers gave one answer on only 9 copies.

To open one, see the open steps. To check one, see the check steps. If your PDF file will not open, begin with the eight causes.

A PDF file is a document file. It stores pages with text, fonts, pictures and drawings in one file, so the pages look the same on every screen and printer. Adobe released the format in 1993, and ISO has maintained it since 2008.

Picture a bank that emails you a statement as statement.pdf. It opens on your phone, on a laptop and on a printer, and the lines never move. Picture a second file with the same ending that every tool refuses. The ending is the same, and only the bytes inside show which one is sound.

Wikipedia says Adobe developed PDF from 1991 to 2008 and released it on 15 June 1993. ISO 32000-1 appeared in 2008 and matches PDF 1.7. It became an open standard on 1 July 2008. PDF 2.0 is ISO 32000-2.

A PDF file is not a text file. Wikipedia explains that the text lives in content streams, and that fonts can be stored inside the file or left out. Pictures and vector drawings live in the same file, and the format supports encryption and digital signatures.

The file ends with a cross reference table and a short trailer. RFC 8118 registered the type application/pdf in March 2017. It describes how a reader treats scripts, attached files and links, and the section on safety uses it.

The IANA record lists application/pdf with the ending .pdf. It names ISO 32000-2, which is PDF 2.0, as the most recent specification. Our file command printed the same type for our example.

Where our PDF files come from

We read 300 files from 2 public projects. pdf.js gives 150 and qpdf gives 150. We chose them at random from the files under 150,000 bytes in every project, and all 300 end in .pdf.

They are test files, and many exist to test readers on odd cases. They are 23 to 120,681 bytes long and 4,911,254 bytes in total, and the median is 6,621 bytes. The longest has 30 pages.

So this page studies how readers treat the PDF layout, and not a sample of documents from everyday downloads. We also made one file of 1,847 bytes and damaged it in 21 ways. We opened nothing in a browser or an editor.

PDF file facts at a glance

Quick facts about PDF files
Property Value
Meaning Portable document. It stores pages with text, fonts, pictures and drawings.
Ending .pdf, per IANA.
Maker Adobe developed it. It was released on 15 June 1993, per Wikipedia. ISO has maintained it since 2008.
Layout A header, numbered objects, a cross reference table, a trailer and an end marker, per Wikipedia.
Kinds Classic, compact, linearized and encrypted. 222, 32, 32 and 14 of our 300 files.
Type application/pdf, per IANA. The file command printed the same type for our example.
First bytes %PDF- and a version. Our example says 1.3.
Our tests 300 files, 21 damaged copies, 6 readers, 1 repair route and a script of 26 lines.

Wrong page? A ZIP file starts with the letters PK, and a PNG file starts with the bytes 89 50 4E 47. See how PDF files differ from images.

Which Kind of PDF File Do You Have? Four kinds, and the structure names the kind

Four kinds name the build of a PDF file. In our 300 files, 222 are classic, 32 are compact, 32 are linearized and 14 are encrypted. All of them end in .pdf, so the ending names none of the kinds.

Check how the file stores its objects and follow the figure. We tested all four kinds with six readers. The encrypted kind is the one that splits them, because only 3 of the 14 encrypted files opened in all six readers.

Decision chart with four kinds of PDF file, a classic file with a cross reference table, a compact file with object streams, a linearized file and an encrypted file, with the number of our 300 test files of that kind and how many of them all six readers opened.
Figure 1. Four kinds of PDF file in our 300 test files.

The four kinds in detail

Four kinds of PDF files and how six readers handled them
Kind Files and results
Classic 222 files. The file has an ordinary cross reference table and no packed objects. All six readers opened 180 of them.
Compact 32 files. The file packs objects into streams, a method that began with PDF 1.5. All six readers opened 28 of them.
Linearized 32 files. pikepdf marks the file as linearized, which Wikipedia also calls optimized or web optimized. All six readers opened 30 of them.
Encrypted 14 files. The file asks for a password or stores its data in locked form. All six readers opened 3 of them.

Start with the first bytes

Open the file in a hex viewer, or use xxd -l 32 example.pdf on Linux or Mac. Our file starts with 25 50 44 46 2d, the characters %PDF-. Then come the version 1.3 and a line break.

The first bytes and the type of one PDF file
xxd -l 32 example.pdf
00000000: 2550 4446 2d31 2e33 0a25 938c 8b9e 2052  %PDF-1.3.%.... R
00000010: 6570 6f72 744c 6162 2047 656e 6572 6174  eportLab Generat
file -b example.pdf
PDF document, version 1.3, 2 page(s)
file -b --mime-type example.pdf
application/pdf

The file command reads the bytes and not the ending. It printed the version and the page count for our example. We did not use it on all 300 files, so we make no claim about its answers on the others.

qpdf --check prints the version and says whether the file is encrypted or linearized. For our example it says File is not encrypted and File is not linearized, and it found no syntax errors.

First bytes first %PDF- at byte 0 and a version number mean a PDF file.
The ending last All 300 files end in .pdf, and 14 of them are encrypted.
Encryption splits readers Only 3 of 14 encrypted files opened in all six readers.
Versions vary 93 files say 1.3 and 66 say 1.7. 3 say 2.0.

Not sure your file is a PDF file at all? Check it with our script.

What Is Inside a PDF File? A header, objects, a table and a trailer

A PDF file is a header, numbered objects, a cross reference table, a trailer and an end marker. Our example is 1,847 bytes long. The table starts at byte 1434, and the startxref line near the end says so.

We looked inside a PDF file because its layout explains every result later on this page. We made example.pdf with ReportLab. It has two pages and a few short lines of text. The start shows the header, and the end points back to the table.

The start and the end of one PDF file
xxd -l 32 example.pdf
00000000: 2550 4446 2d31 2e33 0a25 938c 8b9e 2052  %PDF-1.3.%.... R
00000010: 6570 6f72 744c 6162 2047 656e 6572 6174  eportLab Generat
tail -c 36 example.pdf | xxd
00000000: 2052 0a2f 5369 7a65 2031 300a 3e3e 0a73   R./Size 10.>>.s
00000010: 7461 7274 7872 6566 0a31 3433 340a 2525  tartxref.1434.%%
00000020: 454f 460a                                EOF.

Read the end first. The last bytes spell startxref, a line break, the number 1434, a line break and %%EOF. A reader jumps to byte 1434, finds the word xref and reads one line for every object.

Diagram of one PDF file of 1,847 bytes in five parts: the header, the numbered objects, the cross reference table, the trailer and the startxref line with the end marker, with three rules about offsets, trailing bytes and the header position.
Figure 2. One PDF file from our tests, example.pdf. The parts are not drawn to scale.

The parts, one by one

The table has 10 lines of 20 bytes in ours, and the first line is the free object 0. A line like 0000000061 00000 n gives the byte where object 1 starts. The trailer names the catalog and counts the lines.

The nine objects store the font, the two pages, the page list, the catalog, the info record and two content streams. The content streams store the text as commands. That is why a PDF file is not a text file.

What every part stores

Parts of PDF files and what we saw
Part What it stores
Header The characters %PDF- and a version. Copy A1 changes the P to X, and qpdf and pdfium refused it.
Bytes in front Ours has none. Copy A2 adds 5 junk bytes and all six readers read it. Copy A3 adds 2000, and qpdf and pdfium refused it.
Objects Nine in ours. Copy C5 cuts 100 bytes from inside one, and qpdf and pypdf refused it.
Cross reference table One line for every object. Copy C4 changes one line, and pdfium refused it while pdfminer returned another page count.
Trailer Names the catalog and the size. Copy B2 cuts the last 100 bytes, and only pikepdf read it.
Startxref The byte where the table starts, 1434 in ours. Copy B6 adds 50 and copy B7 sets it to 0, and only qpdf refused them.
End marker The characters %%EOF. Copy B3 removes them, and only pypdf refused it.

What the sources say

Wikipedia describes the same five parts. Version 1.5 added an optional binary cross reference stream, and some objects can live in object streams. Our compact files use those, and our example does not.

We read the layout from our own files and from Wikipedia. We did not read ISO 32000, so we make no claim about rules in it.

The map is small. Our script checks the header, the end marker, the startxref line and the objects. It cannot see inside a stream, so a damaged page can pass it, as the 21 copies show.

What Did We Find in 300 Test PDF Files? Six readers, and encryption split them

We read 300 public test files with six readers and a 26 line script. All six readers opened 241 files. 13 files failed in all six, and 6 of those are encrypted. Of the 286 files without encryption, 238 opened in all six readers.

All 300 files come from 2 public projects, so this is a study of those projects and not of documents in general. Many files exist to test readers. A pass means exit code 0, or qpdf exit code 3 for warnings, and a page count above 0.

Readers were pdfinfo 24.02.0, qpdf 11.9.0, pypdf 5.9.0, pikepdf 10.13.0, pdfminer.six 20260107 and pypdfium2 5.13.0, all on Linux. We tested every file alone, in a fresh folder. pikepdf is built on the qpdf library, so those two answers are not independent.

300 public PDF files read
6 readers on every file
21 damaged copies of one file
15 copies our script flagged or warned on
Bar chart of 300 public test PDF files by size, from 23 bytes to 120,681, next to the number of files that every one of six readers opened, a grid of results by kind of file, and four measured facts.
Figure 3. 300 files, six readers and four measured facts.
What eight checks found in 300 files
Test Result
pdfinfo 24.02.0 Opened 285 of 300 files. It is part of poppler, and it reads the page count from the file.
qpdf 11.9.0 Opened 249 files. It printed warnings on 67 files that it still read, and it refused 26 files that all five others read.
pypdf 5.9.0 Opened 278 files. It read the text of page 1 and tried an empty password on encrypted files.
pikepdf 10.13.0 Opened 281 files, the most after pdfinfo. It is built on the qpdf library.
pdfminer.six Opened 278 files. It read the layout of every page, not only the count.
pypdfium2 5.13.0 Opened 280 files. It also drew page 1 at a small size.
Our script Printed OK for 282 files, WARN for 8 and FLAG for 10. It printed FLAG for no file that all six readers opened.
Sizes 23 to 120,681 bytes, with a median of 6,621 and a total of 4,911,254. 44 files are under 1,000 bytes and 34 are 50,000 bytes or more.

Four kinds, four outcomes

222 files are classic, 32 compact, 32 linearized and 14 encrypted. The four kinds predict how readers do. Compact and linearized files opened almost everywhere. Classic files had most of the failures by count, and encrypted files failed at the highest share.

Of the 14 encrypted files, 3 opened in all six readers, and the other 11 stopped at least one. We did not test passwords. We cannot say whether the 11 are sound, because a reader without the password has nothing to read.

Where the six readers split

Of the 286 files without encryption, 238 opened in all six readers and 6 failed in all six. The other 42 split the readers. 26 of the 42 failed in qpdf alone, so qpdf is the strictest reader in our set.

So refusing a file does not always mean the file is bad. qpdf wanted a file with no errors, and five readers read the file anyway. Test a file that one reader refuses with a second reader before you give up on it.

Passing does not mean the same pages

Of the 286 files that two or more readers opened, all but 2 gave one page count in every reader that opened them. bad12.pdf gave 2 pages in pdfminer and 1 in the other five. duplicate-page-inherited.pdf gave 1 in pdfminer and 2 in the other five.

We did not check which count is right. A pass shows that a reader found pages. It does not show that two tools agree about the file.

Files without the first bytes

6 files do not start with %PDF- at byte 0. 5 of the 6 failed in all six readers. The sixth, issue6069.pdf, opened in 4 readers. Our script printed FLAG for all 6, so the first bytes are a quick test.

Our script on the 300 files

Our script printed FLAG for no file that all six readers opened, and WARN for 1 of the 241. It printed FLAG or WARN for 7 of the 13 files that failed in all six. The other 6 are encrypted files, and the script printed OK for them.

It printed OK for 36 of the 46 files that split the readers. So the script cannot say which reader will refuse a file. It reads the layout and not the contents, which the damaged copies show again.

Readers differ on healthy files The six readers split on 46 of 300 files, and 26 of those failed in qpdf alone.
A short check is cheap Our 26 line script reads the header, the end marker, the startxref line and the objects.
Encryption needs the password Only 3 of 14 encrypted files opened in all six readers.
No browsers tested We opened nothing in a browser or an editor, so passes show a readable file for six tools only.

Read the study with care. It shows what six open readers do with 300 files from 2 projects. A browser, an editor or a phone can be stricter or more lenient than any of them, and we had none of those. Files from other sources can differ from ours.

What Happens When a PDF File Is Damaged? 21 broken copies, and the six readers agreed on 9

We damaged one PDF file in 21 ways. The six readers gave one answer on 9 copies and split on 12. Our script flagged or warned on 15. Our repair script restored 9 of the 14 copies that needed a repair, and it printed OK on 3 copies that still stopped a reader or lost text.

The 21 copies come in three groups of 7. Group A changes the header, like the first characters and the bytes in front. Group B changes the end, like the marker and the startxref line. Group C changes the content, like bits, a stream length and a table entry. Every copy changes one thing in example.pdf.

We asked three questions about every copy. Did a reader find 2 pages, as on the original? Did our script flag it? Did one script bring back a file that all six readers read, with the original text?

Grid of 21 damaged copies of one PDF file against pdfinfo, qpdf, pypdf, pikepdf, pdfminer, pdfium, our check script and a repair test. The six readers gave one answer on 9 copies, our script flagged or warned on 15, and our repair script restored 9 of the 14 copies that needed one.
Figure 4. 21 damaged copies, six readers, our script and a repair test.

Copies that split the readers

12 copies split the readers. qpdf refused 11 copies in all and read 10, and pdfium refused 7. pikepdf refused only 1. A1 changes the P in %PDF to X, and qpdf and pdfium refused it while the other four read it.

B2 cuts the last 100 bytes, which removes the end of the trailer, the startxref line and the marker. Only pikepdf read it. pypdf alone refused B3, which removes only %%EOF. A reader that rebuilds its own table can read a file that another one refuses.

Copies every reader accepted

8 copies read the same in all six readers. A2 adds 5 bytes in front and A4 sets the version to 9.9. B1 cuts the last byte, and B4 and B5 add zeros and a text line at the end. C1 flips one bit, C2 zeroes 64 bytes and C6 swaps two bytes.

So junk at the end of a file does not stop a reader. A strict check can still object. Our script warned on A2, A4, B4 and B5, and pdftotext gave the original text for all four.

The copy every reader refused

Only C7 was refused by all six readers. It cuts the file to half its size, so the objects, the table and the trailer are gone. The readers have no safe way to guess what the missing half said.

Readers agreed, and the text was still lost

C2 zeroes 64 bytes inside a page stream. All six readers found 2 pages, and pdftotext printed only part of the text. The header and the table were sound, so no reader could know that the page data was wrong.

That is why a pass proves little. Our script printed OK for C2, C3 and C4. C3 sets a stream length wrong, and qpdf refused it. C4 changes a table entry, and pdfium refused it while pdfminer returned another page count.

Repairs that worked

Our script of 18 lines restored 9 of the 14 copies that needed a repair. They are A1, A3, A5, A6, B3, B6, B7, C3 and C4. After the repair, all six readers found 2 pages, and pdftotext gave the original text.

It cuts bytes in front of the header, adds a header when none exists and trims bytes after the last %%EOF. Then qpdf rebuilds the table. Every one of the five files we checked came out at 1,683 bytes, and the original has 1,847, so the rebuild is not byte for byte the original.

We found no official repair command. pdfinfo, pypdf and the other readers only read the file. qpdf rebuilds the table, and that was the one step we needed. We did not test the repair features of any editor or browser.

What no command restored

B2 and C7 lost their ends or their halves, and qpdf could not rebuild either one. A7 zeroes the first 100 bytes, and C2 zeroes 64 bytes inside a stream. C5 cuts 100 bytes from inside one. Pages came back for A7 and C2, and part of the text did not.

Ask for the file again in all five cases. No rule shows what the lost bytes said. Our script restored none of them, which leaves 5 of the 14 that needed a repair.

Readers disagree The six readers gave different answers on 12 of 21 copies.
Tables can be rebuilt 9 copies with a bad header, end or table came back readable by all six.
A page count is not proof All six readers found 2 pages in C2, and text was still lost.
Content is lost Cut bytes and zeroed stream data cannot be restored by any rule.

Read the grid with care. Every copy changes one thing in one small file. Damage in the wild changes many things at once. We opened no copy in a browser, and our repairs rely on rules that fit the file we made.

What Does Our Expert Say About PDF Files? A note from Neeraj Singh on trying a second reader

Check a PDF file with two readers before you trust it. Our six readers gave one answer on 9 of 21 damaged copies. All six found the right page count in a copy whose page data was zeroed.

Neeraj Singh

Expert insight

“A PDF file can pass a quick test and still lose its text. Our six readers gave one answer on only 9 of 21 damaged copies, and all six found 2 pages in a file whose page data was zeroed. Compare two readers, and ask for a new file when the page looks wrong.”

Neeraj Singh Manager, Tech Support & Operations 19+ years fixing Windows and system errors

Why one reader is not enough

A reader can return a page count without an error. All six did that for C2, and pdftotext still printed only part of the text. A refusal at least warns you that something is wrong. A page with missing text does not.

If your PDF file opens in the program you trust and the page looks right, treat that as the best test. Use the readers on this page when it does not. pdfinfo and pikepdf opened the most files, and they use different code, so they make a good pair.

Copy your PDF file before you repair it. Work on the copy and keep the original untouched. Write down its SHA-256 hash first, so you can show later which file you started with.

How Do You Open a PDF File? A viewer, six readers and what we tested

A browser or a viewer opens most PDF files. We also tested six readers on Linux. pdfinfo opened 285 of our 300 files, and qpdf opened 249. 241 files opened in all six, and the 13 that failed in all six include 6 encrypted files.

The table lists the ways to open a PDF file. Rows with version numbers are tools we tested on Linux. We tested no browser and no editor, and we opened nothing on Windows or macOS.

Programs that open PDF files
Program What it does
A web browser We opened no file in a browser, so we make no claim about one. pdf.js is the project that gave us 150 test files, and we did not use it as a viewer.
pdfinfo 24.02.0 A command from poppler. It opened 285 of our 300 files and prints the page count and the page size.
qpdf 11.9.0 A command line tool. It opened 249 files and is the strictest of the six. qpdf --check lists problems.
pypdf 5.9.0 A Python library. It opened 278 files and reads text. It tried an empty password on encrypted files.
pikepdf 10.13.0 A Python library built on qpdf. It opened 281 files and reads the objects inside.
pdfminer.six A Python library for text and layout. It opened 278 files.
pypdfium2 5.13.0 A Python library built on pdfium. It opened 280 files and draws pages.
The file command It read the bytes and printed the version and the page count for our example, plus application/pdf.
Editors and viewers We tested none, so we make no claim about which versions read PDF files.

Open a PDF file in a viewer

If you only want to read the pages, drag the file into the viewer you trust. We tested no viewer, so we name none that fails. A viewer can be stricter or more lenient than any of our six, and we have no test of that.

If the viewer shows nothing, the file can be damaged or can be another kind of file with the wrong ending. Look at the first bytes, or use our script to see what is wrong.

Open a PDF file with Python or pdfinfo

Four lines of Python load the file and print the page count, the text of page 1 and the title. The pdfinfo command prints the facts without any code. These are the actual outputs for example.pdf.

pypdf on one PDF file
from pypdf import PdfReader
r = PdfReader('example.pdf')
print(len(r.pages), r.pages[0].extract_text())
2 Page one. The quick brown fox jumps over the lazy dog.
Order 1001 total 42.50
pdfinfo on one PDF file
pdfinfo example.pdf
Title:           Test File
Producer:        ReportLab PDF Library - (opensource)
Pages:           2
Encrypted:       no
Page size:       612 x 792 pts (letter)
File size:       1847 bytes
Optimized:       no
PDF version:     1.3

The page count 2 matches the two pages we made. A reader gives you the pages, and a viewer decides how to show them.

Open an encrypted PDF file

Only 3 of our 14 encrypted files opened in all six readers, and we gave no password. pypdf tried an empty one. If a file asks for a password, ask the sender. We did not test any tool that guesses passwords, and we do not suggest one.

What to use for every task

Read the pages. Use the viewer you trust. Count the pages. Use pdfinfo. Read the text. Use pypdf or pdftotext. Check the structure. Use qpdf. Test a file that one reader refuses. Try a second reader.

Install programs from their makers. Take libraries from their own project pages. A free web tool receives your document, so try the steps on this page first.

How Do You Check a PDF File? Six steps and a script of 26 lines

Check a PDF file in six steps. Look at the first bytes, name the version, use a script and compare two readers. Our 26 line script printed OK for 282 of 300 files and flagged or warned on 15 of 21 damaged copies.

The first looks at a PDF file need no Python. The file command names the version, pdfinfo lists the facts and qpdf --check looks for errors. We used all three on example.pdf.

The version, the type and the structure of one healthy PDF file
file -b example.pdf
PDF document, version 1.3, 2 page(s)
file -b --mime-type example.pdf
application/pdf
qpdf --check example.pdf
PDF Version: 1.3
File is not encrypted
File is not linearized
No syntax or stream encoding errors found; the file may still contain
errors that qpdf cannot detect

A healthy file starts with %PDF- and ends with %%EOF, and the startxref number points at the table. Wikipedia gives the same layout, so a file that breaks these points is damaged, or it is a test file made to break readers.

Check PDF files with Python

The script reads the first and last bytes and finds the startxref number. It checks that the number points at a table or an object. It warns about bytes in front of the header, bytes after the end marker and objects that do not match.

It prints OK, WARN or FLAG, and it never changes a file. The limit of 1024 bytes for the header is our own choice, and we did not check it against the standard.

Check the header, the end marker and the startxref line of a PDF file
import sys, re
def peek(b):
    n, w = len(b), []
    h = b.find(b'%PDF-')
    if h < 0: return 'FLAG', 'no %PDF- header'
    if h > 1024: return 'FLAG', 'header starts at byte %d, past the first 1024' % h
    if h: w.append('%d junk bytes in front of the header' % h)
    v = b[h + 5:h + 8]
    if not re.match(rb'[12]\.\d$', v): w.append('odd version %r' % v.decode('latin1'))
    e = b.rfind(b'%%EOF')
    if e < 0: return 'FLAG', 'no %%EOF marker'
    if n - e > 8: w.append('%d bytes after the end marker' % (n - e - 5))
    s = b.rfind(b'startxref')
    m = re.match(rb'startxref\s+(\d+)', b[s:s + 40]) if s >= 0 else None
    if not m: return 'FLAG', 'no startxref number'
    o = int(m.group(1)) + h
    if not re.match(rb'\s*(xref|\d+\s+\d+\s+obj)', b[o:o + 30]):
        return 'FLAG', 'startxref %s points at nothing useful' % m.group(1).decode()
    a, z = len(re.findall(rb'\d+\s+\d+\s+obj\b', b)), len(re.findall(rb'\bendobj\b', b))
    if a != z: w.append('%d obj but %d endobj' % (a, z))
    d = '%d objects, version %s' % (a, v.decode('latin1'))
    if w: return 'WARN', '; '.join(w) + ', ' + d
    return 'OK', d
if __name__ == '__main__':
    for p in sys.argv[1:]:
        r, m = peek(open(p, 'rb').read()); print(p, r, m)

Here is what it printed for our example and for four damaged copies.

One healthy PDF file and four damaged copies
python3 pdf_peek.py example.pdf
example.pdf OK 9 objects, version 1.3
python3 pdf_peek.py A2.pdf B6.pdf C2.pdf C3.pdf
A2.pdf WARN 5 junk bytes in front of the header, 9 objects, version 1.3
B6.pdf FLAG startxref 1484 points at nothing useful
C2.pdf OK 9 objects, version 1.3
C3.pdf OK 9 objects, version 1.3

A2 has 5 bytes in front and gets a warning. B6 says the table starts at byte 1484, and nothing is there. C2 and C3 print OK, and both still break something. C3 stops qpdf, and C2 loses text.

What the script found in our files

Our script on damaged PDF files
Result Cases
Printed OK (6 of 21 copies) B1, C1 and C6 read fine in all six readers. C2, C3 and C4 still stop a reader or lose text, so OK is not a verdict.
Printed FLAG (11 of 21 copies) The script names the first problem it finds, like no header or a startxref number that points at nothing.
Header (A1, A3, A5, A7) The script names a missing %PDF- header, or a header that starts at byte 2000. qpdf and pdfium refused all four.
End marker (B2, B3, C7) The script names a missing %%EOF. B2 cut the last 100 bytes, B3 removed the marker and C7 cut the file in half.
Startxref (A6, B6, B7, C5) The script says the number points at nothing. A6 changed the line ends, B6 added 50, B7 set 0 and C5 cut bytes from inside.
Warned (A2, A4, B4, B5) Junk in front, version 9.9, and bytes after the end marker. All six readers read all four copies.
Printed OK but stopped a reader (C3, C4) A wrong stream length and a changed table entry. qpdf refused C3, and pdfium refused C4.
Printed OK but lost text (C2) Zeroed bytes in a page stream. All six readers found 2 pages, and pdftotext gave only part of the text.

The script has limits. It cannot see inside a stream, and it cannot know whether a table entry is right. It printed OK for 3 copies that still stop a reader or lose text. The repair routes fix the kinds of damage we tested.

Check order for any PDF file

  1. Copy your PDF file, write its SHA-256 hash and work on the copy.
  2. Look at the first bytes with xxd -l 32. %PDF- and a version is a PDF file. See the four kinds.
  3. Name the version and the page count with file -b and pdfinfo.
  4. Use our script. The first word is OK, WARN or FLAG, and a flag names the problem.
  5. Open the file in two readers and compare the page count. The open steps show how.
  6. Look at the pages. We opened nothing in a browser, so passes show a sound layout for six readers only.

Is a PDF File Safe to Open? Looking is safe, and the viewer needs care

A PDF file is page data, so looking at one starts nothing. The risk lives in what a viewer starts. In our 281 openable files, 6 have JavaScript and 13 have attached files. We scanned none of our files for malware.

Our readers loaded files and read pages in empty folders, and the test files are public. We started no file as a program, we scanned none for malware and we opened none in a browser.

The RFC list of risks comes next. A PDF file can include scripts that the reader starts, so untrusted code needs a protected place. It can store files of any type, and readers must not start attached programs. It can point to other files and to sites on the internet.

Wikipedia adds that embedded JavaScript has been a source of reader flaws, and that some experts recommend turning it off. We tested no viewer setting, so we give no steps for one.

What we found in our own files

We scanned the 281 files that pikepdf could open. 6 have JavaScript and 13 have attached files. 16 have an open action, which asks a viewer to do something when the file opens. 9 have web links. 0 have a launch action, and 36 have forms.

These are test files, and many were made to test those features. Do not read the counts as a share of everyday files. They show that the features exist in files made by others, and that a quick look at the first bytes cannot see them.

Reading the first bytes Safe. xxd, file and our script read bytes and start nothing inside the file.
Writing down hashes Safe. A hash shows which copy you have.
Opening it in a viewer A viewer reads untrusted data. Keep it current, and check whether it starts scripts. We tested no viewer.
Trusting the ending The ending says little. A file named .pdf can contain another kind of data, so read the first bytes.
Opening attached files A PDF file can store any type of file, per RFC 8118. Do not open an attachment from a sender you do not know.
Uploading to a free tool The site gets your document. Use a program you installed from its maker.

Look at the file before you open it

Check the first bytes and the size. A proper PDF file starts with %PDF- and a version. If you see something else, check it with file -b and do not open it in an editor until you know what it is.

Our biggest test file is 120,681 bytes. We did not test large files, so we give no size limit of our own.

A PDF file from someone else

Read the first bytes and use the script. Open the file in two readers and look at the pages. Work on a copy, and keep your software current.

Share a PDF file with a helper

Send the PDF file and the output of our script and pdfinfo. The first word of the script output shows a helper whether the layout is sound. Keep the original, and send a copy.

Not sure? Check it first. Copy the hash, look at the first bytes with our check steps and ask the sender for a fresh file if anything looks wrong. Open it last, on a copy.

Why Will Your PDF File Not Open? Eight causes, and what to do about every one

Most PDF files that will not open fail for one of eight reasons. Look at the first bytes, use the script and try a second reader. Our script flagged or warned on 15 of 21 damaged copies, and our repair script restored 9 of the 14 that needed a repair.

We damaged one PDF file in 21 ways and tested a repair script on the copies that needed one. The table groups what we saw into eight causes.

Every row says what happens to your PDF file and what to try next. Fixes work on copies, and none changes the original. We opened no repaired file in a browser, so a repaired file shows only that six Linux readers can read it.

Eight causes of PDF files that will not open
Cause Result and fix
A missing or changed header (3) A1 changes the P to X, A5 removes the header line and A7 zeroes the first 100 bytes. qpdf and pdfium refused all three. Fix: Our script adds the header. A1 and A5 came back readable by all six readers. A7 got its 2 pages back, and the text did not match.
Junk in front or an odd version (3) A2 adds 5 junk bytes, A3 adds 2000 and A4 sets the version to 9.9. All six readers read A2 and A4. qpdf and pdfium refused A3. Fix: Our script cuts the junk, and A3 came back readable by all six. A2 and A4 needed no fix.
A missing end marker (1) B3 removes %%EOF, and pypdf alone refused it. Fix: Our script adds the marker again, and B3 came back readable by all six.
A file that is cut short (3) B1 loses the last byte, and all six readers read it. B2 loses the last 100 bytes, and only pikepdf read it. C7 keeps half the file, and all six refused it. Fix: None for B2 and C7. The missing bytes are gone, so ask for the file again.
A wrong startxref number (3) A6 changes the line ends to CRLF, so every offset moves, and qpdf and pdfium refused it. B6 adds 50 and B7 sets 0, and only qpdf refused them. Fix: Our script lets qpdf rebuild the table. All three came back readable by all six.
Extra bytes at the end (2) B4 adds 512 zero bytes and B5 adds a text line. All six readers read both. Fix: None needed for these readers. Our script warns, and the repair trims the bytes.
A wrong stream length or table line (2) C3 sets a stream length wrong, and qpdf refused it. C4 changes one table line, pdfium refused it and pdfminer returned another page count. Fix: The rebuild fixed both, and both came back readable by all six.
Damaged page data (4) C1 flips a bit in a spacing number, and C6 swaps two bytes in the table. Both looked the same as the original. C2 zeroes 64 bytes in a page stream and lost text. C5 cuts 100 bytes inside one, and qpdf and pypdf refused it. Fix: None for C2 and C5. Ask for the file again.

When the file is not damaged

Four things look like a damaged PDF file and are not. A file can ask for a password. A reader can be strict. A program can be too old. A file can differ from its ending.

A password Only 3 of our 14 encrypted files opened in all six readers, and we gave no password. Ask the sender, and do not guess.
A strict reader qpdf alone refused 26 files that the other five read. A refusal from one reader does not prove the file is bad.
An old program We tested no editor or viewer, so we cannot name which versions fail. Try the file in a second program.
Another kind of file A file named .pdf can contain other data. Look at the first bytes and use the program that fits.

Repairs we tested

A script of 18 lines covers the fixes that need our own code. It takes the damaged PDF file and a name for the new file. It cuts junk in front of the header, adds a header when none exists and trims bytes after the last end marker. Then qpdf rebuilds the table.

pdf_fix.py, 18 lines
import sys, re, subprocess, os
def fix(b):
    i = b.find(b'%PDF-')
    if i > 0: b = b[i:]
    elif i < 0:
        m = re.search(rb'\d+\s+\d+\s+obj', b)
        if not m: return None
        b = b'%PDF-1.4\n' + b[m.start():]
    e = b.rfind(b'%%EOF')
    return b[:e + 5] + b'\n' if e >= 0 else b.rstrip(b'\x00\r\n ') + b'\n%%EOF\n'
if __name__ == '__main__':
    src, dst = sys.argv[1], sys.argv[2]
    b = fix(open(src, 'rb').read())
    if b is None: sys.exit('no objects found')
    open(dst + '.tmp', 'wb').write(b)
    r = subprocess.call(['qpdf', dst + '.tmp', dst], stderr=subprocess.DEVNULL)
    os.remove(dst + '.tmp')
    sys.exit(0 if r in (0, 3) and os.path.exists(dst) else 'qpdf could not rebuild the file')
Commands for five kinds of damage
# 1  header (A1), junk in front (A3), no end marker (B3), startxref (B6), table line (C4)
python3 pdf_fix.py A1.pdf A1_fixed.pdf
python3 pdf_fix.py A3.pdf A3_fixed.pdf
python3 pdf_fix.py B3.pdf B3_fixed.pdf
python3 pdf_fix.py B6.pdf B6_fixed.pdf
python3 pdf_fix.py C4.pdf C4_fixed.pdf
# compare the text with the original
for f in example.pdf *_fixed.pdf; do echo -n "$f  "; pdftotext $f - | md5sum | cut -c1-12; done
example.pdf  49a7710c3029
A1_fixed.pdf  49a7710c3029
A3_fixed.pdf  49a7710c3029
B3_fixed.pdf  49a7710c3029
B6_fixed.pdf  49a7710c3029
C4_fixed.pdf  49a7710c3029

The last lines print a short hash of the text. The hash matches for the original and the five repaired files, so every repaired file gave the original text. The other 4 copies in the list of 9 came back the same way.

The script needs qpdf to rebuild the file, and a rebuilt file is not byte for byte the original. We tested it on one small file with classic layout. We did not test it on compact, linearized or encrypted files, so do not trust it there.

Fix PDF files that will not open, step by step

We tested the steps on Linux with pdfinfo 24.02.0, qpdf 11.9.0, pypdf 5.9.0, pikepdf 10.13.0, pdfminer.six and pypdfium2 5.13.0. We opened no file in a browser, and the steps keep the original untouched at every point.

  1. Copy your PDF file, write its SHA-256 hash and work on the copy.
  2. Name the start with xxd -l 32 and the kinds of PDF file. A PDF file starts with %PDF- and a version.
  3. Use the check script on this page. A flag or warning names the problem.
  4. Try a second reader. pdfinfo and pikepdf opened the most files, so use both.
  5. Match the answer to a cause in the table above and try its fix on the copy.
  6. If one fix fails, do not stack another on top. Go back to the copy and try the next cause.
  7. Open the result in two readers, then look at the pages and the text.

What to send a helper

Short messages get faster answers. Include the details below and add the script output. Its first word gives OK, WARN or FLAG.

A message that gets an answer
Subject: My PDF file will not open
File: name, size in bytes and SHA-256 hash
Start: the output of xxd -l 32
Check: the output of pdf_peek.py and qpdf --check
Program: name, version and the exact words of the error
Source: where the file came from, and how it was sent

What no step can fix

Cut bytes B2 and C7 lost bytes, and no rule shows what the bytes said.
Changed page data C2 and C5 changed or lost bytes inside a stream. Pages came back for C2, and part of the text did not. Only another copy of the file shows the text.
Readers that disagree If two readers disagree, trust neither. Try a third, then ask for a new file.
A different page count pdfminer returned another count for C4 and for two corpus files. Check the count in a second reader.
Wrong endings A file named .pdf can contain any data. Name the kind first.
A quiet change All six readers found 2 pages in C2 with no error. Look at the text too.

Do not try online repair sites. Sites that offer to fix any document receive your upload. Try the steps above on a copy first.

How Do You Convert a PDF File to PNG or Make One From a Picture? Tested routes both ways, and what changed

We converted a 128 by 128 photo of 30,605 bytes to PDF three ways. img2pdf gave 31,813 bytes and kept every pixel. Pillow gave 5,579 bytes and changed pixels. We also drew a page to PNG, pulled out its text and rebuilt, split and joined PDF files with qpdf and poppler tools.

People convert a PDF file to use its text or pages somewhere else, or they convert a picture to PDF to send it. The commands below do it from a terminal, and every number below is actual. We tested no web converter and no editor.

Convert PNG or JPG to PDF

img2pdf hopper.png -o hopper.pdf wrote 31,813 bytes. The PNG is 30,605 bytes. pdfimages -png pulled the picture back out, and every pixel matched. ImageMagick with convert hopper.png hopper.pdf wrote 67,615 bytes, and every pixel matched too.

Three routes from one PNG to PDF
img2pdf hopper.png -o hopper_img2pdf.pdf
convert hopper.png hopper_magick.pdf
python3 -c "from PIL import Image; Image.open('hopper.png').convert('RGB').save('hopper_pillow.pdf')"
hopper.png            30605 bytes
hopper_img2pdf.pdf    31813 bytes   pixels match
hopper_magick.pdf     67615 bytes   pixels match
hopper_pillow.pdf      5579 bytes   pixels differ, largest gap 53 of 255

Pillow saved the picture with JPEG compression, and pdfimages -list says jpeg. The file is small, and the pixels changed. The largest gap from the original was 53 on a scale of 255. Check the picture before you delete the PNG.

img2pdf hopper_q80.jpg -o hopper_jpg.pdf wrote 7,262 bytes. The JPG is 5,824 bytes, and the PDF stores those same bytes, so it adds about 1,438 bytes of wrapper. The page sizes differ, 128 points for Pillow and 96 for img2pdf.

Convert PDF to PNG or text

pdftoppm -png -r 150 -f 1 -l 1 example.pdf pg150 drew page 1 as a 1275 by 1650 PNG of 25,384 bytes. At 72 dots per inch the PNG was 10,216 bytes. A JPG at quality 80 and 150 dots per inch was 45,468 bytes, so the JPG was larger for a page of text.

pdftotext -f 1 -l 1 example.pdf p1.txt wrote 79 bytes of text. The text matched the two lines we typed. A PDF made from a picture has no text to pull out. pdftotext hopper_img2pdf.pdf - printed nothing.

Rebuild, shrink, split and join

qpdf example.pdf rebuilt.pdf wrote 1,683 bytes from 1,847. qpdf --object-streams=generate example.pdf compact.pdf wrote 1,235 bytes and raised the version to 1.5. qpdf --linearize wrote 2,207 bytes, which is larger for a file this small.

Split and join one PDF file
qpdf --empty --pages example.pdf 2 -- page2.pdf
pdfseparate example.pdf sep-%d.pdf
pdfunite sep-1.pdf sep-2.pdf united.pdf
example.pdf  1847   page2.pdf  915   sep-1.pdf  1340   sep-2.pdf  1279   united.pdf  1950

qpdf --empty --pages example.pdf 2 -- page2.pdf pulled out page 2 as a 915 byte file. pdfseparate wrote one file for every page, and pdfunite joined them again into 2 pages. pypdf read the text of page 2 from the joined file.

Before you convert

We did not test forms, signatures, tagged pages or PDF/A rules, so we do not claim that a conversion keeps them. Check the result against the original. Use a new name for the output so the source file stays safe.

New name first Output files can overwrite files with the same names. Use a new name for every output.
Look before you convert pdfinfo and our script print the facts and write nothing.
Compare the pages The same page count in two readers is a good sign. A different count means look closer.
Keep the original A conversion can lose text, forms or pixels, so keep the source file.

Keep the original file. A converted copy can lose pixels, text or form fields. Keep the original for later edits.

How Does a PDF File Differ From PNG and JPG Files? One page, four formats, measured

A PDF file stores pages, and PNG and JPG files store one picture. Page 1 of our example was 948 bytes as a PDF file, 25,384 as a PNG at 150 dots per inch and 79 as plain text. A PDF wrapper added 1,208 bytes to our PNG and 1,438 to our JPG.

The table lists the neighbors of a PDF file and what we measured. We made the files from one small document and one photo. Sizes depend on the tool and the settings, so they show one case and not a rule.

PDF, PNG, JPG and text compared
Type What separates it
PDF Pages with text, fonts and pictures. Page 1 of our example was 948 bytes, and the text stays text.
PNG Lossless picture. Our page drawn at 150 dots per inch was 25,384 bytes, and the text became pixels. Read the PNG page below for the format.
JPG Lossy picture. Our page at quality 80 and 150 dots per inch was 45,468 bytes. Read the JPG page below for the format.
Text Letters only. The text of page 1 was 79 bytes, and it keeps no fonts or layout.

PDF or PNG

A PNG stores pixels. A PDF stores the commands that draw the page. Our page was 948 bytes as a PDF file and 25,384 as a PNG. For a photo it goes the other way, since a PDF wrapper added 1,208 bytes to our 30,605 byte PNG.

PDF or JPG

A JPG is lossy and stores one picture. For our photo at quality 80, the JPG was 5,824 bytes and the PDF that wraps it was 7,262. For a page of text the JPG was 45,468 bytes, which is more than the PNG. Do not send text pages as JPG files.

PDF or plain text

A text file is the smallest, at 79 bytes for page 1. It has no fonts, no page size and no pictures. A PDF file made from a picture has no text at all, so pdftotext printed nothing for ours.

Which one do you have

PNG files Read the PNG file page for the lossless format.
JPG files Read the JPG file page for the lossy photo format.
ZIP files Read the ZIP file page when a PDF file arrives inside an archive.
Other types Browse the resource library for more formats.

What Is the PDF File Format Made Of? The facts we could check, and where they came from

A PDF file stores a header, objects, a cross reference table and a trailer. Facts below come from our own checks of 300 files and 21 damaged copies. Sources are Wikipedia, IANA, RFC 8118 and the six test tools.

Facts about PDF files
Part What we could check
Maker Adobe developed PDF from 1991 to 2008 and released it on 15 June 1993. ISO has maintained it since 2008, per Wikipedia.
Name Portable document, with the ending .pdf. IANA lists application/pdf, and the file command printed the same type for our example.
First bytes %PDF- and a version. 93 of our 300 files say 1.3, 57 say 1.4, 31 say 1.5, 40 say 1.6, 66 say 1.7 and 3 say 2.0.
Layout A header, a body of objects, a cross reference table, a trailer and a footer with startxref and %%EOF, per Wikipedia.
Table Version 1.5 added an optional cross reference stream, and some objects can live in object streams, per Wikipedia. 55 of our 300 files have a cross reference stream.
Content Text is stored as content streams, and fonts can be stored inside the file or left out, per Wikipedia.
Security The format supports encryption and digital signatures. PDF 2.0 defines 256 bit AES, per Wikipedia. 14 of our 300 files are encrypted.
Standards ISO 32000-1 appeared in 2008 and matches PDF 1.7. PDF 2.0 is ISO 32000-2. Subsets are PDF/A, PDF/E, PDF/UA, PDF/VT and PDF/X, per Wikipedia.
Linearized Wikipedia calls linearized files optimized or web optimized. 32 of our 300 files are linearized.
Our tests 300 files, 21 damaged copies, 6 readers, 1 repair route and a script of 26 lines. pdfinfo 24.02.0, qpdf 11.9.0, pypdf 5.9.0, pikepdf 10.13.0, pdfminer.six and pypdfium2 5.13.0 on Linux.

About the values we list

Facts about the layout come from our own reading of one example file and 300 test files, and from the pages we name. RFC 8118 from the IETF registered the type, and IANA lists application/pdf with ISO 32000-2 as the most recent specification.

RFC 8118 came out in March 2017 and obsoleted RFC 3778. The IANA record now cites a newer draft, so we use the RFC for its safety notes and the IANA page for the type. We found no registration date, so we give none.

What we left out

We opened no file in a browser or an editor. We used no web converter and scanned no file for malware. Our readers and scripts worked on Linux, and Windows and macOS programs can accept or refuse files that they treat differently.

All 300 files come from 2 public projects, and many are built to test readers. Our damaged copies come from one small file and change one thing at a time. We did not read ISO 32000, and we did not read the safety section of any reader.

Sources checked

For the format, see the Wikipedia article on PDF, the IANA record for application/pdf and the full RFC 8118.

For the tools, see the qpdf project, pypdf, pikepdf, pdfminer.six and pypdfium2. The test files come from pdf.js and qpdf. Every count on this page came from files and scripts in our research folder.

Frequently Asked Questions About PDF Files

What is a PDF file?

A PDF file is a document file that stores pages with text, fonts, pictures and drawings. Adobe released the format on 15 June 1993, and ISO has maintained it since 2008, per Wikipedia.

How do I open a PDF file?

Drag it into a viewer you trust. We also tested pdfinfo, qpdf, pypdf, pikepdf, pdfminer and pdfium. pdfinfo opened 285 of our 300 files, and qpdf opened 249.

Why will my PDF file not open?

The file can be damaged, encrypted or read by a strict reader. qpdf alone refused 26 files that five other readers read. Try a second reader before you give up.

What is the difference between a PDF and a PNG?

A PNG stores pixels, and a PDF stores the commands that draw a page. Our page was 948 bytes as a PDF file and 25,384 as a PNG. A photo goes the other way, because a PDF wrapper added bytes to it.

Is a PDF file safe to open?

Looking at the bytes starts nothing. The risk lives in what a viewer starts. RFC 8118 names scripts, attached files and links. 6 of our 281 openable files have JavaScript. Keep your viewer current.

How do I convert a PDF file to PNG?

Use pdftoppm. pdftoppm -png -r 150 -f 1 -l 1 example.pdf pg150 drew page 1 as a 25,384 byte PNG. A higher dot count gives a larger picture, and the text becomes pixels.

How do I convert a PNG to PDF?

Use img2pdf to keep every pixel. It wrote 31,813 bytes from our 30,605 byte PNG. Pillow wrote 5,579 bytes, because it used JPEG compression, and the pixels changed.

How can I check if a PDF file is damaged?

Look at the first bytes, use our script and compare two readers. The script flagged or warned on 15 of 21 damaged copies, and it printed OK for 3 that still broke something. Look at the pages too.

Can a damaged PDF file be repaired?

Some damage can be repaired. Our script restored 9 of 14 copies that needed it, using qpdf to rebuild the table. Cut bytes and zeroed page data did not come back. Keep the original and work on a copy.

Do all readers agree on a damaged PDF file?

No. Our six readers gave one answer on 9 of 21 damaged copies and split on 12. All six found 2 pages in a copy whose page data was zeroed. Compare two readers.

How do I split or join PDF files?

Use qpdf or poppler. qpdf --empty --pages example.pdf 2 -- page2.pdf pulled out page 2 as a 915 byte file. pdfunite sep-1.pdf sep-2.pdf united.pdf joined two pages back into one.

What MIME type is a PDF file?

IANA lists application/pdf with the ending .pdf, and the file command printed the same type for our example. The IANA record names ISO 32000-2, which is PDF 2.0, as the most recent specification.

Still Stuck With a PDF File? Where to go next, depending on what went wrong

Your PDF file will not open

Look at the first bytes and use the check script. The cause table lists what to try in order.

You do not know the kind

The kinds table names the kind, and the neighbors name the formats that look alike.

Is it a PNG file? The guide to PNG files covers the lossless image format.

Is it a JPG file? The guide to JPG files covers the lossy photo format.

Did it arrive in an archive? The guide to ZIP files covers how to look inside one.

Is it a file type you do not know? Browse the file extension resource library, or send it on the submit a file type page.

Do you need the format facts? Read the format part for the layout, the standards and the sources of PDF files.