Pages

Sunday, 9 February 2014

PDF - Two pages per page to one pages per page

So we had a PDF, it's size was 297 x 215 mm. Each page of this PDF contained two pages, like this:
 Page 1 of the input file                       Page 1 of output   Page 2 of output
    +------------------------------+            +---------------+  +---------------+
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    | Page 1        | Page 2       |            |   Page 1      |  |   Page 2      |
    |  of the book  |  of the book |            |    of the book|  |    of the book|
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    +------------------------------+            +---------------+  +---------------+
                                     +------->
 Page 2 of the input file            +------->  Page 3 of output   Page 4 of output
    +------------------------------+            +---------------+  +---------------+
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    | Page 3        | Page 4       |            |   Page 3      |  |   Page 4      |
    |  of the book  |  of the book |            |    of the book|  |    of the book|
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    |               |              |            |               |  |               |
    +------------------------------+            +---------------+  +---------------+
So to do this, I extracted the left sides as page-0.pdf, and the right sides as page-1.pdf to the folder out, and the n joined these together:
#!/bin/bash
set -eux

# Default resolution is 720 dpi
# For points, use 72 dpi

# The actual pdf was 297 x 215 mm --(720 dpi)----> 8418 x 6094 pixels
# I only want to extract half of it -------------> 4209 x 6094
# For the right page, I need an offset -(72 dpi)-> -421.0 0

INPUT_FILE="$1"

rm -rf ./out
mkdir out

ghostscript -o "out/%d-0.pdf" -sDEVICE=pdfwrite -g4209x6094 \
-c "<</PageOffset [0 0]>> setpagedevice" -f "$INPUT_FILE"

ghostscript -o "out/%d-1.pdf" -sDEVICE=pdfwrite -g4209x6094 \
-c "<</PageOffset [-421.0 0]>> setpagedevice" -f "$INPUT_FILE"

ghostscript -o "singlepage-$INPUT_FILE" -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook \
    $(ls out/* | sort -V)

rm -rf ./out
References:
http://superuser.com/questions/54054/convert-pdf-2-sides-per-page-to-1-side-per-page

No comments:

Post a Comment