#!/nix/store/z6rd8wq02azalrlm2m5k08iy53klg624-bash-5.1-p12/bin/bash # Usage: pdf2{odt,ods} [ options ] input.{jpg,pdf,png} ... output.{odt,ods} # # This script converts one or more PDF, JPG, or PNG files to an ODT or ODS # file. The contents of any PDF file(s) is first converted to a set of image # files. These files are then inserted as background images in the ODT or # ODS file. # # Copyright (c) 2011 Markus Gutschke. All rights reserved. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to # deal in the Software without restriction, including without limitation the # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or # sell copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. export LC_ALL=C # Check arguments [ $# -ge 2 ] || { echo "Usage:" >&2 echo " $(/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/basename "$0") [ options ] input.{jpg,pdf,png} ... output.{odt,ods}" >&2 echo >&2 echo "Options:" >&2 echo " -- Stop scanning for options." >&2 echo " --format={odt|ods} Select output file format." >&2 echo " --margin=X{cm|in} Add extra margin around input files." >&2 echo " --paper={letter|a4} Select paper size." >&2 echo " --portrait Portrait is always the default orientation." >&2 echo " --landscape Assume all input files are in landscape format." >&2 echo " --resolution=RES Change resolution of embedded images." >&2 echo " Only affects PDF files. PDF/PNG files will not" >&2 echo " be rescaled." >&2 echo " --trim|--no-trim Attempt to trim existing margins from input files." >&2 exit 1 } # Attempt to deduce the output file format from the output file name. eval out="\${$#}" format="${out##*.}"; format="$(printf '%s' "${format}"|/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/tr A-Z a-z)" [ "x${format}" = "xodt" -o "x${format}" = "xods" ] || format="${0: -3}" [ "x${format}" = "xodt" -o "x${format}" = "xods" ] || format="odt" # Select default papersize. papersize="${PAPERSIZE:-$(/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat "${PAPERCONF:-/etc/papersize}" 2>/dev/null || :)}" [ "x${papersize}" != "a4" ] && papersize="letter" # Parse position-independent command line flags. pdf=("$@"); unset pdf[$((${#pdf[@]}-1))] margin="0in" orientation="portrait" i=0; while [ ${i} -lt ${#pdf[@]} ]; do if [ "x${pdf[${i}]#-}" != "x${pdf[${i}]}" ]; then case "${pdf[${i}]}" in --) break ;; --format|--format=*) if [ "x${pdf[${i}]#--format=}" != "x${pdf[${i}]}" ]; then format="${pdf[${i}]#--format=}" else i=$((${i} + 1)) format="${pdf[${i}]}" fi format="$(printf '%s' "${format}"|/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/tr A-Z a-z)" if [ "x${format}" != "xodt" -a "x${format}" != "xods" ]; then echo "Unsupported output format \"${format}\"." >&2 exit 1 fi ;; --margin|--margin=*) if [ "x${pdf[${i}]#--margin=}" != "x${pdf[${i}]}" ]; then margin="${pdf[${i}]#--margin=}" else i=$((${i} + 1)) margin="${pdf[${i}]}" fi if [ "x${margin%in}" == "x${margin}" -a \ "x${margin%cm}" == "x${margin}" ]; then echo "Unsupported margin \"${margin}\" provided." >&2 exit 1 else margin="${margin/-/_}" fi ;; --paper|--paper=*) if [ "x${pdf[${i}]#--paper=}" != "x${pdf[${i}]}" ]; then papersize="${pdf[${i}]#--paper=}" else i=$((${i} + 1)) papersize="${pdf[${i}]}" fi if [ "x${papersize}" != "xletter" -a "x${papersize}" != "xa4" ]; then echo "Unsupported paper size \"${papersize}\" provided." >&2 exit 1 fi ;; --portrait) orientation="portrait" ;; --landscape) orientation="landscape" ;; --resolution=*) ;; --resolution) i=$((${i} + 1)) ;; --trim|--no-trim) ;; *) echo "Unknown command line option \"${pdf[${i}]}\"." >&2 exit 1 ;; esac else if [ ! -r "${pdf[${i}]}" ]; then echo "Invalid or non-existent filename \"${pdf[${i}]}\"." >&2 exit 1 fi fi i=$((${i} + 1)) done [ "${format}" = odt ] && formatname="Text" || formatname="Spreadsheet" if [ "${papersize}" = "letter" ]; then units="in" units2inch="1" width="8.5" height="11" if [ "x${margin%in}" != "x${margin}" ]; then margin="${margin%in}" else margin="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "3k ${margin%cm} 2.54/p")" fi else units="mm" units2inch="25.4" width="210" height="297" if [ "x${margin%cm}" != "x${margin}" ]; then margin="${margin%cm}" else margin="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "3k ${margin%in} 2.54*p")" fi fi if [ "${orientation}" != "portrait" ]; then w="${width}" width="${height}" height="${w}" fi # Don't overwrite the output file, unless it is an ODT/ODS file already. # This protects users from overwriting input files, if they completely # forgot to specify any output file. if [ -e "${out}" ] && [[ ! "$(/nix/store/qpq3ylbybkq1dzbvksnwqmv3jr6wa20c-file-5.41/bin/file "${out}" 2>/dev/null)" =~ "OpenDocument ${formatname}" ]]; then echo "Did you forget to specify the output file name? \"${out}\"" \ "exists, but is not an $(printf '%s' "${format}"|/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/tr a-z A-Z) file" >&2 exit 1 fi # Set up temporary staging directory TMPDIR=$(/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mktemp -d) # Adjust DPI so that the image fits on a letter- or a4-sized page. function scale() { local res="$(/nix/store/3vc3yi41531pnd21arjmkrr89z7ln8nq-imagemagick-7.1.0-20/bin/identify "${1}")" res="$(echo "x ${res#${1}}"|/nix/store/vr8p2is1j4n68gwr1hj4r061pf6462ng-gawk-5.1.1/bin/awk '{ print $3 }')" local dpiX=$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "3k ${res%x*} ${width} ${margin} 2*- ${units2inch}/0k/p") local dpiY=$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "3k ${res#*x} ${height} ${margin} 2*- ${units2inch}/0k/p") [ ${dpiX} -gt ${dpiY} ] && local dpi="${dpiX}" || local dpi="${dpiY}" /nix/store/3vc3yi41531pnd21arjmkrr89z7ln8nq-imagemagick-7.1.0-20/bin/convert "${1}" -set units PixelsPerInch -set density "${dpi}" "${2}" } # Preprocess image file(s), and convert PDF file(s) to images /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mkdir -p "${TMPDIR}/Pictures" endofargs= resolution=300 trim= i=0; while [ ${i} -lt ${#pdf[@]} ]; do if [ -z "${endofargs}" -a "x${pdf[${i}]#-}" != "x${pdf[${i}]}" ]; then case "${pdf[${i}]}" in --) endofargs=t ;; --format=*|--margin=*|--paper=*|--portrait|--landscape) ;; --format|--margin|--paper) i=$((${i} + 1)) ;; --resolution*) if [ "x${pdf[${i}]#--resolution=}" != "x${pdf[${i}]}" ]; then resolution="${pdf[${i}]#--resolution=}" else i=$((${i} + 1)) resolution="${pdf[${i}]}" fi if [ "x${resolution}" != "x$(printf '%s' "${resolution}"|/nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed 's/[^0-9]//')" ] || [ "${resolution}" -lt 50 -o "${resolution}" -gt 2400 ]; then echo "Invalid resolution \"${resolution}\" provided." >&2 exit 1 fi ;; --trim) trim="t" ;; --no-trim) trim= ;; *) echo "Unknown command line option \"${pdf[${i}]}\"." >&2 exit 1 ;; esac else suffix="${pdf[${i}]##*.}"; suffix="$(printf '%s' "${suffix}"|/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/tr A-Z a-z)" n=$(($(/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls "${TMPDIR}/Pictures/Image-"* 2>/dev/null | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/wc -l) + 1)) case "$suffix" in png|jpg|jpeg) if [ "$suffix" = "jpg" ]; then suffix="jpeg"; fi in="${pdf[${i}]}" ot="${TMPDIR}/Pictures/Image-$(printf '%03d' $n).$suffix" [ -n "${trim}" ] && { /nix/store/3vc3yi41531pnd21arjmkrr89z7ln8nq-imagemagick-7.1.0-20/bin/convert "${in}" "${ot}"; in="${ot}"; } scale "${in}" "${ot}" ;; *) /nix/store/w0k1ip6cizl1a8lf6sxvynf504xzdjw3-ghostscript-9.55.0/bin/gs -dNOPAUSE -sDEVICE=png256 \ -sOutputFile="${TMPDIR}/Pictures/TMP-%03d.png" \ -q -r"${resolution}" -dBATCH "${pdf[${i}]}" for j in "${TMPDIR}/Pictures/TMP-"*".png"; do in="${j}" ot="${TMPDIR}/Pictures/Image-$(printf '%03d' $n).png" [ -n "${trim}" ] && { /nix/store/3vc3yi41531pnd21arjmkrr89z7ln8nq-imagemagick-7.1.0-20/bin/convert "${in}" "${ot}"; in="${ot}"; } if [ "${margin}" != "0" ]; then scale "${in}" "${ot}" [ "${in}" != "${ot}" ] && /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/rm -f "${in}" elif [ "${in}" != "${ot}" ]; then /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mv "${in}" "${ot}" fi n=$((${n} + 1)) done ;; esac fi i=$((${i} + 1)) done /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mkdir -p "${TMPDIR}/META-INF" # Create "mimetype" file printf '%s' "application/vnd.oasis.opendocument.$(printf '%s' "${formatname}"|/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/tr A-Z a-z)" >"${TMPDIR}/mimetype" # Create "META-INF/manifest.xml" file { /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat < EOF [ "${format}" = "ods" ] && echo ' ' /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls "${TMPDIR}/Pictures/"*.{png,jpeg} 2>/dev/null | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/sort | \ /nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed 's/^.*\/Pictures\/\([^.]*[.]\)\(.*\)/ /' echo ''; } >"${TMPDIR}/META-INF/manifest.xml" # Create "content.xml" file if [ "${format}" = "odt" ]; then /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<'EOF' EOF /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls "${TMPDIR}/Pictures/"*.{png,jpeg} 2>/dev/null | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/sort | \ /nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed 's/^.*\/Pictures\/Image-\([0-9]*\)[.].*/ <\/style:style>/' /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<'EOF' EOF /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls "${TMPDIR}/Pictures/"*.{png,jpeg} 2>/dev/null | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/sort | \ /nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed 's/^.*\/Pictures\/Image-\([0-9]*\)[.].*/ /' /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<'EOF' EOF else if [ "${papersize}" = "letter" ]; then xcs="0.03125" # 1/32" ycs="0.03125" # 1/32" ucs="in" u2i="1" else xcs="0.75" ycs="0.75" ucs="mm" u2i="25.4" fi wcs="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "10k ${width} ${units2inch}/ ${xcs} ${u2i}/0k/p")" hcs="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "10k ${height} ${units2inch}/ ${ycs} ${u2i}/0k/p")" col="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "${wcs} [1 - d 26 % 65 + a r 26 / d 0 !=a] sa lax sa [n z 0 !=a]sa lax")" /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat < EOF /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls "${TMPDIR}/Pictures/"*.{png,jpeg} 2>/dev/null | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/sort | \ while read -r file; do idx="${file#${TMPDIR}/Pictures/Image-}"; idx="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "${idx%.*}p")" res="$(/nix/store/3vc3yi41531pnd21arjmkrr89z7ln8nq-imagemagick-7.1.0-20/bin/identify "${file}")" res="$(echo "x ${res#${file}}"|/nix/store/vr8p2is1j4n68gwr1hj4r061pf6462ng-gawk-5.1.1/bin/awk '{ print $3 }')" trim='/[.]/b1;q;:1;s/\([1-9]\)0*$/\1/;s/[.]0*$//' round=' 8k1/0.00000005+6k1/' iwu="$(/nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc -e "10k ${height} ${res#*x}/${res%x*}*ddsa ${width} dsb [lb]sc EOF done /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<'EOF' EOF fi >"${TMPDIR}/content.xml" # Create "styles.xml" file if [ "${format}" = "odt" ]; then /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<'EOF' EOF /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls "${TMPDIR}/Pictures/"*.{png,jpeg} 2>/dev/null | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/sort | \ /nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed 's/^.*\/Pictures\/Image-\([0-9]*\)[.]\(.*\)/ <\/style:page-layout-properties><\/style:page-layout>/' /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<'EOF' EOF /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls "${TMPDIR}/Pictures/"*.{png,jpeg} 2>/dev/null | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/sort | \ /nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed 's/^.*\/Pictures\/Image-\([0-9]*\)[.].*/ /' /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat <<'EOF' EOF else /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat < EOF fi >"${TMPDIR}/styles.xml" # Create "settings.xml" file if [ "${format}" = "ods" ]; then /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat >>"${TMPDIR}/settings.xml" <<'EOF' true EOF fi # Pack individual files into ODT/ODS archive [ "x${out}" = "x${out#/}" ] && out="${PWD}/${out}" || : /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/rm -f "${out}" cd "${TMPDIR}" && /nix/store/qziw36maxmgng6rvcrni0x4qylygbzyy-zip-3.0/bin/zip -m -q -X -0 "${out}" mimetype && /nix/store/qziw36maxmgng6rvcrni0x4qylygbzyy-zip-3.0/bin/zip -m -q -X "${out}" $(/nix/store/9amg9w74w2qch736hjbwgi35fhmzfvyp-findutils-4.8.0/bin/find . -name mimetype -o -type f -print) && cd .. # Clean up /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/rm -rf "${TMPDIR}" trap '' EXIT INT TERM QUIT HUP # Done exit 0 ### resholve directives (auto-generated) ## format_version: 2 # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/basename # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/cat # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/ls # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mkdir # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mktemp # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mv # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/rm # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/sort # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/tr # resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/wc # resholve: keep /nix/store/3vc3yi41531pnd21arjmkrr89z7ln8nq-imagemagick-7.1.0-20/bin/convert # resholve: keep /nix/store/3vc3yi41531pnd21arjmkrr89z7ln8nq-imagemagick-7.1.0-20/bin/identify # resholve: keep /nix/store/9amg9w74w2qch736hjbwgi35fhmzfvyp-findutils-4.8.0/bin/find # resholve: keep /nix/store/bd2i58qr7gsjxay3wvkr8ymrd2fq6yyq-gnused-4.8/bin/sed # resholve: keep /nix/store/p1n56cx455bcirirwmkkx22v990wnhms-bc-1.07.1/bin/dc # resholve: keep /nix/store/qpq3ylbybkq1dzbvksnwqmv3jr6wa20c-file-5.41/bin/file # resholve: keep /nix/store/qziw36maxmgng6rvcrni0x4qylygbzyy-zip-3.0/bin/zip # resholve: keep /nix/store/vr8p2is1j4n68gwr1hj4r061pf6462ng-gawk-5.1.1/bin/awk # resholve: keep /nix/store/w0k1ip6cizl1a8lf6sxvynf504xzdjw3-ghostscript-9.55.0/bin/gs