Skip to content

Instantly share code, notes, and snippets.

@pkgw
Created October 15, 2012 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkgw/3892706 to your computer and use it in GitHub Desktop.
Save pkgw/3892706 to your computer and use it in GitHub Desktop.
Helper to use XPDF to extract figures from PDFs as vector graphics
#! /bin/bash
# -*- shell-script -*-
margin=4
# XPDF gives its y coordinates in terms of the standard PDF coordinate
# system, where (0,0) is the bottom left corner and y increases going
# up. But pdftocairo uses Cairo coordinates, in which (0,0) is the top
# left corner and y increases going down. We can use pdfinfo to get
# the page size to translate between these conventions.
file="$1"
page="$2"
pageh=$(pdfinfo -f $page -l $page "$file" |grep '^Page.*size' \
|sed -e 's/.* x //' -e 's/pts.*$//')
# Our variables end up in Cairo convention, so the box height is ybr -
# ytl.
xtl=$(python -c "import math; print int (math.floor ($3))")
ytl=$(python -c "import math; print $pageh - int (math.ceil ($4))")
xbr=$(python -c "import math; print int (math.ceil ($5))")
ybr=$(python -c "import math; print $pageh - int (math.floor ($6))")
w=$(python -c "print $xbr - $xtl")
h=$(python -c "print $ybr - $ytl")
# Lamebrained uniqifying of output filename.
n=1
while [ -f fig$n.pdf ] ; do
n=$((n + 1))
done
# OK to go.
echo pdftocairo -pdf -f $page -l $page -x $xtl -y $ytl -W $w -H $h \
-paperw $w -paperh $h "$file" - '|'pdfcrop --margin $margin - fig$n.pdf
exec pdftocairo -pdf -f $page -l $page -x $xtl -y $ytl -W $w -H $h \
-paperw $w -paperh $h "$file" - |pdfcrop --margin $margin - fig$n.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment