Skip to content

Instantly share code, notes, and snippets.

@guillermo
Forked from s417-lama/svg2pdf.bash
Created December 1, 2020 16:57
Show Gist options
  • Select an option

  • Save guillermo/3258662554c6afa2128492ca9a1a116c to your computer and use it in GitHub Desktop.

Select an option

Save guillermo/3258662554c6afa2128492ca9a1a116c to your computer and use it in GitHub Desktop.

Revisions

  1. guillermo revised this gist Dec 1, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion svg2pdf.bash
    Original file line number Diff line number Diff line change
    @@ -42,4 +42,4 @@ tmpfile=$(mktemp XXXXXX.html)
    trap "rm -f $tmpfile" EXIT
    echo $HTML > $tmpfile

    google-chrome --headless --disable-gpu --print-to-pdf=$OUTPUT $tmpfile
    google-chrome --headless --disable-gpu --print-to-pdf="$OUTPUT" $tmpfile
  2. @s417-lama s417-lama created this gist Apr 29, 2020.
    45 changes: 45 additions & 0 deletions svg2pdf.bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #!/bin/bash
    #
    # Convert an SVG file to a PDF file by using headless Chrome.
    #

    if [ $# -ne 2 ]; then
    echo "Usage: ./svg2pdf.bash input.svg output.pdf" 1>&2
    exit 1
    fi

    INPUT=$1
    OUTPUT=$2

    HTML="
    <html>
    <head>
    <style>
    body {
    margin: 0;
    }
    </style>
    <script>
    function init() {
    const element = document.getElementById('targetsvg');
    const positionInfo = element.getBoundingClientRect();
    const height = positionInfo.height;
    const width = positionInfo.width;
    const style = document.createElement('style');
    style.innerHTML = \`@page {margin: 0; size: \${width}px \${height}px}\`;
    document.head.appendChild(style);
    }
    window.onload = init;
    </script>
    </head>
    <body>
    <img id=\"targetsvg\" src=\"${INPUT}\">
    </body>
    </html>
    "

    tmpfile=$(mktemp XXXXXX.html)
    trap "rm -f $tmpfile" EXIT
    echo $HTML > $tmpfile

    google-chrome --headless --disable-gpu --print-to-pdf=$OUTPUT $tmpfile