Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save NorfeldtKnowit/33f955e3dbe29eb58a6897d1943e897c to your computer and use it in GitHub Desktop.

Select an option

Save NorfeldtKnowit/33f955e3dbe29eb58a6897d1943e897c to your computer and use it in GitHub Desktop.
Export Drawio to PDF files
#!/bin/zsh
# Ensure the previous_exports directory exists
mkdir -p previous_exports
for drawioFile in *.drawio.svg; do
# Extracting the "content" attribute value
content=$(sed -n 's/.*content="\([^"]*\)".*/\1/p' "$drawioFile")
# Calculating the MD5 hash of the "content" and taking the first 5 characters
hash=$(echo -n "$content" | md5 | cut -c 1-5)
baseName="${drawioFile%.drawio.svg}"
outputName="${baseName}_$hash.pdf"
outputFilePath="./$outputName"
# Ensure globbing does not split filenames with spaces
OLDIFS=$IFS
IFS=$'\n'
# Check if the PDF with the current hash already exists to avoid unnecessary moves
if [[ -f "$outputFilePath" ]]; then
echo "PDF $outputName already exists with the same content. Skipping export and any moves."
continue
fi
# Find any existing PDFs that start with the base name but aren't the current hash version
existingPdfs=($(ls "${baseName}"_*.pdf 2> /dev/null))
for existingPdf in "${existingPdfs[@]}"; do
if [[ "$existingPdf" != "$outputFilePath" ]]; then
# Move the existing PDF with a different hash to the previous_exports directory
echo "Moving existing PDF $existingPdf to previous_exports directory."
mv "$existingPdf" "./previous_exports/"
fi
done
# Reset IFS to its original value
IFS=$OLDIFS
# Export the new PDF
/Applications/draw.io.app/Contents/MacOS/draw.io --export --format pdf --output "$outputName" "$drawioFile"
echo "Exported new PDF with hash $hash: $outputName"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment