0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

A0ポスターを分割してA4用紙16枚で印刷する

Last updated at Posted at 2023-05-31

背景

  • 学会発表に用いるA0サイズのポスターをA4サイズ16枚で分割して印刷したい
    • Adobe Acrobat のポスター印刷がなぜかうまくいかない
    • →ファイル自体を分割してしまおう!

準備

pypdf というパッケージを使います

pip install pypdf

コード

import pathlib

import pypdf

# file path to the pdf
pdf_path = input("Please input pdf path: ")

# Read pdf file
pdf_file = open(pdf_path, "rb")
pdf_reader = pypdf.PdfReader(pdf_file)

# Output directory
outdir = pathlib.Path("output")
pathlib.Path.mkdir(outdir, exist_ok=True)

# Confirm pdf size
p1 = pdf_reader.pages[0]
doc_width = p1.cropbox.width
doc_height = p1.cropbox.height
print(doc_width, doc_height)  # in mm

# the number of division
N = 4

# Save separately
for c in range(N):
    for r in range(N):
        p1 = pdf_reader.pages[0]
        lower_left = (doc_width / N * c, doc_height / N * r)
        upper_right = (doc_width / N * (c + 1), doc_height / N * (r + 1))
        print((c, r), lower_left, upper_right)
        p1.mediabox.lower_left = lower_left
        p1.mediabox.upper_right = upper_right

        writer = pypdf.PdfWriter()
        writer.add_page(p1)
        output_path = outdir / f"cropped_{c}-{r}.pdf"
        with open(output_path, "wb") as output_file:
            writer.write(output_file)

# Save as single file
writer = pypdf.PdfWriter()
for c in range(N):
    for r in range(N):
        p1 = pdf_reader.pages[0]
        lower_left = (doc_width / N * c, doc_height / N * r)
        upper_right = (doc_width / N * (c + 1), doc_height / N * (r + 1))
        print((c, r), lower_left, upper_right)
        p1.mediabox.lower_left = lower_left
        p1.mediabox.upper_right = upper_right

        writer.add_page(p1)

output_path = outdir / f"cropped.pdf"
with open(output_path, "wb") as output_file:
    writer.write(output_file)

参考資料

  • A0サイズ1ページのPDFをA4サイズ16ページのPDFに分割する - Qiita
    • 上記記事と本記事との違い
      • 本記事の方法はドキュメントの大きさに合わせて分割するので,841 などの数字をハードコーディングしなくてよい
      • ドキュメントが1:√2かつ分割する大きさ (コード中の N ) が2の倍数であれば,A0→A4以外の分割も可能
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?