Skip to content

Instantly share code, notes, and snippets.

@jacquesfize
Last active October 9, 2018 07:14
Show Gist options
  • Save jacquesfize/4f8a42637fab8632d5545bf3e850b666 to your computer and use it in GitHub Desktop.
Save jacquesfize/4f8a42637fab8632d5545bf3e850b666 to your computer and use it in GitHub Desktop.
Bash script that paginates a pdf file
#! /bin/bash
# If you use MacOS, download the latest version of pdftk from here :
# https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-2.02-mac_osx-10.11-setup.pkg
function exists(command){
if ! [ -x "$(command -v $command)" ]; then
echo "Error: $command is not installed." >&2
exit 1
fi
}
# Check if required tools exist
exists(pdflatex)
exists(pdftk)
input=$1 #Input PDF filename
output_name=$2 # Output PDF filename
# Retrieve the number of pages from the original
n_page=$(pdftk $input dump_data|grep NumberOfPages| awk '{print $2}')
# Build a stamp
echo " \documentclass[12pt,a4paper]{article}
\usepackage{multido}
\usepackage[hmargin=.8cm,vmargin=1.5cm,nohead,nofoot]{geometry}
\begin{document}
\multido{}{$n_page}{\vphantom{x}\newpage}
\end{document}" > number.tex
pdflatex number.tex
# Add the "numbered" stamp to the pdf
pdftk $input multistamp numbers.pdf output $output_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment