Skip to content

Instantly share code, notes, and snippets.

@EBoisseauSierra
Last active July 28, 2023 13:22
Show Gist options
  • Save EBoisseauSierra/4944c7e8286b4c321f590fcdebc840c8 to your computer and use it in GitHub Desktop.
Save EBoisseauSierra/4944c7e8286b4c321f590fcdebc840c8 to your computer and use it in GitHub Desktop.
Script to generate a PDF in which each page is a layer of a SVG
#!/bin/bash
# Parse CLI argument
INPUT_FILE=$1 # SVG file to export
TEMP_SVG_FILE="temp_$INPUT_FILE" # temporary SVG file, with all layers visible
OUTPUT_FILE="${INPUT_FILE%.*}.pdf" # output filename is the same as input — just changing .svg into .pdf
# Ensure each layer is visible
inkscape --actions=unhide-all --export-filename=$TEMP_SVG_FILE $INPUT_FILE
# Export one PDF per layer
inkscape --query-all $TEMP_SVG_FILE | # get the list of object in the SVG file
grep layer | # keep those that begins with 'layer' only
awk -F "," "{print \$1}" | # extract the ID from each layer
awk -v TEMP_SVG_FILE="$TEMP_SVG_FILE" '{print "inkscape --export-area-page --export-id " $1 " --export-id-only --export-filename temp_pdf_" sprintf("%03d", NR) ".pdf " TEMP_SVG_FILE }' | # for each layer print the command to print this layer only
xargs -0 bash -c # Execute the previously printed commands
# Concatenate the generated one-page PDF into a single document
pdfunite temp_pdf_* $OUTPUT_FILE
# Delete tpmorary files
rm $TEMP_SVG_FILE
rm temp_pdf_*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment