mybin

my ~/bin
git clone https://a3nm.net/git/mybin/
Log | Files | Refs | README

pdftosvg_fix (1329B)


      1 #!/bin/bash
      2 # PDF to SVG using pdftocairo
      3 # Usage: pdftosvg_fix INPUT OUTPUT
      4 # only for single-page documents
      5 # inspired by http://bazaar.launchpad.net/~suv-lp/+junk/inkscape-extensions/view/head:/pdf-input/pdf2svg-ext-custom.py
      6 # depends on xmlstarlet
      7 # thanks a lot to su_v on freenode #inkscape for help
      8 
      9 SCALE=100
     10 INPUT="$1"
     11 OUTPUT="$2"
     12 
     13 # a bit pedestrian but it works...
     14 while true
     15 do
     16   W=$(pdfinfo "$INPUT" | grep '^Page size' | cut -d':' -f2 | awk '{print $1}')
     17   H=$(pdfinfo "$INPUT" | grep '^Page size' | cut -d':' -f2 | awk '{print $3}')
     18   WW=$(echo "$W * $SCALE" | bc | sed 's/\.0*$//')
     19   HH=$(echo "$H * $SCALE" | bc | sed 's/\.0*$//')
     20   if echo "$WW $HH" | grep '\.' > /dev/null
     21   then
     22     SCALE=$(($SCALE*10))
     23     # loop again
     24   else
     25     break
     26   fi
     27 done
     28 
     29 # now SCALE is >= 100 and all dimensions are integers
     30 
     31 rm -f "$OUTPUT"
     32 pdftocairo -svg -paperw "$WW" -paperh "$HH" -expand "$INPUT" "$OUTPUT" \
     33     || (echo "pdftocairo invocation failed" && exit 2)
     34 
     35 if [ ! -f "$OUTPUT" ]
     36 then
     37   # pdftocairo sometimes fails silently
     38   echo "pdftocairo invocation silently failed"
     39   exit 2
     40 fi
     41 
     42 xmlstarlet ed --inplace -N N="http://www.w3.org/2000/svg" \
     43   --update '//N:svg/@width' --value "${W}pt" "$OUTPUT"
     44 xmlstarlet ed --inplace -N N="http://www.w3.org/2000/svg" \
     45   --update '//N:svg/@height' --value "${H}pt" "$OUTPUT"
     46