| from __future__ import annotations |
| from pathlib import Path |
| import fitz |
| from pypdf import PdfReader, PdfWriter |
|
|
| def open_pdf(path: Path) -> fitz.Document: |
| return fitz.open(path) |
|
|
| def save_pdf(doc: fitz.Document, out_path: Path) -> Path: |
| out_path.parent.mkdir(parents=True, exist_ok=True) |
| doc.save(out_path, garbage=4, deflate=True, clean=True) |
| return out_path |
|
|
| def rebuild_pdf(input_path: Path, output_path: Path) -> Path: |
| reader = PdfReader(str(input_path)) |
| writer = PdfWriter() |
| for page in reader.pages: |
| writer.add_page(page) |
| output_path.parent.mkdir(parents=True, exist_ok=True) |
| with output_path.open("wb") as f: |
| writer.write(f) |
| return output_path |
|
|