#!/bin/bash # # Syntax: pdf_extract_pages_gs [-h] range pdf_file # # Purpose: Use gs to extract pages from a pdf file. # Page ranges can be specified as X-Y, X-end, or as begin-Y # Output is stored in the file pdf_file_pages.pdf # The -h flag shows the help info for this command. # # See also: pdf_extract_pages (based on pdftk) # Created: Jan 1, 2011 by Bart De Schutter # Last revised: Jan 11, 2011 by Bart De Schutter # # See http://www.deschutter.info/util/scripts.html for the latest version of # this script. # Status: public # Category: pdf # # Define default options # start="" end="" pdf_option="" # # Process options # while getopts :h option do case "$option" in h) sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1;; \?) echo >&2 echo >&2 "Unknown option: $OPTARG" echo >&2 sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1;; esac done shift $(($OPTIND-1)) if [ $# -ne 2 ]; then echo >&2 echo >&2 "ERROR: ${0##*/} requires exactly 2 input arguments." echo >&2 sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1 fi range="$1" case "$range" in begin-[0-9]*) start="" end=${range#begin-};; [0-9]*-end) start=${range%-end} end="";; [0-9]*-[0-9]*) start=${range%-[0-9]*} end=${range#[0-9]*-};; *) echo >&2 echo >&2 "ERROR: Invalid page range $range for ${0##*/}." echo >&2 exit 1;; esac pdf_option="" if [ -n "$start" ] then pdf_option="$pdf_option -dFirstPage=$start" fi if [ -n "$end" ] then pdf_option="$pdf_option -dLastPage=$end" fi pdf_file="$2" if [ ! -e "$pdf_file" ]; then echo >&2 echo >&2 "ERROR: File $pdf_file not found." echo >&2 sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1 fi gs -q -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER \ $pdf_option \ -dCompatibilityLevel=1.4 \ -dEmbedAllFonts=true -dSubsetFonts=true -dMaxSubsetPct=100 \ -dAutoFilterColorImages=false -dAutoFilterGrayImages=false \ -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode \ -dAutoFilterMonoImages=false -dMonoImageFilter=/CCITTFaxEncode \ -sOutputFile="${pdf_file%.pdf}_pages.pdf" "$pdf_file" # Extra option: -sPAPERSIZE=a4 # -dPDFSETTINGS=/prepress