Reputation:
I cannot find a way to alter the code below to remove the whitespace from a SVG image. What should I modify or remove to achieve this? I am editing the code using this website: https://petercollingridge.appspot.com/svg-editor/
How the SVG image looks like with this code: See here.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events" width="250.0px" height="100.0px" overflow="visible" viewBox="0.0 0.0 250.0 100.0" enable-background="new 0.0 0.0 250.0 100.0">
<defs>
<radialGradient id="Grad1" gradientUnits="userSpaceOnUse" gradientTransform="matrix(0.0021972656 0.0 0.0 0.0040893555 120.8 38.55)" r="819.2" cx="0" cy="0">
</radialGradient>
<linearGradient id="Grad2" gradientUnits="userSpaceOnUse" gradientTransform="matrix(0.0 0.001739502 -0.015563965 0.0 108.05 40.25)" x1="-819.2" y1="0" x2="819.2" y2="0">
</linearGradient>
<radialGradient id="Grad3" gradientUnits="userSpaceOnUse" gradientTransform="matrix(-0.0021972656 0.0 0.0 0.0040893555 95.3 38.55)" r="819.2" cx="0" cy="0">
</radialGradient>
<linearGradient id="Grad4" gradientUnits="userSpaceOnUse" gradientTransform="matrix(0.0 -0.001953125 0.015563965 0.0 108.05 36.95)" x1="-819.2" y1="0" x2="819.2" y2="0">
</linearGradient>
</defs>
<g>
<g transform="matrix(1.0 0.0 0.0 1.0 0.0 0.0)">
<path fill="#DC0100" stroke-linecap="round" stroke-linejoin="round" d="M 232.05 57.75 L 230.45 60.6 L 221.85 68.25 L 221.5 67.9 C 221.2 67.6 220.84999 67.36666 220.45 67.2 L 221.9 61.85 L 223.5 62.4 L 226.05 57.25 L 227.2 57.85 L 229.95 55.95 L 232.05 57.75 Z M 227.85 82.2 L 226.65 83.0 L 226.65 86.35 L 224.2 87.15 L 222.5 84.45 L 219.6 84.75 L 219.0 84.7 L 219.25 80.35 L 219.35 80.35 C 221.61667 80.35 223.61667 79.75 225.35 78.55 L 227.85 82.2 Z M 212.3 82.8 C 211.23334 82.166664 210.25 81.416664 209.35 80.55 L 207.8 78.75 L 212.6 75.35 L 213.5 76.35 L 214.5 77.25 L 217.1 73.7 L 218.45 74.35 L 216.4 81.6 C 215.4 81.36667 214.46666 81.01667 213.6 80.55 L 212.3 82.8 Z M 205.15 70.3 L 201.75 68.9 L 202.25 65.6 L 205.85 65.6 C 206.45 63.8 207.4 62.15 208.7 60.65 L 207.25 58.15 L 209.1 56.2 L 211.7 58.1 L 213.75 61.2 C 213.11667 61.566666 212.53334 62.033333 212.0 62.6 C 210.43333 64.166664 209.45 65.96667 209.05 68.0 L 208.85 70.25 L 205.15 70.3 Z M 208.85 70.25 L 208.85 70.3 Z M 218.15 67.9 L 217.8 68.2 C 217.4 68.63333 217.18332 69.15 217.15 69.75 L 211.1 68.45 C 211.43333 66.816666 212.23334 65.36667 213.5 64.1 L 214.9 62.95 L 218.15 67.9 Z M 232.15 70.2 C 232.15 72.13333 231.76666 73.916664 231.0 75.55 L 223.55 72.0 C 223.78333 71.46667 223.9 70.88333 223.9 70.25 L 223.85 69.7 L 232.1 68.65 L 232.15 70.2 Z"/>
<path fill="#6C0B0B" stroke-linecap="round" stroke-linejoin="round" d="M 226.05 57.25 L 223.5 62.4 L 221.9 61.85 L 220.45 67.2 C 220.85 67.36666 221.20001 67.6 221.5 67.9 L 221.85 68.25 L 230.45 60.6 C 231.85 62.166668 232.85 63.9 233.45 65.8 L 233.45 65.9 L 235.95 65.9 L 236.4 66.15 L 236.9 68.4 C 235.96666 68.86667 235.01666 69.35 234.05 69.85 L 234.05 69.95 L 234.05 70.3 C 234.05 72.23333 233.73334 74.01666 233.1 75.65 L 235.3 78.35 L 233.85 80.2 L 230.95 79.35 L 229.85 80.55 L 228.05 82.1 L 227.85 82.2 L 225.35 78.55 C 223.61667 79.75 221.61667 80.35 219.35 80.35 L 219.25 80.35 L 219.0 84.7 L 218.75 84.7 L 216.65 84.45 L 214.7 87.15 L 212.3 86.35 L 212.3 82.8 L 213.6 80.55 C 214.46666 81.01667 215.4 81.36667 216.4 81.6 L 218.45 74.35 L 217.1 73.7 L 214.5 77.25 L 213.5 76.35 L 212.6 75.35 L 207.8 78.75 L 205.55 80.35 L 203.85 78.2 L 206.05 75.5 C 205.45 73.9 205.15 72.16667 205.15 70.3 L 208.85 70.3 L 209.05 68.0 C 209.45 65.96667 210.43333 64.166664 212.0 62.6 C 212.53334 62.033333 213.11667 61.566666 213.75 61.2 L 211.7 58.1 C 213.43333 56.93333 215.35 56.216663 217.45 55.95 L 218.3 52.9 L 220.9 52.9 L 221.7 55.95 C 223.23334 56.11667 224.68333 56.550003 226.05 57.25 Z M 219.0 84.7 L 219.0 84.75 Z M 208.85 70.3 L 208.85 70.25 Z M 217.8 68.2 L 218.15 67.9 L 214.9 62.95 L 213.5 64.1 C 212.23334 65.36667 211.43333 66.816666 211.1 68.45 L 217.15 69.75 C 217.18333 69.15 217.40001 68.63333 217.8 68.2 Z M 231.0 75.55 C 231.76666 73.916664 232.15 72.13333 232.15 70.2 L 232.1 68.65 L 223.85 69.7 L 223.9 70.25 C 223.9 70.88333 223.78333 71.46667 223.55 72.0 L 231.0 75.55 Z"/>
</g>
</g>
</svg>
Upvotes: 37
Views: 75695
Reputation: 157
You can paste your SVG code to the HTML input in this website, then replace the viewBox
in your SVG code by the viewBox
generated by the website. The whitespace in your SVG image will be trimmed perfectly.
Upvotes: 11
Reputation: 807
Someone created a jsfiddle with a script that trims the svg for you which you can find here.
Here's the script if you need to use it in your project:
var svg = document.getElementsByTagName("svg")[0];
var bbox = svg.getBBox();
var viewBox = [bbox.x, bbox.y, bbox.width, bbox.height].join(" ");
svg.setAttribute("viewBox", viewBox);
prompt("Copy to clipboard: Ctrl+C, Enter", svg.outerHTML);
Additionally, @user2415116 linked another website that achieves similar results: svgcrop.com
Upvotes: 66
Reputation: 71
You can also use Inkscape in command line interface mode (CLI). Below is the command...
inkscape --export-plain-svg --export-filename="your path to the edited svg" --export-area-drawing "path to the svg with whitspace"
Use this in conjunction with a shell script if you have many SVGs.
Upvotes: 7
Reputation: 860
Auto converter: paste SVG in first textarea and get from second...
Note: Run script in full page
document.getElementById("p1").addEventListener('paste', function() {
var that = this;
setTimeout(function() {
var x = document.createElement("div");
x.innerHTML = that.value;
document.body.appendChild(x);
setTimeout(function() {
var svg = document.getElementsByTagName("svg")[0];
var bbox = svg.getBBox();
var viewBox = [bbox.x, bbox.y, bbox.width, bbox.height].join(" ");
svg.setAttribute("viewBox", viewBox);
document.getElementById("p2").value = svg.outerHTML;
svg.outerHTML = '';
}, 1)
}, 1);
}, false);
<html>
<body>
<textarea id="p1" style="width: 800px; height:400px;"></textarea>
<hr>
<textarea id="p2" style="width: 800px; height:400px;"></textarea>
</body>
</html>
Upvotes: 10
Reputation: 124324
Adjust the viewBox so that it just covers the area you want. I've done most of it below, you could tighten if further if you need to.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events" width="250.0px" height="100.0px" overflow="visible" viewBox="200.0 50.0 8.0 8.0" enable-background="new 0.0 0.0 250.0 100.0">
<defs>
<radialGradient id="Grad1" gradientUnits="userSpaceOnUse" gradientTransform="matrix(0.0021972656 0.0 0.0 0.0040893555 120.8 38.55)" r="819.2" cx="0" cy="0">
</radialGradient>
<linearGradient id="Grad2" gradientUnits="userSpaceOnUse" gradientTransform="matrix(0.0 0.001739502 -0.015563965 0.0 108.05 40.25)" x1="-819.2" y1="0" x2="819.2" y2="0">
</linearGradient>
<radialGradient id="Grad3" gradientUnits="userSpaceOnUse" gradientTransform="matrix(-0.0021972656 0.0 0.0 0.0040893555 95.3 38.55)" r="819.2" cx="0" cy="0">
</radialGradient>
<linearGradient id="Grad4" gradientUnits="userSpaceOnUse" gradientTransform="matrix(0.0 -0.001953125 0.015563965 0.0 108.05 36.95)" x1="-819.2" y1="0" x2="819.2" y2="0">
</linearGradient>
</defs>
<g>
<g transform="matrix(1.0 0.0 0.0 1.0 0.0 0.0)">
<path fill="#DC0100" stroke-linecap="round" stroke-linejoin="round" d="M 232.05 57.75 L 230.45 60.6 L 221.85 68.25 L 221.5 67.9 C 221.2 67.6 220.84999 67.36666 220.45 67.2 L 221.9 61.85 L 223.5 62.4 L 226.05 57.25 L 227.2 57.85 L 229.95 55.95 L 232.05 57.75 Z M 227.85 82.2 L 226.65 83.0 L 226.65 86.35 L 224.2 87.15 L 222.5 84.45 L 219.6 84.75 L 219.0 84.7 L 219.25 80.35 L 219.35 80.35 C 221.61667 80.35 223.61667 79.75 225.35 78.55 L 227.85 82.2 Z M 212.3 82.8 C 211.23334 82.166664 210.25 81.416664 209.35 80.55 L 207.8 78.75 L 212.6 75.35 L 213.5 76.35 L 214.5 77.25 L 217.1 73.7 L 218.45 74.35 L 216.4 81.6 C 215.4 81.36667 214.46666 81.01667 213.6 80.55 L 212.3 82.8 Z M 205.15 70.3 L 201.75 68.9 L 202.25 65.6 L 205.85 65.6 C 206.45 63.8 207.4 62.15 208.7 60.65 L 207.25 58.15 L 209.1 56.2 L 211.7 58.1 L 213.75 61.2 C 213.11667 61.566666 212.53334 62.033333 212.0 62.6 C 210.43333 64.166664 209.45 65.96667 209.05 68.0 L 208.85 70.25 L 205.15 70.3 Z M 208.85 70.25 L 208.85 70.3 Z M 218.15 67.9 L 217.8 68.2 C 217.4 68.63333 217.18332 69.15 217.15 69.75 L 211.1 68.45 C 211.43333 66.816666 212.23334 65.36667 213.5 64.1 L 214.9 62.95 L 218.15 67.9 Z M 232.15 70.2 C 232.15 72.13333 231.76666 73.916664 231.0 75.55 L 223.55 72.0 C 223.78333 71.46667 223.9 70.88333 223.9 70.25 L 223.85 69.7 L 232.1 68.65 L 232.15 70.2 Z"/>
<path fill="#6C0B0B" stroke-linecap="round" stroke-linejoin="round" d="M 226.05 57.25 L 223.5 62.4 L 221.9 61.85 L 220.45 67.2 C 220.85 67.36666 221.20001 67.6 221.5 67.9 L 221.85 68.25 L 230.45 60.6 C 231.85 62.166668 232.85 63.9 233.45 65.8 L 233.45 65.9 L 235.95 65.9 L 236.4 66.15 L 236.9 68.4 C 235.96666 68.86667 235.01666 69.35 234.05 69.85 L 234.05 69.95 L 234.05 70.3 C 234.05 72.23333 233.73334 74.01666 233.1 75.65 L 235.3 78.35 L 233.85 80.2 L 230.95 79.35 L 229.85 80.55 L 228.05 82.1 L 227.85 82.2 L 225.35 78.55 C 223.61667 79.75 221.61667 80.35 219.35 80.35 L 219.25 80.35 L 219.0 84.7 L 218.75 84.7 L 216.65 84.45 L 214.7 87.15 L 212.3 86.35 L 212.3 82.8 L 213.6 80.55 C 214.46666 81.01667 215.4 81.36667 216.4 81.6 L 218.45 74.35 L 217.1 73.7 L 214.5 77.25 L 213.5 76.35 L 212.6 75.35 L 207.8 78.75 L 205.55 80.35 L 203.85 78.2 L 206.05 75.5 C 205.45 73.9 205.15 72.16667 205.15 70.3 L 208.85 70.3 L 209.05 68.0 C 209.45 65.96667 210.43333 64.166664 212.0 62.6 C 212.53334 62.033333 213.11667 61.566666 213.75 61.2 L 211.7 58.1 C 213.43333 56.93333 215.35 56.216663 217.45 55.95 L 218.3 52.9 L 220.9 52.9 L 221.7 55.95 C 223.23334 56.11667 224.68333 56.550003 226.05 57.25 Z M 219.0 84.7 L 219.0 84.75 Z M 208.85 70.3 L 208.85 70.25 Z M 217.8 68.2 L 218.15 67.9 L 214.9 62.95 L 213.5 64.1 C 212.23334 65.36667 211.43333 66.816666 211.1 68.45 L 217.15 69.75 C 217.18333 69.15 217.40001 68.63333 217.8 68.2 Z M 231.0 75.55 C 231.76666 73.916664 232.15 72.13333 232.15 70.2 L 232.1 68.65 L 223.85 69.7 L 223.9 70.25 C 223.9 70.88333 223.78333 71.46667 223.55 72.0 L 231.0 75.55 Z"/>
</g>
</g>
</svg>
Upvotes: 15