Sign inSign up

gerph/pdf2svg

By gerph

Updated 11 months ago

Convert PDFs to SVGs

Image
Developer tools
0

273

gerph/pdf2svg repository overview

PDF2SVG

Based on https://github.com/douglasmiranda/dockerfiles which contains a docker build of https://github.com/dawbarton/pdf2svg.

To run, mount the files you want to convert and then supply their filenames as arguments.

docker run --rm -u 502:20 -v $PWD/INPUT.pdf:/input/INPUT.pdf -v $PWD/OUTPUT.svg:/output/OUTPUT.svg gerph/pdf2svg:0.2.4 /input/INPUT.pdf /output/OUTPUT.svg

I use a wrapper script which handles the mapping...

#!/bin/bash
##
# Convert a PDF into an SVG.
#
# Syntax: pdf2svg <args>
#

set -e


##
# Remember the output file we're going to use.
function set_output() {
    local arg="$1"
    local remotename="$(basename "$arg")"

    if [[ ! -e "$arg" ]] ; then
        touch "$arg"
    fi
    output_host="$arg"
    dockerargs+=(-v "$(realpath "$arg"):/output/$remotename")
    output="/output/$remotename"
}


##
# Remember the input file we're going to use.
function set_input() {
    local arg="$1"
    local remotename="$(basename "$arg")"

    # They're setting the input
    if [[ ! -f "$arg" ]] ; then
        echo "File '$arg' does not exist" >&2
        exit 1
    fi

    remotename="$(basename "$arg")"
    dockerargs+=(-v "$(realpath "$arg"):/input/$remotename")
    input="/input/$remotename"
}


tempfile=
function cleanup() {
    if [[ "$tempfile" != '' && -f "$tempfile" ]] ; then
        rm "$tempfile"
    fi
}
trap cleanup EXIT

docker_image=gerph/pdf2svg

pdf2svg_version=0.2.4
args=()
dockerargs=()
input=
output_host=
output=
page=
for arg in "$@" ; do
    if [[ "${arg:0:1}" == '-' ]] ; then
        switch=
        if [[ "$arg" == '--pull' ]] ; then
            docker pull "$docker_image:$pdf2svg_version"
        else
            if [[ "$arg" == '-i' || "$arg" == '--input' || "$arg" == '--pdf' ]] ; then
                switch=input

            elif [[ "$arg" == '-o' || "$arg" == '--output' || "$arg" == '--svg' ]] ; then
                switch=output

            else
                args+=("$arg")
            fi
        fi

    elif [[ "$switch" == '' ]] ; then
        # This is a filename to transform into the container
        if [[ "$input" == '' ]] ; then
            set_input "$arg"

        elif [[ "$output" == '' ]] ; then
            set_output "$arg"

        elif [[ "$page" == '' ]] ; then
            # They want to set the page
            page=$arg

        else
            echo "Input and page number can only be supplied once" >&2
            exit 1
        fi

    elif [[ "$switch" == 'output' ]] ; then
        set_output "$arg"

    elif [[ "$switch" == 'input' ]] ; then
        set_input "$arg"

    else
        # This is a parameter value

        args+=("${arg}")
    fi
done

if [[ "$output" == '' ]] ; then
    args=()
else
    args+=("$input" "$output")
    if [[ "$page" != '' ]] ; then
        args+="$page"
    fi
fi

set +e

docker run --rm -u "$(id -u):$(id -g)" "${dockerargs[@]}" "$docker_image:$pdf2svg_version" "${args[@]}"
rc=$?

if [[ "$rc" != 0 ]] ; then
    if [[ "$output_host" != '' ]] ; then
        rm "$output_host"
    fi
fi
exit $rc

Tag summary

Content type

Image

Digest

sha256:7bfa432c7

Size

172.6 MB

Last updated

11 months ago

docker pull gerph/pdf2svg