見出し画像

冊子印刷をページの並び替えで実現する(Mac/Win/Linux)

Macで冊子(Booklet)印刷する際に、ページの並び替えで実現する方法です。

わたしはEPSONのプリンターを使っています。このEPSONのプリンタードライバーでは、冊子印刷ができませんでした。

そのため、冊子印刷をするときは、

  1. 印刷のページイメージをPDFに出力する。

  2. そのPDFを並び替える。

  3. 2ページ/1ページ表示で、両面印刷する。

という方法でしていました。

去年あたりのアップデートで、冊子印刷ができるようになりました。

しかし、冊子印刷をするとページマージンが大きすぎる。用紙いっぱいに印刷したいときは、いまだ上記の"PDFを並び替える"方法を使っています。

以下は、ページの並べ替えときのページ順です。
印刷するときは、以下ののように並び替えた上で、プリンター印刷設定を

  • Paper Per Sheetを2ページ

  • Layout Directionを右、もしくは左(綴じ方向と同じ)

  • 両面(Two-sided)

  • Layout > Short-Edge binding

を選択ください。

一枚あたりに4ページ印刷できるため、冊子印刷では4の倍数でしか指定できません。4の倍数でないときは、後ろに空ページを挿入して、4の倍数に揃えてください。

ページが多いときは、作業が面倒です。分割して、4や8ページ構成を組み合わせた方が楽です。

4ページ構成
4,1,2,3

8ページ構成
8,1,2,7,6,3,4,5

12ページ構成
12,1,2,11,10,3,4,9,8,5,6,7

16ページ構成
16,1,2,15,14,3,4,13,12,5,6,11,10,7,8,9

20ページ構成
20,1,2,19,18,3,4,17,16,5,6,15,14,7,8,13,12,9,10,11

24ページ構成
24,1,2,23,22,3,4,21,20,5,6,19,18,7,8,17,16,9,10,15,14,11,12,13

28ページ構成
28,1,2,27,26,3,4,25,24,5,6,23,22,7,8,21,20,9,10,19,18,11,12,17,16,13,14,15

32ページ構成
32,1,2,31,30,3,4,29,28,5,6,27,26,7,8,25,24,9,10,23,22,11,12,21,20,13,14,19,18,15,16,17

36ページ構成
36,1,2,35,34,3,4,33,32,5,6,31,30,7,8,29,28,9,10,27,26,11,12,25,24,13,14,23,22,15,16,21,20,17,18,19

40ページ構成
40,1,2,39,38,3,4,37,36,5,6,35,34,7,8,33,32,9,10,31,30,11,12,29,28,13,14,27,26,15,16,25,24,17,18,23,22,19,20,21

これらを計算した時のメモ書きを先程見つけたのだけれど、いま見ると意味がわからない。よくやったものだ。

いまでは、これらの並び替えをシェルで行っています。リリースするほどのクオリティはありません。

分かる人へ、参考までに。
生成AIを使えば、改造できるはずです。

ImageMagick と img2pdf を使っています。

#!/bin/bash

# ディレクトリのパスを指定
directory_path="."
# 出力ディレクトリの名前を指定
output_directory="pmb-output"
# 結合方向のデフォルトを左から右に設定
direction="left"

# オプション解析
while getopts ":r" opt; do
  case $opt in
    r)
      direction="right"
      ;;
    \?)
      echo "無効なオプション: -$OPTARG" >&2
      exit 1
      ;;
  esac
done

shift $((OPTIND - 1))

# パスが指定されなかった場合エラーメッセージを表示して終了
if [ -z "$1" ]; then
  echo "エラー: 変換するPDFファイルのパスを指定してください。"
  exit 1
fi

pdf_file="$1"

# ファイル名を、拡張子を除いて抽出
filename=$(basename "$1")
pdf_org_filename="${filename%.*}"

# 出力ディレクトリを作成
mkdir -p "$output_directory"

# 既に存在する処理生成の画像ページを削除
rm -f "${output_directory}/bl"*.jpg
rm -f "${output_directory}/pmb"*.jpg
rm -f "${output_directory}/pmb"*.png

# convertコマンドを使用して指定されたPDFをpngに変換
# -qualityオプションは通常JPEG形式での品質を調整するために使われますが、PNG形式には効果がない
# convert -density 300 "$1" -quality 100 "${output_directory}/pmb%03d.png"
convert -density 600 "${pdf_file}" "${output_directory}/pmb%03d.png"

# 画像ファイルの一覧を取得して文字列に格納
image_files=$(find "${directory_path}/${output_directory}" -type f \
  \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.tif" \))

# 拡張子別にファイル名昇順にソート
sorted_image_files=$(echo "$image_files" | tr ' ' '\n' | sort)

# ファイル数ごとに順番を変更
file_count=$(echo "$sorted_image_files" | wc -l)


# ファイル数ごとに順番を変更
if [ "$file_count" -le 4 ]; then
    needed_blank_pages=$((4 - file_count))
elif [ "$file_count" -le 8 ]; then
    needed_blank_pages=$((8 - file_count))
elif [ "$file_count" -le 12 ]; then
    needed_blank_pages=$((12 - file_count))
elif [ "$file_count" -le 16 ]; then
    needed_blank_pages=$((16 - file_count))
elif [ "$file_count" -le 20 ]; then
    needed_blank_pages=$((20 - file_count))
elif [ "$file_count" -le 24 ]; then
    needed_blank_pages=$((24 - file_count))
elif [ "$file_count" -le 28 ]; then
    needed_blank_pages=$((28 - file_count))
elif [ "$file_count" -le 32 ]; then
    needed_blank_pages=$((32 - file_count))
elif [ "$file_count" -le 36 ]; then
    needed_blank_pages=$((36 - file_count))
elif [ "$file_count" -le 40 ]; then
    needed_blank_pages=$((40 - file_count))
fi

# 空白画像ページを生成
for ((i=0; i<needed_blank_pages; i++)); do
    # 空白ページの画像を作成
    first_image="${sorted_image_files%%$'\n'*}"
    convert -size $(identify -format "%wx%h" "$first_image") xc:white "./${output_directory}/bl${i}.jpg"
done

# 空白画像ページをファイルリストに追加
blank_files=$(find "./${output_directory}" -type f -iname "bl*.jpg")

sorted_image_files="${sorted_image_files}
"${blank_files}""

# ファイル数ごとに順番を変更したリストを出力
echo "${sorted_image_files}"
echo ""
echo ""

if [ "$file_count" -le 4 ]; then
    sorted_image_files=$(echo "$sorted_image_files" | awk 'BEGIN{ORS=RS} {a[NR]=$0} END{print a[4],a[1],a[2],a[3]}')
elif [ "$file_count" -le 8 ]; then
    sorted_image_files=$(echo "$sorted_image_files" | \
      awk 'BEGIN{ORS=RS} {a[NR]=$0} END{print a[8],a[1],a[2],a[7],a[6],a[3],a[4],a[5]}')
elif [ "$file_count" -le 12 ]; then
    sorted_image_files=$(echo "$sorted_image_files" | \
      awk 'BEGIN{ORS=RS} {a[NR]=$0} END{print a[12],a[1],a[2],a[11],a[10],a[3],a[4],a[9],a[8],a[5],a[6],a[7]}')
elif [ "$file_count" -le 16 ]; then
    sorted_image_files=$(echo "$sorted_image_files" | \
      awk 'BEGIN{ORS=RS} {a[NR]=$0} END{print a[16],a[1],a[2],a[15],a[14],a[3],a[4],a[13],a[12],a[5],a[6],a[11],a[10],a[7],a[8],a[9]}')
elif [ "$file_count" -le 20 ]; then
    sorted_image_files=$(echo "$sorted_image_files" | \
      awk 'BEGIN{ORS=RS} {a[NR]=$0} END{print a[20],a[1],a[2],a[19],a[18],a[3],a[4],a[17],a[16],a[5],a[6],a[15],a[14],a[7],a[8],a[13],a[12],a[9],a[10],a[11]}')
elif [ "$file_count" -le 24 ]; then
    sorted_image_files=$(echo "$sorted_image_files" | \
      awk 'BEGIN{ORS=RS} {a[NR]=$0} END{print a[24],a[1],a[2],a[23],a[22],a[3],a[4],a[21],a[20],a[5],a[6],a[19],a[18],a[7],a[8],a[17],a[16],a[9],a[10],a[15],a[14],a[11],a[12],a[13]}')
elif [ "$file_count" -le 28 ]; then
    sorted_image_files=$(echo "$sorted_image_files" | \
      awk 'BEGIN{ORS=RS} {a[NR]=$0} END{print a[28],a[1],a[2],a[27],a[26],a[3],a[4],a[25],a[24],a[5],a[6],a[23],a[22],a[7],a[8],a[21],a[20],a[9],a[10],a[19],a[18],a[11],a[12],a[17],a[16],a[13],a[14],a[15]}')
elif [ "$file_count" -le 32 ]; then
    sorted_image_files=$(echo "$sorted_image_files" | \
      awk 'BEGIN{ORS=RS} {a[NR]=$0} END{print a[32],a[1],a[2],a[31],a[30],a[3],a[4],a[29],a[28],a[5],a[6],a[27],a[26],a[7],a[8],a[25],a[24],a[9],a[10],a[23],a[22],a[11],a[12],a[21],a[20],a[13],a[14],a[19],a[18],a[15],a[16],a[17]}')
elif [ "$file_count" -le 36 ]; then
    sorted_image_files=$(echo "$sorted_image_files" | \
      awk 'BEGIN{ORS=RS} {a[NR]=$0} END{print a[36],a[1],a[2],a[35],a[34],a[3],a[4],a[33],a[32],a[5],a[6],a[31],a[30],a[7],a[8],a[29],a[28],a[9],a[10],a[27],a[26],a[11],a[12],a[25],a[24],a[13],a[14],a[23],a[22],a[15],a[16],a[21],a[20],a[17],a[18],a[19]}')
elif [ "$file_count" -le 40 ]; then
    sorted_image_files=$(echo "$sorted_image_files" | \
      awk 'BEGIN{ORS=RS} {a[NR]=$0} END{print a[40],a[1],a[2],a[39],a[38],a[3],a[4],a[37],a[36],a[5],a[6],a[35],a[34],a[7],a[8],a[33],a[32],a[9],a[10],a[31],a[30],a[11],a[12],a[29],a[28],a[13],a[14],a[27],a[26],a[15],a[16],a[25],a[24],a[17],a[18],a[23],a[22],a[19],a[20],a[21]}')
fi

#
# echo ${sorted_image_files}
# exit;

# 始めから2つずつ画像ファイルを左右に結合
counter=0
id=1
for file in $sorted_image_files; do
  if [ $counter -eq 0 ]; then
    first_file="$file"
    counter=1
  else
    second_file="$file"
    if [ "$direction" == "right" ]; then
      output_file="${output_directory}/pmb-output-R$(printf "%02d" $id)_$(basename "${second_file%.*}")-$(basename "${first_file%.*}").jpg"
      # convert +append "$second_file" "$first_file" "$output_file"
      # appendは"+"で横
      # -border 境界(マージン)を入れる 幅ピクセルx高ピクセル
      convert +append -bordercolor white -border 50x0 "$second_file" "$first_file" "$output_file"
    else
      output_file="${output_directory}/pmb-output-L$(printf "%02d" $id)_$(basename "${first_file%.*}")-$(basename "${second_file%.*}").jpg"
      # convert +append "$first_file" "$second_file" "$output_file"
      convert +append -bordercolor white -border 50x0 "$first_file" "$second_file" "$output_file"
    fi
    echo "画像が結合されました。出力ファイル: $output_file"
    counter=0
    id=$((id+1))
  fi
done

/Users/username/.pyenv/shims/img2pdf "${output_directory}"/pmb-output*.jpg -o "${directory_path}/${pdf_org_filename}_out_booklet_`date +"%Y%m%d-%H%M%S"`.pdf"

# 生成した画像ページを削除
# rm -f "${output_directory}/bl"*.jpg
# rm -f "${output_directory}/pmb"*.jpg
# rm -f "${output_directory}/pmb"*.png

この記事が気に入ったらサポートをしてみませんか?