diff --git a/fcw2pdf/fcw2pdf.sh b/fcw2pdf/fcw2pdf.sh new file mode 100755 index 0000000..c78dffe --- /dev/null +++ b/fcw2pdf/fcw2pdf.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +# fcw2pdf.sh: a simple fcw to PDF converter script. +# Copyright 2017 Christopher Spinrath + +# fcw2pdf.sh is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# fcw2pdf.sh is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with fcw2pdf.sh. If not, see . + +set -e + +INPUT_FILE=$1 + +if [ "x$1" == "x" ]; then + echo "Usage: fcw2pdf.sh []" + exit 1 +fi + +INPUT_BASENAME="$(basename "$INPUT_FILE" ".fcw")" +shift + +OUTPUT_FILE=${1:-"${INPUT_BASENAME}.pdf"} + +WORKDIR="$(mktemp -d)" +function cleanup { + rm -Rf "$WORKDIR" +} +trap cleanup EXIT + +cp "$INPUT_FILE" "$WORKDIR/in.zip" +pushd "$WORKDIR" > /dev/null +unzip "in.zip" > /dev/null + +#We could just go with $(ls *.svg) but then the files may be in the wrong order. +#Hence, let's look up the files in the correct order in the manifest file. +MANIFEST_FILE=imsmanifest.xml +PAGE_FILE_QUERY='//resources/resource[@identifier="pages"]/file/@href' +PAGE_FILE_QUERY_RES=$(xmllint --html --xpath "$PAGE_FILE_QUERY" "$MANIFEST_FILE" 2> /dev/null) + +#xmllint's return format is crap; clean it up +PAGE_FILES=$(echo "$PAGE_FILE_QUERY_RES" | tr "[:space:]" "\n" | cut -d\" -f2 | grep -v '^$') + +PDF_FILES="" +for PAGE_FILE in $PAGE_FILES; do + PAGE_BASENAME="$(basename "$PAGE_FILE" ".svg")" + PAGE_PDF="$PAGE_BASENAME.pdf" + PDF_FILES="$PDF_FILES $PAGE_PDF" + echo "Converting $PAGE_BASENAME" + #convert -density 90 -define pdf:fit-page=A4 "$PAGE_FILE" "$PAGE_PDF" + #pass input via stdin, otherwise cairosvg seems to be unable to open included image files + cairosvg -o "$PAGE_PDF" - < "$PAGE_FILE" +done + +pdfunite $PDF_FILES out.pdf + +popd > /dev/null +cp "$WORKDIR/out.pdf" "$OUTPUT_FILE"