Skip to content

Instantly share code, notes, and snippets.

@aont
Last active June 29, 2025 15:55
Show Gist options
  • Save aont/bb8864bcc4f696e015086e6ca8b375fb to your computer and use it in GitHub Desktop.
Save aont/bb8864bcc4f696e015086e6ca8b375fb to your computer and use it in GitHub Desktop.

iOS tall screenshot PDF to multi-page PDF

This Python 3 script, built with the pypdf library, converts a single extremely tall PDF—such as one created by an iOS browser’s full-page screenshot feature—into a multi-page document:

  • Aspect-ratio slicing It cuts the original page into consecutive slices that keep the same aspect ratio as A4 paper (≈ 1 : √2), regardless of the actual width.
  • Overlap control Each new page starts slightly higher than the previous slice end, leaving a configurable overlap (e.g., 10 mm) so content isn’t split awkwardly.
  • Fully offline Everything happens locally; no external services or GUI frameworks are required.
  • Customisable You can change the overlap size, shrink or crop the width if it exceeds A4, and wrap the logic in a CLI or GUI if needed.
from pathlib import Path
from pypdf import PdfReader, PdfWriter, PageObject
import sys
# ========= ユーザ設定 =========================
SRC_PDF = Path(sys.argv[1])
DST_PDF = Path(sys.argv[2])
OVERLAP_MM = 10.0 # 各ページの重なり (mm) - 必要なければ 0
DPI = 72 # PDF pt ⇔ inch 変換用
# ============================================
# ---------------- 補助 -----------------------
def mm2pt(mm: float) -> float:
return mm * DPI / 25.4
# --------------------------------------------
reader = PdfReader(SRC_PDF)
if len(reader.pages) != 1:
raise ValueError("1 ページ PDF であることを想定しています")
orig = reader.pages[0]
W = orig.mediabox.width # pt
H = orig.mediabox.height # pt
ratio_a4 = 842 / 595 # ≒ 1.414213562
H_page = W * ratio_a4
overlap = mm2pt(OVERLAP_MM)
# -------- 実幅が A4 幅 を超える場合の選択肢 ----------
# 1) 幅をそのまま使う(以下の実装)
# 2) 幅を 595 pt に縮小し左右にマージン
# 3) 幅を 595 pt にトリミング
# ------------------------------------------------------
writer = PdfWriter()
y_top = H
step = H_page - overlap # 前進量
while True:
lower = max(y_top - H_page, 0)
upper = y_top
# CropBox 指定範囲で複製
page = PageObject.create_blank_page(width=W, height=H_page)
page.merge_page(orig) # 内容をコピー
# 元 PDF 座標系は左下原点
page.cropbox.lower_left = (0, lower)
page.cropbox.upper_right = (W, upper)
page.mediabox = page.cropbox # ビューア互換用
writer.add_page(page)
if lower == 0:
break # 最終ページ
y_top -= step
with DST_PDF.open("wb") as f:
writer.write(f)
print(f"{len(writer.pages)} ページ生成 → {DST_PDF}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment