0

I have to encode svg images to base64 strings (for use in jsPDF). I found this question which povides a way to do it using an svg element that's already in the DOM. But my svg images are in a folder on my server, and should not be displayed in the DOM.

Is there a way I can use this syntax

var s = new XMLSerializer().serializeToString(something)

with an external image file (say, 'images/picture.svg') ?

GuitarExtended
  • 787
  • 2
  • 11
  • 32
  • 1
    Please read this post on codepen: very useful advices about how to [Optimize SVGs in data URIs](https://codepen.io/tigt/post/optimizing-svgs-in-data-uris). Also if you need to encode only one SVG image you may want to use this [URL-encoder for SVG](https://codepen.io/yoksel/pen/JDqvs) – enxaneta Feb 01 '19 at 12:45

1 Answers1

1

You could use JQuery to do that for example:

$.get("/my/nice/url/image.svg", function(data) {
  var s = new XMLSerializer().serializeToString(data.documentElement)
  console.log(s)
  ...
});
Silvio Biasiol
  • 856
  • 8
  • 14
  • ... and then encode as suggested in this question https://stackoverflow.com/questions/28450471/convert-inline-svg-to-base64-string – GuitarExtended Feb 01 '19 at 13:54