# pdforensic

> pdforensic is an open-source command-line tool and Python library for PDF forensics. Given a PDF it reports whether the file was changed after it was created, what each later save changed, recovers earlier versions byte for byte, extracts all metadata (Info dictionary, XMP, hashes, signatures, fonts, images), and infers how the file was produced (office export, generated by software, browser print, scan, OCR, PDF editor). It is built on qpdf and runs on Linux, macOS and Windows.

- PyPI package: `pdforensic-authentic-check`. Import name and main command: `pdforensic`. Requires Python 3.11+.
- Install: `uv tool install pdforensic-authentic-check` (commands) or `uv add pdforensic-authentic-check` (library).
- License: MIT (bundled pdfresurrect source: BSD-3-Clause). Repository: https://github.com/genie360s/pdforensic

## Command line

- `pdforensic analyze FILE.pdf` prints a readable report. Add `--json` for the full report as JSON, `--password PW` for an encrypted file.
- `pdforensic recover FILE.pdf [-o DIR] [--all]` writes each earlier revision to `DIR` (default `<name>-revisions/`) as `<name>-rev<N>.pdf`.
- `pdforensic --version` prints the version.
- Exit code 0: report produced. Exit code 2: file missing or not a PDF. The exit code does not reflect the findings; read `tamper.level`.
- Legacy pdfresurrect wrappers: `pdf-versions`, `pdf-meta`, `pdf-recover`, `pdf-eof`.

## Python API

```python
from pdforensic import analyze, recover_revisions, format_report, PdfError

report = analyze("file.pdf", password="")   # path or bytes -> JSON-serializable dict; raises PdfError if not a PDF
text = format_report(report)                # the readable report as a string
items = recover_revisions("file.pdf", out_dir=None, include_current=False, password="")
# -> [{"revision": 1, "path": "...", "size": 1385, "sha256": "..."}]
```

## Report structure

Top-level keys of the dict returned by `analyze()`:

- `file`: `path`, `size`, `md5`, `sha1`, `sha256`
- `engine`: versions of pdforensic, pikepdf and qpdf
- `structure`: `version`, `header_version`, `catalog_version`, `revision_count`, `eof_markers`, `objects`, `linearized`, `hybrid_reference`, `trailing_bytes`, `nonstandard_filters`
- `encryption`: `encrypted`, and when encrypted `decrypted`, `password`, `bits`, methods
- `identifiers`: the trailer `/ID` pair and whether its two parts are equal
- `info`, `info_dates`: the Info dictionary and its dates in ISO 8601
- `xmp`, `xmp_error`, `xmp_history`, `preflight_ticket`: XMP properties (or why they could not be read), the xmpMM:History events, the GWG preflight ticket
- `conformance_claims`: declared PDF/A, PDF/X, PDF/UA, PDF/E, PDF/VT (not validated)
- `revisions`: per revision `revision`, `offset`, `end`, `sha256`, `info`, `objects` (`added`/`modified`/`deleted` counts), `changes` (list of `object`, `change`, `type`), `signing`
- `signatures`: `signer_name`, `date_iso`, `subfilter`, `byte_range`, `signed_revision`, `covers_whole_file`, `live`
- `pages`: `count`, `kinds`, `details`; `fonts`; `images`; `features` (forms, JavaScript, embedded files, link and launch actions, tagging)
- `provenance`: `tools`, `origin`, `generation`, `content_profile`, `evidence`
- `tamper`: `level` (`none` | `info` | `low` | `medium` | `high`), `modified_after_creation` (`true` or `null`), `indicators` (each `id`, `severity`, `title`, `detail`, `basis`), `note`
- `warnings`

Indicator ids: `incremental_updates`, `tool_changed_between_revisions`, `deleted_objects`, `unreferenced_objects`, `id_mismatch`, `permanent_id_changed`, `linearization_stale`, `dates_differ`, `moddate_before_creation`, `future_date`, `info_xmp_mismatch`, `info_newer_than_xmp`, `xmp_history`, `editing_tool_named`, `content_after_signature`, `signature_removed`, `signature_byte_range_unusual`, `preflight_ticket`, `preflight_ticket_unsealed`, `structural_damage`, `xref_unreadable`, `xref_offset_mismatch`, `syntax_warnings`, `trailing_data`, `unknown_pdf_version`, `version_downgraded`, `nonstandard_filter`.

`provenance.generation` values: `exported_from_application`, `programmatic`, `browser_print`, `print_to_pdf`, `image_conversion`, `scan`, `scan_or_image_conversion`, `post_processed`, `unknown`.

## Limits to state when using results

- Indicators are evidence, not proof. A file re-saved in full ("Save As", "Print to PDF") carries no history, so a clean report does not prove a file is original.
- Signature cryptography is not verified; the report states what each signature's byte range covers.
- Conformance claims are reported as declared, not validated.
- Metadata is editable; Producer and Creator are what the file claims.
- The number of `%%EOF` markers does not show editing: linearized files and Microsoft Office exports have two. Use `structure.revision_count`.

## Docs

- [User guide](https://raw.githubusercontent.com/genie360s/pdforensic/main/docs/USER_GUIDE.md): installation, every command, how to read each result and each finding, troubleshooting, glossary
- [README](https://raw.githubusercontent.com/genie360s/pdforensic/main/README.md): overview, examples, development and release process
- [PDF forensics notes](https://raw.githubusercontent.com/genie360s/pdforensic/main/docs/PDF_FORENSICS.md): the rule and standard clause behind each indicator, sources, performance, what was verified

## Optional

- [Changelog](https://raw.githubusercontent.com/genie360s/pdforensic/main/CHANGELOG.md): changes per version
- [Vendored pdfresurrect](https://raw.githubusercontent.com/genie360s/pdforensic/main/src/pdfresurrect/UPSTREAM.md): version, patches and known limits of the bundled C tool
