SuperAPI AI Directory

img2pdf

Office & productivity

View repository

Updated 10-15 20:10

About this skill

img2pdf

Positioning

This skill solves the problems of "a large number of screenshots/photos need to be arranged into a compact PDF for submission" and "very long screenshots (WeChat chats, court hearing transcripts) need to preserve their top-to-bottom logic when converted to PDF". The core scenario is organizing legal evidence materials.

Core responsibilities:

  1. Arrange an image directory or multiple image files into an A4 PDF, supporting 1/2/3/4 images per page.
  2. Re-arrange each page of an existing PDF into a compact layout with N per page.
  3. Automatically detect image orientation (landscape or portrait) and choose the appropriate A4 page orientation.
  4. Configurable page margins to ensure good printing results.
  5. v1.2.0 Long screenshot mode: automatically split very long images according to the A4 ratio before arranging (WeChat chat scenario), or render the entire long image as a single PDF with adaptive height (court hearing transcript scenario).

This skill does not perform OCR, does not edit PDF content, and does not process video files. If screenshots need to be extracted from videos, use video-screenshot first.

Coordination with other skills

  • Upstream: after extracting video screenshots with video-screenshot, use this skill to arrange them into a PDF.
  • Upstream: image files produced by screenshot tools (phone screenshots, browser screenshots).
  • Downstream: pdf-organizer can further organize the arranged PDF (split, merge, name).
  • Alternative: pdf-organizer's --normalize-a4 only standardizes pages and does not arrange multiple images.

Dependencies

System dependencies

No additional system dependencies.

Python packages

Package name Purpose Install command
pypdf>=4.0.0 PDF page transformation and merging python3 -m pip install -r scripts/requirements.txt
Pillow>=10.0.0 Image format detection Same as above
PyMuPDF>=1.24.0 Image to PDF page conversion Same as above

Input/Output

Input

  • Image directory: scan all JPG/PNG/WebP files in the directory.
  • Multiple image files: directly list the image paths.
  • Existing PDF: treat each page of the PDF as an image and re-arrange it.

Output

  • A single A4 PDF file, with each page containing 1-4 images, scaled proportionally and centered.

Workflow

1. Collect input

According to the --input parameter, collect images or PDF files. If it is a directory, scan all images in supported formats within it. Sort by file name or modification time.

2. Convert to pages

  • Image files: convert to single-page PDF via PyMuPDF.
  • PDF files: read each page as an independent page.

3. Calculate layout

According to --per-page and the page orientation, calculate the available area for each image:

  • per-page=1: full page minus margins, with orientation determined by the image orientation.
  • per-page=1: full page minus margins, with orientation determined by the image orientation.
  • per-page=2: A4 landscape, two columns on the left and right.
  • per-page=3: A4 landscape, three columns side by side.
  • per-page=4: A4 landscape or portrait, 2×2 grid.
  • per-page=auto(Default): many portrait images → 3 per page, many landscape images → 1 per page.

Each image is scaled proportionally to fit its available area and centered.

4. Generate PDF

Write the arranged pages to the output PDF. Do not modify any original files.

Run the script

Install dependencies on first use:

python3 -m pip install -r scripts/requirements.txt

Phone screenshots (automatic 3 per page)

python3 scripts/img_to_pdf.py \
  --input /path/to/screenshots/ \
  --output /path/to/output.pdf
# 自动检测:竖版图多 → 3张/页

Computer screenshots (automatic 1 per page)

python3 scripts/img_to_pdf.py \
  --input /path/to/desktop-screenshots/ \
  --output /path/to/output.pdf
# 自动检测:横版图多 → 1张/页(A4横版)

Phone screenshots 2 per page (A4 landscape, side by side)

python3 scripts/img_to_pdf.py \
  --input /path/to/screenshots/ \
  --output /path/to/output.pdf \
  --per-page 2

Video screenshots 3 per page

python3 scripts/img_to_pdf.py \
  --input /path/to/frames/ \
  --output /path/to/output.pdf \
  --per-page 3

Rearrange an existing PDF

python3 scripts/img_to_pdf.py \
  --input /path/to/original.pdf \
  --output /path/to/repacked.pdf \
  --per-page 2

Multiple image files

python3 scripts/img_to_pdf.py \
  --input img1.jpg img2.jpg img3.png \
  --output /path/to/output.pdf \
  --per-page 3

WeChat chat long screenshot (v1.2.0, automatically split by A4 ratio + 3 per page)

python3 scripts/img_to_pdf.py \
  --input /path/to/wechat_long.png \
  --output /path/to/wechat.pdf \
  --split \
  --per-page 3
# 1080×6000 → 按 1080×√2≈1527px 切 4 段 → 2 页 A4 横版

WeChat chat long screenshot (explicit split segment height)

python3 scripts/img_to_pdf.py \
  --input /path/to/wechat_long.png \
  --output /path/to/wechat.pdf \
  --split \
  --split-height 1500 \
  --per-page 3

Court transcript long screenshot (v1.2.0 vertical mode, whole image as one long page)

python3 scripts/img_to_pdf.py \
  --input /path/to/transcript.png \
  --output /path/to/transcript.pdf \
  --mode vertical
# 不切割,1080×5000 → 1 页 595×2573pt
# 页面高度按图等比缩放,保留上下逻辑

Preview (do not write to file)

python3 scripts/img_to_pdf.py \
  --input /path/to/dir/ \
  --per-page 2 \
  --dry-run

Common parameters

Parameter Description Default value
--input / -i Image file, PDF file, or directory (required) -
--output / -o Output PDF path <输入名>_编排.pdf
--mode Layout mode:nup(N per page) or vertical(single image as one long page) nup
--per-page / -n Number of images per page in nup mode:1/2/3/4, or omit for automatic auto(3 for portrait, 1 for landscape)
--margin / -m Page margin (pt) 25
--orientation Page orientation in nup mode:auto/landscape/portrait auto
--sort Sort:name/time/none name
--split Enable long screenshot splitting (nup mode) Off
--split-height Split segment height (px); omit = by A4 ratio (图宽 × √2); ignored in vertical mode A4 ratio
--dry-run Preview only, no output false

Comparison of the two modes

Dimension nup vertical
Whether to split Depends on --split depends No split (forced)
Images per page 1/2/3/4 Must be 1
Page size A4 fixed Width fixed at A4 595pt, height proportional to image
Applicable scenarios WeChat chats, video screenshots, evidence photos Court transcripts, single-page long screenshots

Delivery check

After completion, check:

  1. Output PDF page count = ceil(total images / per-page) (nup mode) or = number of images (vertical mode).
  2. Images on each page are clear and readable, with nothing exceeding the page boundaries.
  3. Page margins are reasonable, and content will not be cropped when printing.
  4. Orientation is correct (phone screenshots side by side in landscape, video screenshots in three columns, etc.).
  5. Original images and PDFs have not been modified or deleted.
  6. Long screenshot mode: split segment height matches --split-height or the A4 ratio default; vertical mode page height = image height × (A4 width - 2×margin) / image width + 2×margin.
  7. vertical mode: temporary directory has been cleaned up (/tmp/img2pdf-splits-* does not remain).