I\'ve got an SVG diagram (complicated a little) that is generated using php and some data from the database. In that case, I can\'t predict the length of labels on the svg and a
Building off this answer and the above comments:
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);
If you really need a non-JS solution: convert to PNG (or any other lossless format), then use trim to lop off the whitespace. You can do this from within PHP.