I have written a script for converting SVG to PNG which is working pretty well, until there is content in the SVG that is “off the page”.
eg:

The black line will be converted fine, but the green box is completely ignored. Some SVG files have the content completely off the page and renders an empty PNG file.
My current command:
convert -background none "$file" -trim -geometry $size "$target"
I have 2k + SVG files I am converting with at least around 500 or more with this issue so manually moving the image onto the page is not an option really.
Edit
Here is a link to an example file. The actual drawing is in line with the bottom of the page to the left hand side.
So I finally got this working. Could not seem to do it with ImageMagick directly so ended up manipulating the actual SVG data.
First thing is to find the bounds of the object. Using PHP and simpleXml I converted the SVG data to an array for easy manipulation / traversing.
It seems like all the coordinates are in the form of x y so this breaks the coordinates up into numbers and switches between x / y recording if its higher/lower than the previous values.
Once you got the min/max bounds I found a property
transformwithtranslatewhich moves an object by the x,y specified. So just take0 - $minand it will be moved to0as it could be0 - -10or0 - 10All my files have with no attributes so I just done a simple replace of
<g>with<g transform="translate($widthOffset, $heightOffset)">Also set the viewbox size:
Last thing to do is just convert the array back to XML and save the new data back into the file.