# Save off an SVG with images

**URL:** <https://talk.observablehq.com/t/save-off-an-svg-with-images/7025>\
**Category:** Help\
**Created:** [September 12, 2022, 5:16am UTC](https://talk.observablehq.com/t/save-off-an-svg-with-images/7025 "2022-09-12T05:16:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zachbogart](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/zachbogart/32/8597_2.png) [@zachbogart](https://talk.observablehq.com/u/zachbogart)\
**Post date:** [September 12, 2022, 5:16am UTC](https://talk.observablehq.com/t/save-off-an-svg-with-images/7025/1 "2022-09-12T05:16:29Z")

</div>

I want to make an SVG that includes another SVG icon I have as an `<image>` so I can save off the colored result. But, when I try to export it, I only see the rectangle, not the image. Is there a way I can make an SVG that contains an `<image>`, and export the results?

[Notebook](https://observablehq.com/d/1bfaa6a43c80f9fc)

Thanks. Always learning,  
Zach

---

<div class="post-metadata">

**Author:** ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)\
**Post date:** [September 12, 2022, 5:48am UTC](https://talk.observablehq.com/t/save-off-an-svg-with-images/7025/2 "2022-09-12T05:48:50Z")

</div>

A quick (and dirty) solution is to pass the icon’s code as a data url:

```auto
testExport = html`<svg width=400 height=400>
  <rect width=400 height=400 fill="${color}"></rect>
  <image 
    class="fill-mainImg"
    x="0" 
    y="0" 
    width="${mainImg.width * 0.3}" 
    height="${mainImg.height * 0.3}" 
    href="data:image/svg+xml;utf8,${await FileAttachment("mainImg.svg").text().then(d => d.replace(/^.*?<svg /, "<svg ").replace(/"/g, '&quot;'))}" 
  />
</svg>
`

```

I had to remove some elements at the beginning of the file (the xml declaration etc), and convert any " to &quot;. ymmv

---

<div class="post-metadata">

**Author:** ![zachbogart](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/zachbogart/32/8597_2.png) [@zachbogart](https://talk.observablehq.com/u/zachbogart)\
**Post date:** [September 12, 2022, 6:21am UTC](https://talk.observablehq.com/t/save-off-an-svg-with-images/7025/3 "2022-09-12T06:21:25Z")

</div>

Interesting. So you can use `.text()` with a file attachment of an SVG to get at some of the codestuffs. I’d have to say I don’t understand the syntax of the href you are making, but can confirm this works nicely. Any insight into how this works and why this is different than referencing the src from the image() version?

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [September 12, 2022, 6:52am UTC](https://talk.observablehq.com/t/save-off-an-svg-with-images/7025/4 "2022-09-12T06:52:50Z")

</div>

@zachbogart You can read more about data URLs here:

> **[Data URLs - HTTP | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs)**
>
> Data URLs, URLs prefixed with the data: scheme, allow content creators to embed small files inline in documents. They were formerly known as "data URIs" until that name was retired by the WHATWG.

---

<div class="post-metadata">

**Author:** ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)\
**Post date:** [September 12, 2022, 7:13am UTC](https://talk.observablehq.com/t/save-off-an-svg-with-images/7025/5 "2022-09-12T07:13:35Z")

</div>

The crucial difference in this case is that image.url is just a pointer to a remote resource (which stays on observable’s servers), at the address static.observableusercontent.com/files/004d5688321…  
whereas the data url contains the actual data (contents) of the image, without having to look it up on the net.

---

<div class="post-metadata">

**Author:** ![zachbogart](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/zachbogart/32/8597_2.png) [@zachbogart](https://talk.observablehq.com/u/zachbogart)\
**Post date:** [September 12, 2022, 7:47am UTC](https://talk.observablehq.com/t/save-off-an-svg-with-images/7025/6 "2022-09-12T07:47:22Z")

</div>

Excellent, thanks!
