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?

PyMuPDF を用いて印刷用の PDF を作成 (2)

Last updated at Posted at 2025-01-10

2 年ほど前にこの記事で PyMuPDF を用いて無線綴じで印刷する場合のページ割付を行うためのコードを紹介したが、今回は「中綴じ用」のそれである。

中綴じ用のページ割付

以下のコードで saddle_stitching.pdf が生成される。

import sys
import fitz

def main(filename):

    src = fitz.open(filename)
    page_count = src.page_count
    page = src.load_page(0)
    width = page.rect.width
    height = page.rect.height

    dst = fitz.open()

    imax = int((page_count + 3) / 4) * 2
    if page_count % 4 == 0:
        j = page_count - 1
    else:
        j = imax * 2 - 1
    k = 0
    for i in range(imax):
        if i % 2 == 0:
            if j >= page_count:
                dst.new_page(-1, width = width, height = height)
            else:
                dst.insert_pdf(src, from_page = j, to_page = j)
            dst.insert_pdf(src, from_page = k, to_page = k)
        else:
            dst.insert_pdf(src, from_page = k, to_page = k)
            if j >= page_count:
                dst.new_page(-1, width = width, height = height)
            else:
                dst.insert_pdf(src, from_page = j, to_page = j)

        j -= 1
        k += 1

    dst.save('saddle_stitching.pdf')

if __name__ == "__main__":
    if len(sys.argv) == 2:
        main(sys.argv[1])

2in1

PyMuPDF を使って一気に処理することも可能であるが、PDFMate Free PDF Merger で 2in1 印刷用の PDF に変換している。

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?