#!/bin/bash
# PDF support

set -e


######
# Native dependencies

pdf_client_packages=( pdftk poppler-utils )


######
# Settings

doc_pdf_config_init() {
	lib_setting_arrays pdftk_inputs pdftk_op
	lib_setting_vars --null \
			pdftk_input_pw pdftk_output \
			pdftk_allow pdftk_owner_pw pdrtk_user_pw
	lib_setting_vars \
			pdftk_flatten pdftk_appearances \
			pdftk_compress pdftk_uncompress

	pdftk_flatten=false
	pdftk_appearances=false
	pdftk_compress=false
	pdftk_uncompress=false
}


######
# pdftk support

run_pdftk() {
	local -a opts=( "${pdftk_inputs[@]}" )

	[ -z "$pdftk_input_pw" ] || opts+=( input_pw "$pdftk_input_pw" )
	[ -z "${pdftk_op[*]}" ] || opts+=( "${pdftk_op[@]}" )
	[ "$pdftk_output" ] || error "\$pdftk_output is not set"
	opts+=( output "$pdftk_output" )

	[ -z "${pdftk_allow}" ] \
		|| opts+=( allow "$pdftk_allow" )
	[ -z "${pdftk_owner_pw}" ] \
		|| opts+=( owner_pw "$pdftk_owner_pw" )
	[ -z "${pdftk_user_pw}" ] \
		|| opts+=( user_pw "$pdftk_user_pw" )

	! $pdftk_flatten || opts+=( flatten )
	! $pdftk_appearances || opts+=( need_appearances )
	! $pdftk_compress || opts+=( compress )
	! $pdftk_uncompress || opts+=( uncompress )
	! $verbose || opts+=( verbose )

	run pdftk "${opts[@]}"
}

run_pdftk_op() {
	local -a pdftk_inputs=( "$1" )
	local pdftk_output=$2
	shift 2
	local -a pdftk_op=( "$@" )
	run_pdftk
}

pdf_burst() {
	has_args 2 "$@"
	run_mkdir "$2"
	run_pdftk_op "$1" "$2/%04d.pdf"
}
pdf_cat() { has_args 3 "$@"; run_pdftk_op "$1" "$2" cat "$3"; }
pdf_stamp() { has_args 3 "$@"; run_pdftk_op "$1" "$2" stamp "$3"; }
pdf_overlay() { has_args 3 "$@"; run_pdftk_op "$1" "$2" multistamp "$3"; }

pdf_lock() {
	has_args 3 "$@"
	local pdftk_owner_pw=$3
	run_pdftk_op "$1" "$2"
}
pdf_encrypt() {
	has_args 3 "$@"
	local pdftk_user_pw=$3
	run_pdftk_op "$1" "$2"
}

pdf_encrypt_ro() {
	has_args 3 "$@"
	local pdftk_owner_pw=$3
	local pdftk_user_pw=$3
	run_pdftk_op "$1" "$2"
}

pdf_decrypt() {
	has_args 3 "$@"
	local pdftk_input_pw=$3
	run_pdftk_op "$1" "$2"
}
pdf_fair_use() {
	has_args 2 "$@"
	pdftk_allow='AllFeatures'
	run_pdftk_op "$1" "$2"
}

pdf_join() {
	min_args 2 "$@"
	local pdftk_output=$1
	shift
	local -a pdftk_inputs=( "$@" )
	local -a pdftk_op=( cat )
	run_pdftk
}

######
# poppler-utils support

pdf_page_count ()
{
	local name=$1
	if [ "${name/.pdf}" = "$name" ]; then
		info "$(basename "$name" .pdf): skipped"
	else
		pdfinfo "$name" \
			| grep -oP '(?<=Pages:          )[ A-Za-z0-9]*'
	fi
}

######
# GUI support

run_pdf_viewer() { run okular "$@"; }

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:35:34 PDT 2017 by mcsh d14 v0.23.0.