From 6fe7de802014d0c967e3b839ccda7711b1028029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Mon, 14 Apr 2025 20:25:18 +0100 Subject: [PATCH] fix: Add DOCTYPE and XML preamble in exported SVG documents (#9386) * Add DOCTYPE and XML preamble in exported SVG documents * Update packages/excalidraw/data/index.ts --------- Co-authored-by: David Luzar <5153846+dwelle@users.noreply.github.com> --- packages/common/src/constants.ts | 3 +++ packages/excalidraw/data/index.ts | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/common/src/constants.ts b/packages/common/src/constants.ts index 7eb36d5d9..cd3bd7a15 100644 --- a/packages/common/src/constants.ts +++ b/packages/common/src/constants.ts @@ -319,6 +319,9 @@ export const DEFAULT_MAX_IMAGE_WIDTH_OR_HEIGHT = 1440; export const MAX_ALLOWED_FILE_BYTES = 4 * 1024 * 1024; export const SVG_NS = "http://www.w3.org/2000/svg"; +export const SVG_DOCUMENT_PREAMBLE = ` + +`; export const ENCRYPTION_KEY_BITS = 128; diff --git a/packages/excalidraw/data/index.ts b/packages/excalidraw/data/index.ts index ac8147e85..93d5f5677 100644 --- a/packages/excalidraw/data/index.ts +++ b/packages/excalidraw/data/index.ts @@ -5,6 +5,7 @@ import { isFirefox, MIME_TYPES, cloneJSON, + SVG_DOCUMENT_PREAMBLE, } from "@excalidraw/common"; import { getNonDeletedElements } from "@excalidraw/element"; @@ -134,7 +135,11 @@ export const exportCanvas = async ( if (type === "svg") { return fileSave( svgPromise.then((svg) => { - return new Blob([svg.outerHTML], { type: MIME_TYPES.svg }); + // adding SVG preamble so that older software parse the SVG file + // properly + return new Blob([SVG_DOCUMENT_PREAMBLE + svg.outerHTML], { + type: MIME_TYPES.svg, + }); }), { description: "Export to SVG",