Monday, May 2, 2022

Batch crop pdf pages in half with ghostscript and merge into single pdf

Get the page size and crop to desired dimensions with CropBox:

hckr@hckr:~$ pdfinfo img_00217.pdf
Creator:        OmniPage CSDK 21
Producer:       Document Capture
CreationDate:   Mon May  2 09:49:51 2022 CEST
ModDate:        Mon May  2 09:49:51 2022 CEST
Tagged:         no
UserProperties: no
Suspects:       no
Form:           none
JavaScript:     no
Pages:          1
Encrypted:      no
Page size:      770.64 x 577.44 pts
Page rot:       0
File size:      156189 bytes
Optimized:      no
PDF version:    1.4

Thus 770/2. And the offset for the right side.

Make a script to split all pdf files in the folder in two halves, left and right:

Test and remove "echo" if it looks good.

 
 
#!/bin/bash
for f in *.pdf; do
   echo gs -o "${f%.pdf}-right.pdf" -sDEVICE=pdfwrite  -c "[/CropBox [385 0 771 578]" -c " /PAGES pdfmark" -f "$f"
   echo gs -o "${f%.pdf}-left.pdf" -sDEVICE=pdfwrite  -c "[/CropBox [0 0 385 578]" -c " /PAGES pdfmark" -f "$f"
done

Make executable

hckr@hckr:~$ chmod +x splitpdf

Run file

hckr@hckr:~$ ./splitpdf

Merge left and right files into output.pdf

hckr@hckr:~$ gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=output.pdf 
img_00217-right.pdf img_00216-right.pdf img_00215-left.pdf