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, and this page is available as Markdown at https://anyformat.swc.rs/api/pdf.md.

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.

LanguagePackageModule
Node.js@swc/anyformat-pdf@swc/anyformat-pdf
Pythonanyformat-pdfanyformat_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

function isPdf(data: Uint8Array): boolean
import { readFile } from 'node:fs/promises'
import { isPdf } from '@swc/anyformat-pdf'

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

Python

def is_pdf(data: bytes) -> bool
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

function detectVersion(data: Uint8Array): string | null
import { readFile } from 'node:fs/promises'
import { detectVersion } from '@swc/anyformat-pdf'

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

Python

def detect_version(data: bytes) -> str | None
from pathlib import Path

import anyformat_pdf

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