Experiencing issues with exporting <SVG> file due to corruption

I received some downvotes on my previous post, and I'm not sure why. My question was clear, and I provided a lot of information. Let's give it another shot.

My issue is with exporting <SVG> files from an HTML document. When I try to open them in Illustrator or Inkscape, I get an error saying the file is corrupted.

The code snippet for reference:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="svg-converter.js"></script>
<script>
jQuery(document).ready(function($) {
    $('.svg-convert').svgConvert();
});
</script>
... (Code continues)

I am using SVG Convert tool to convert .SVG files to <svg>, which works fine without any issues.

The conversion process successfully converts 2 .svg images to 2 <svg> elements. Here is a working example:

However, when I try to export these converted .SVG files and open them in Illustrator or Inkscape, I encounter a "file corrupted" message.

The exported .SVG code looks like this:

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<div xmlns="http://www.w3.org/1999/xhtml" id="mySVG">
   <defs xmlns="http://www.w3.org/2000/svg"/>
   <img src="https://cdn.kastatic.org/images/avatars/svg/leafers-sapling.svg" class="svg-convert" />
   <img src="https://cdn.kastatic.org/images/avatars/svg/aqualine-sapling.svg" class="svg-convert" />
 </div>

I need assistance in properly exporting all the <SVG> elements within the HTML page into a single .SVG file that can be opened and edited in vector programs like Illustrator or Inkscape.

Update: I have made changes to the code to make the .SVG file compatible with Inkscape. However, it only exports one SVG element instead of both:

<!DOCTYPE html>
... (Updated code snippet continues)

Answer №1

the svg data provided is not valid, it should be formatted as follows

<svg xmlns="http://www.w3.org/2000/svg" viewBox=..
<path fill="#7AB344" d="M554.457 388.243c....

remember to invoke the exportSVG() function within the onComplete method

$('.svg-convert').svgConvert({
  onComplete: function() {
    exportSVG(document.getElementById('mySVG'));
  }
});

also, make sure to eliminate the following line in order to ensure compatibility with Inkscape

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<div xmlns="http://www.w3.org/1999/xhtml" id="mySVG">
    <defs xmlns="http://www.w3.org/2000/svg"/>

Answer №2

It is advisable to aim for something similar to the example below.

<svg xmlns="http://www.w3.org/3000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

  <svg viewBox="0 0 612 612"><path fill="#7AB344" d="M554.457 388.243c-26.453 31.338-50.593 39.842-90.15 42.498-2.355.159-4.596.388-6.747.669-15.848 2.041-26.179 7.242-32.521 14.737-.201.232-.396.464-.586.703-.18.226-.352.45-.531.684-10.825 14.556-.352 33.054-16.72 39.236 14.962 2.893 28.397 4.335 40.49 4.593 114.973 2.438 106.765-103.12 106.765-103.12z"></path><path fill="#85C55A" d="M464.307 430.741c-2.355.159-4.596.388-6.747.669a57.783 57.783 0 0 0-3.715 4.898 56.238 56.238 0 0 0-6.694 13.487c7.979-3.927 18.332-6.749 31.834-7.329 43.512-1.873 65.338-31.285 75.473-54.224-26.454 31.339-50.594 39.843-90.151 42.499z"></path><path fill="#6EA041" d="M554.457 388.243c-10.135 22.938-31.961 52.351-75.473 54.224-13.502....

For optimal results, you may need to make further adjustments such as:

  1. Include a viewBox in the main <svg> element.
  2. Optionally specify a width and height for the root <svg> element.
  3. Position and size the inner <svg> elements using attributes like x, y, width, and height.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error encountered while adding x-ray-scraper to project using Npm

I am currently working on a Vue application and utilizing the x-ray-scraper library. However, when I attempt to run npm run serve in the terminal to preview the application locally, I encounter the following error: This dependency was not found: * _http_c ...

Using a function as a parameter in a React component constructor

I have a question about a snake game I'm developing using React. After reading a post on Stack Overflow discussing setting functions inside the constructor, I am facing an issue with calling a function (foodGenerator) from within another function (sn ...

What is the proper way to ensure a pull right display appears correctly?

This is the code I am currently using .tag-container.bottom-border.col-middle h1.text-center {{ tag.name }} button.btn.btn-follow.pull-right(ng-hide="hasFollowed" ng-click="tagArticles.followTag()") Follow I am trying to align the tag name in th ...

Is it considered improper to include non-input HTML elements within a form tag in Angular reactive forms?

When working with an angular reactive form to manage html inputs, is it acceptable to include non-input html elements within the form tags? NOTE: The previous example has been removed. A new one will be provided soon. ...

Modern UI Styling for ASP.NET MVC with Metro Design

I am interested in incorporating the METRO UI CSS into my application: Begin by creating an ASP NET MVC Project To download METRO UI MVC, follow these steps: Package Manager Console: PM> Install-Package Metro.UI.CSS Adjust the bundles to incl ...

I'm curious if there is an eslint rule specifically designed to identify and flag any unnecessary spaces between a block comment and the function or

Can anyone help me find a solution to prevent the following issue: /** * This is a comment */ function foo() { ... } I need it to be corrected and formatted like this: /** * This is a comment */ function foo() { ... } ...

Retrieve the visible text content of an element by utilizing various ids

I am currently working on a project using AngularJS with multiple conditions all sharing the same id. My goal is to extract text only from the condition that evaluates to true. Recently, I discovered a major bug in an app that I am preparing for release. ...

Booting up a module in AngularJS without using automatic bootstrapping

Here is the structure of my HTML... <body id="main"> {{pageName}} </body> This is how I implement it in JavaScript: angular.module('myApp',[]) .controller('myController', function($scope){ console.log('initial ...

Using jQuery to fetch and display content from an external webpage

Is there a more effective method for loading an external web page on the same server? I've experimented with .load() and .get(), but they only load the page after the PHP script is finished. I've also used an iFrame, which displays the informatio ...

JavaScript and CSOM updates cause SharePoint list item properties to disappear

Within my SharePoint environment, there exists a website where users can load a single list item based on their selection using JavaScript and CSOM. This particular list item contains approximately 60 properties defined in its list definition. Users have ...

Tips for invoking a reduce mechanism within a separate function

I need assistance with implementing a reduce function: users.reduce(function (acc, obj) { return acc + obj.age/3; }, 0); within the following function structure: function calculateUserAverageAge(users) {}; To analyze this array of objects and calculate ...

I am working with two dropdown menus and I need to search the database for data that matches both selections. Unfortunately, the code I have written does not seem to be functioning correctly

I am facing an issue with a dropdown filter in my PHP code. I have 3 pages and the dropdown is working for one filter, but I am unable to make it work for both filters simultaneously. I suspect it's a syntax error since I am new to PHP. I have tried v ...

Ending Current Tab using angularJS Controller

Is there a way to automatically close the current browser tab after a successful action in AngularJS? ...

Embed Vue applications within the container of the main Vue application

My goal is to establish a foundational Vue application that offers essential features such as signing in, navigating with a sidebar, and the flexibility to interchange navbar items. I envision creating separate Vue applications for each navbar item. Main ...

I am having trouble embedding YouTube videos with my code

Need a fresh pair of eyes on this code. Everything looks correct to me, but it's not functioning as expected. The entries are results from a search. function displayVideos(data) { var feed = data.feed; var entries = feed.entry || []; va ...

Comparing HTML5 Drag and Drop to jQuery UI Drag and Drop: What's the Difference

In the ever-evolving world of web development, what is the most effective method for creating a drag and drop builder in 2017? While this question has been posed in the past (as seen here), technology changes rapidly. Is HTML5 still the go-to option, or ha ...

The absence of reactivity is evident when working with composition-api and nuxt3

I've got a Nuxt code snippet that is functioning correctly: <template lang="pug"> div {{ isActive }} !-- Reactivity is working fine, isActive switches from false to true --! </template> <script> export default { data() ...

SailsJS failing to detect changes in local JSON file

https://i.stack.imgur.com/RG13Y.png This sailsjs application does not utilize any database. Instead, it relies on multiple JSON files located in the data folder within the root directory. The controller accesses this data as follows: req.session.categori ...

Issue with create-react-app and express server not displaying correctly in Internet Explorer

My application functions perfectly with Chrome and Safari. It utilizes React for the front-end and Express for the back-end. However, when I try to open it in Internet Explorer, all I see is a blank white page. Additionally, I encounter this error message: ...

What are some ways to postpone the execution of a function in React or NextJs?

How do I automatically redirect to the home page after 5 seconds of accessing a page in NextJS? I attempted to achieve this using the following code: useEffect(() => { const timer = setTimeout(() => { redirect('/home') ...