Sort PDF pages by shell script -


i'd scan huge documents using adf of scanner. because not duplex adf, cannot automatically scan two-sided pages. is:

  1. scan odd pages.
  2. scan pages.

what pdf file page number pattern:

1 3 5 2 4

(how) can sort pdf using shell script? there pattern, should possible.

this might started. first extract pages separate files. filenames in bash array. sort array. recombine pages.

#!/bin/bash  # extract pages "orig-000.png", "orig-001.png" pdfimages -png "$1" orig  # make array of names of pages orig=($(ls orig-*png))  echo extracted pages: echo @{orig[@]}  npages=${#orig[@]} echo pages: $npages  # ii = input index # oi = output index halfway=$(echo "($npages-1)/2" | bc) oi=0 for((ii=0;ii<npages;ii++));    [[ ii -eq $halfway ]] && oi=1;    echo $ii,$oi    out[oi]=${orig[ii]}    ((oi+=2)) done  echo sorted pages: echo ${out[@]}  # reassemble pages - suggesting imagemagick's "convert" os may have better tools echo convert ${out[@]} sorted.pdf 

output - 9 page pdf

extracted pages: orig-000.png orig-001.png orig-002.png orig-003.png orig-004.png orig-005.png orig-006.png orig-007.png orig-008.png pages: 9 0,0 1,2 2,4 3,6 4,1 5,3 6,5 7,7 8,9 sorted pages: orig-000.png orig-004.png orig-001.png orig-005.png orig-002.png orig-006.png orig-003.png orig-007.png orig-008.png convert orig-000.png orig-004.png orig-001.png orig-005.png orig-002.png orig-006.png orig-003.png orig-007.png orig-008.png sorted.pdf 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -