> For AI agents: the complete documentation index is available at https://anyformat.swc.rs/llms.txt, the full documentation bundle is available at https://anyformat.swc.rs/llms-full.txt.

# PDF

The PDF family reads the header of a PDF file. It finds the header and its
version. The MIME type of PDF files is `application/pdf`.

| Language | Package              | Module               |
| -------- | -------------------- | -------------------- |
| Node.js  | `@swc/anyformat-pdf` | `@swc/anyformat-pdf` |
| Python   | `anyformat-pdf`      | `anyformat_pdf`      |

The PDF specification lets the `%PDF-M.m` header start anywhere in the first
1024 bytes of a file. Both functions search that range.

## `isPdf`

Returns `true` when `data` starts with a PDF file header.

### Node.js

```ts
function isPdf(data: Uint8Array): boolean
```

```js
import { readFile } from 'node:fs/promises'
import { isPdf } from '@swc/anyformat-pdf'

isPdf(await readFile('paper.pdf'))
// => true
```

### Python

```python
def is_pdf(data: bytes) -> bool
```

```python
from pathlib import Path

import anyformat_pdf

anyformat_pdf.is_pdf(Path("paper.pdf").read_bytes())
# => True
```

## `detectVersion`

Reads the version, for example `"1.7"`, from the PDF header of `data`. Returns
`null` in Node.js and `None` in Python when the first 1024 bytes of `data` do
not contain a PDF header.

### Node.js

```ts
function detectVersion(data: Uint8Array): string | null
```

```js
import { readFile } from 'node:fs/promises'
import { detectVersion } from '@swc/anyformat-pdf'

detectVersion(await readFile('paper.pdf'))
// => '1.7'
```

### Python

```python
def detect_version(data: bytes) -> str | None
```

```python
from pathlib import Path

import anyformat_pdf

anyformat_pdf.detect_version(Path("paper.pdf").read_bytes())
# => '1.7'
```
