---
name: pdf
description: Read, create, inspect, merge, split, rotate, encrypt, fill, and validate PDF files. Use when the user asks to work with a PDF or convert supported Markdown into a polished PDF in AstrBot.
title: pdf
canonical_url: https://skilld.dev/gh/astrbotdevs/astrbot/pdf
last_updated: 2026-10-29T11:16:36.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [references/forms.md](https://skilld.dev/api/skills-raw/astrbotdevs/astrbot/pdf/references/forms.md), [scripts/markdown_to_pdf.py](https://skilld.dev/api/skills-raw/astrbotdevs/astrbot/pdf/scripts/markdown_to_pdf.py).
>
> If the user asked to install this Skill, run `npx skilld install astrbotdevs/astrbot/pdf`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/pdf`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/astrbotdevs/astrbot/pdf) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/pdf`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/pdf --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/pdf --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Work with PDFs

Use `pypdf` for PDF structure operations and validation. Use ReportLab for new page layout. Do not install another PDF library unless the requested feature cannot be implemented safely with the bundled dependencies.

## Follow the workflow

1. Preserve source files unless the user explicitly requests an in-place change.
2. Inspect the input with `pypdf.PdfReader`: page count, metadata, encryption, page sizes, text, annotations, and form fields as relevant.
3. Perform the smallest operation that satisfies the request.
4. Reopen every output with `pypdf` and verify its page count, expected text or fields, and encryption state.
5. Render representative pages when a PDF renderer is available. Inspect the rendered images for clipping, overlap, missing glyphs, broken spacing, and blank pages. If rendering is unavailable, state that only structural validation was completed.

Work in the current workspace unless the user provides another writable location. Keep intermediate files separate from final outputs and remove them when they are no longer needed.

## Read and inspect

Use `astrbot_file_read_tool` for a quick text extraction when it is sufficient. Use Python with `pypdf` when page structure or exact metadata matters:

```python
from pypdf import PdfReader

reader = PdfReader(input_path)
print(len(reader.pages), reader.metadata)
for page_number, page in enumerate(reader.pages, start=1):
    print(page_number, page.mediabox, page.rotation)
    print(page.extract_text() or "")
```

Text extraction does not prove that visual layout is correct. Render with `pdftoppm` when it is available:

```text
pdftoppm -png -r 144 input.pdf rendered/page
```

## Create from Markdown

Prefer the bundled deterministic converter:

```text
python <this-skill-directory>/scripts/markdown_to_pdf.py input.md output.pdf
```

The converter intentionally supports a limited Markdown subset:

- ATX headings (`#` through `######`)
- paragraphs
- flat ordered and unordered lists
- block quotes
- fenced code blocks
- horizontal rules
- `<!-- pagebreak -->`
- bold, italic, inline code, and HTTP(S) or mail links

It does not interpret raw HTML, images, tables, nested lists, footnotes, or arbitrary Markdown extensions. Simplify unsupported content or explain the limitation instead of silently changing meaning.

Font selection follows this order:

1. Use an explicit readable font passed with `--font path/to/font.ttf` or `--font path/to/font.ttc`.
2. Otherwise, search common system locations for a readable Chinese font and verify that it contains every character used by the document.
3. If no suitable system font is available, use ReportLab's built-in Simplified Chinese fallback for supported Latin and Chinese text. The converter stops instead of silently emitting missing glyphs when the fallback cannot represent the input.

When a broader or embedded font is required and no suitable system font is readable, download it into the workspace and pass its path with `--font`. Prefer an official source over an arbitrary font mirror or CDN:

- [MiSans from Xiaomi](https://hyperos.mi.com/font/zh/download/) is an official source that is generally accessible from mainland China. Review its license before use and do not redistribute the font package unless the license permits it.
- [Noto Sans CJK](https://github.com/notofonts/noto-cjk/blob/main/Sans/README.md) is an official open-source fallback with documented download options.

If an official GitHub font URL times out for a user in mainland China, the user may choose one of these third-party proxy prefixes:

- `https://edgeone.gh-proxy.com/`
- `https://hk.gh-proxy.com/`
- `https://gh-proxy.com/`
- `https://gh.dpik.top/`

Append the complete official GitHub URL directly after the prefix. For example:

```text
Official: https://github.com/googlefonts/noto-cjk/raw/main/Sans/Variable/TTF/Subset/NotoSansSC-VF.ttf
Proxy:   https://edgeone.gh-proxy.com/https://github.com/googlefonts/noto-cjk/raw/main/Sans/Variable/TTF/Subset/NotoSansSC-VF.ttf
```

These proxies are not operated by the font project or AstrBot. Their availability and returned content can change. Never send credentials, private repository URLs, or other sensitive data through them. After downloading, verify that the final response used HTTPS, inspect the file type, and compare a checksum with an official checksum when one is published.

Do not download a font silently. Obtain user approval when network access or a new file is required, use HTTPS, and keep the downloaded font in a workspace or temporary directory rather than installing it system-wide.

After conversion, use `pypdf` to confirm the output opens, contains pages, and exposes representative expected text. Render and inspect the first page plus any page with dense content, code, or a page break.

## Transform existing PDFs

Use small `pypdf` scripts directly for ordinary operations.

Merge documents in the requested order:

```python
from pypdf import PdfWriter

writer = PdfWriter()
for path in input_paths:
    writer.append(path)
writer.write(output_path)
```

Split selected pages without modifying the source:

```python
from pypdf import PdfReader, PdfWriter

reader = PdfReader(input_path)
writer = PdfWriter()
for page_index in selected_zero_based_indexes:
    writer.add_page(reader.pages[page_index])
writer.write(output_path)
```

Rotate by a multiple of 90 degrees with `page.rotate(angle)`. Encrypt with `writer.encrypt(password)` only when the user asks, and never echo a password into logs or the final response. Reopen encrypted outputs with the password before reporting success.

## Fill forms

Read [forms.md](https://skilld.dev/api/skills-raw/astrbotdevs/astrbot/pdf/references/forms.md) before modifying AcroForms. Preserve interactivity unless the user requests a flattened result. Validate both the canonical field tree and page widget annotations; a successful render alone is not proof that field values were saved.

## Finish

Report the final path, the operation performed, and the validation completed. Mention any unsupported content, unavailable renderer, password requirement, or form ambiguity. Do not claim visual verification unless rendered pages were actually inspected.
