nfr/pdf2book
nfr
/
pdf2book
Archived
1
0
Fork 0

Initial commit

This commit is contained in:
Nils Freydank 2020-11-11 11:30:35 +01:00
commit 4ae795dd55
No known key found for this signature in database
GPG Key ID: BC5DC2998AAD2B21
3 changed files with 115 additions and 0 deletions

19
LICENSE Normal file
View File

@ -0,0 +1,19 @@
Copyright 2020 Nils Freydank <holgersson@posteo.de>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

14
README.txt Normal file
View File

@ -0,0 +1,14 @@
# pdf2book
This repo contains a small script to reorganize PDF from a normal increasing
page numbering into a printable set of sheets which can be foled into a book.
## usage
pdf2book <input.pdf> <output.pdf>
## Author and license
The author of this script is Nils Freydank. The script is licensed as MIT.
Details can be found inside the file LICENSE.
<!--
vim:fileencoding=utf-8:ts=4:syntax=markdown:colorcolumn=81:noexpandtab
-->

82
pdf2book.sh Executable file
View File

@ -0,0 +1,82 @@
#!/bin/sh
# Reorganize PDF for book-alike printing
# -- w/o cover i.e. page 1 is the cover page --
#
# Author: Nils Freydank <holgersson@posteo.de>
# Copyright: 2020 by Nils Freydank
# License: MIT
# Version: 2020-11-11
#
# dependencies:
# - bash
# - coreutils (date, mkdir)
# - groff (for empty page generation)
# - poppler (pdfseperate, pdfunite)
PATH="/bin:/usr/bin"
INPUT_FILE="${1:-input.pdf}"
OUTPUT_FILE="${2:-printable_output.pdf}"
TMPDIR="tmp_$(date --utc +%s)"
mkdir -p "${TMPDIR}"
# Separate the document into single pages.
pdfseparate "${INPUT_FILE}" "${TMPDIR}/%d.pdf"
# Rearrange the pages:
# 1. Find out the highest page number.
# 2. If page number can't be devided by 4 (i.e. modulus operation is not zero),
# then align/fill with empty pages until it can.
# 3. Create an array with old -> new mappings.
# 4. Move the pages.
# -- step 1 of 4 --
PAGE_COUNTER="$(ls "${TMPDIR}" | wc -l)"
# -- step 2 of 4 --
while [[ $((${PAGE_COUNTER} % 4)) != 0 ]]
do
# Create an new empty page.
echo "" | groff -T pdf > ${TMPDIR}/$((${PAGE_COUNTER} + 1)).pdf
# Re-scan the files.
PAGE_COUNTER="$(ls "${TMPDIR}" | wc -l)"
done
# -- step 3 of 4 --
NEW_PAGE_LIST=()
ctr_down="${PAGE_COUNTER}"
# Note: Start at 1 as this is our first page.
ctr_up=1
while [[ ${ctr_up} -le $((${PAGE_COUNTER} / 2 )) ]]
do
NEW_PAGE_LIST+=( ${ctr_down} )
NEW_PAGE_LIST+=( ${ctr_up} )
ctr_down=$((${ctr_down} - 1))
ctr_up=$((${ctr_up} + 1))
NEW_PAGE_LIST+=( ${ctr_up} )
NEW_PAGE_LIST+=( ${ctr_down} )
ctr_down=$((${ctr_down} - 1))
ctr_up=$((${ctr_up} + 1))
done
# -- step 4 of 4 --
# Warning: Remember to start at zero as an array is accessed!
for ((i=0; i < ${#NEW_PAGE_LIST[*]}; i++))
do
if [[ $((i + 1)) -le 9 ]]
then
mv "${TMPDIR}/${NEW_PAGE_LIST[$i]}.pdf" "${TMPDIR}/newpage-0$((i + 1)).pdf"
else
mv "${TMPDIR}/${NEW_PAGE_LIST[$i]}.pdf" "${TMPDIR}/newpage-$((i + 1)).pdf"
fi
done
# Re-unite the sorted pages for printing.
pdfunite "${TMPDIR}"/newpage-* "${OUTPUT_FILE}"
# Cleanup intermediate data.
rm -rf "${TMPDIR}"
# vim:fileencoding=utf-8:ts=4:syntax=bash:colorcolumn=81:noexpandtab