LoginSignup
13
8

More than 5 years have passed since last update.

A4のPDFをA3に2面付(2in1)する -- Python(PyPDF2)

Posted at

A4のPDFを左右に並べてA3で書き出します。
2in1 とか 2UP とか言うんですかね?

A4-2in1-Simple.py
import PyPDF2

a3_width = 1190.5511811024
a3_height = 841.8897637795

pdf_file = open('A4.pdf','rb')
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
page_obj = pdf_reader.getPage(0)

# A3 の台紙を生成
base_page = PyPDF2.pdf.PageObject.createBlankPage(width=a3_width, height=a3_height)
# A3の左にPDFを配置
base_page.mergePage(page_obj)
# A3の右にPDFを配置
base_page.mergeRotatedScaledTranslatedPage(page_obj, 0, 1, a3_width / 2, 0, expand=False)

pdf_writer = PyPDF2.PdfFileWriter()
pdf_writer.addPage(base_page)

pdf_output_file = open('A3-2in1.pdf','wb')
pdf_writer.write(pdf_output_file)

pdf_output_file.close()
pdf_file.close()

Acrobat等のアプリケーションでも可能ですが、余白がついてしまい100%等倍で出力できません。
イラストレーターだと処理できますが、重いアプリ立ち上げたくないケースもありますよね?

ネットで検索しても、あまりヒットしなかったので、「退屈なことはPythonにやらせよう」のサンプルコードとPyPDF2のドキュメントを参考にして書いてみました。

A3用紙の(ポイント)サイズは、イラストレーターで作成したA3のドキュメントのサイズをAppleScriptで取得した数値です。

getDocSize.scpt
tell application "Adobe Illustrator"
    tell document 1
        {width, height}
    end tell
end tell
13
8
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
13
8