January 20, 2017

Automatically Split and Crop a Multi Page PDF File

My workflow for LaTeX documents with figures is that I typically first draw the figures in MS Visio, then export the drawing to PDF and finally crop the PDF file so that there is no white margin anymore. This is a little tedious when you have lots of figures and lots of updates on these figures…

I changed my workflow a bit. Meanwhile I draw all figures in the same Visio file. When you export this Visio to PDF, you get one PDF file with all your drawings inside. You can easily automate the task to split the PDF file into several files containing a single drawing. These can be cropped automatically as well.

    #!/bin/bash

    FILE=$1
    PATTERN="*_?.pdf"

    pdfseparate $FILE $(basename $FILE .pdf)_%d.pdf
    find $PATTERN -exec pdfcrop {} {} \;

Just call the script with ./split filename.pdf. This will produce PDF files named filename_1..n.pdf

pdfcrop will be available on a Mac when MacTex is installed. The pdfseparate tool is available via Homebrew with brew install poppler.

© holger 2015 - 2020 |