# Repair a damaged PDF

Fix a PDF that other apps refuse to open or open with errors, for example after an interrupted download, by rewriting it with `sumatrapdf-tool clean`.

**Available in [pre-release 3.7](https://www.sumatrapdfreader.org/prerelease)**

**Run `sumatrapdf-tool clean damaged.pdf repaired.pdf`.** At a glance:

- **Repair:** `clean` rebuilds a broken cross-reference table (the index of objects in the file) and writes a new, well-formed PDF.
- **Check:** run `pages`, `info` or `draw -F txt` on the result.
- **Save what's left:** keep only the pages that survived with a page list.

Use `sumatrapdf-tool.exe`, or [SumatraPDF.exe clean](Tools.md) with the same arguments.

## Repair a PDF

```
sumatrapdf-tool clean damaged.pdf repaired.pdf
```

For a damaged file it prints messages like these:

```
format error: cannot find startxref
warning: trying to repair broken xref
warning: repairing PDF document
```

They mean the file's index was missing or wrong, so it was rebuilt by scanning the whole file. The command still succeeds (exit code `0`) and writes `repaired.pdf`. For an undamaged file `clean` prints nothing.

- Without an output file name, the result is written to `out.pdf`.
- For a password-protected PDF, add `-p <password>`.

Note: SumatraPDF uses the same repair code when it opens a PDF, so a damaged file may open in SumatraPDF but not in other apps. The repaired file opens in other apps too.

## Check the result

List the pages and their sizes:

```
sumatrapdf-tool pages repaired.pdf
```

Print the text of every page:

```
sumatrapdf-tool draw -F txt repaired.pdf
```

If these commands print no `error` or `warning` lines, the repair worked. `draw` prints a `page repaired.pdf 1` line for each page; those are progress messages, not errors.

## Save the pages that survived

If part of the file is gone, `clean` still writes a file, but pages whose data was lost come out blank, and tools warn when they read it:

```
format error: non-page object in page tree
warning: Page tree load failed. Falling back to slow lookup
```

`pages` shows a lost page with no size:

```
<page pagenum="3">
</page>
```

Keep only the good pages:

```
sumatrapdf-tool clean damaged.pdf good.pdf 1-2
```

`good.pdf` has pages 1 and 2 and opens without warnings. The page list works as in [Extract pages from PDF](Tool-x-extract-pages-from-pdf.md): `1-5,7,9-N`, where `N` is the last page.

## Garbage collection (`-g`)

`-g`, `-gg`, `-ggg` and `-gggg` remove unused objects and make the file smaller (see [sumatrapdf-tool clean](Tool-clean.md)).

Repair without `-g` first. In a test with a lost page, plain `clean` made a blank page 3, but `clean -g` made a file that failed on page 3 with `format error: cycle in page tree`. Once you have a good file, compress it:

```
sumatrapdf-tool clean -gggg -e 100 -f -i -t -Z good.pdf good-small.pdf
```

## What can't be fixed

- `clean` can't recover data that isn't in the file. Text, images and pages that were cut off are lost; get the file again if you can.
- Some damage stops the repair. `clean` then prints an error such as `syntax error: invalid key in dict`, exits with code `1` and writes no file.

## Tips

- Keep the damaged original. Write the repaired file under a new name.
- If a download was interrupted, download it again before you try to repair it.
- Use `sumatrapdf-tool info repaired.pdf` to see the page count, fonts and images of the result (see [See PDF info](Show-PDF-info.md)).
- To repair a whole folder, see [Process many PDFs at once](Batch-process-PDFs.md).

## See also

- [sumatrapdf-tool clean](Tool-clean.md) — all options
- [Compress a PDF](Tool-x-compress-pdf.md)
- [sumatrapdf-tool pages](Tool-pages.md) and [sumatrapdf-tool info](Tool-info.md) — check the result
- [All cmd-line tools](Tools.md)
