Cropping SVG to range in Inkscape?

1.2k views Asked by At

Say I have a range – something like a 400x400 rectangle at 60, 60 – which is dynamically generated by a separate program. I'm wondering how it's possible to crop my document to that range in the command line?

Everything I've read has suggested I'd need to add a rectangle to the document, resize the document to that rectangle (resize to selection), and then remove the rectangle.

But I'm having trouble with adding and removing that rectangle. I found the ToolRect verb, but I can't seem to find anything related to actually drawing that rectangle (or removing it).

So, am I doing this wrong or is there just no way to add (and select) the rectangle using only the command line? Using another program is also fine, but I haven't had much luck with that (I couldn't get the python modules installed for the only possibly helpful thing I found..).

3

There are 3 answers

0
rudolfbyker On

In this email discussion from 2012, someone said:

There is no way to pass parameters to verbs (with the current implementation, they don't take parameters by design).

In case they add this capability later, the required verbs to crop the page would be:

  1. EditSelectAll
  2. SelectionGroup
  3. ToolRect (requires parameters, i.e. where to crop)
  4. EditSelectAll
  5. ObjectSetClipPath
  6. FitCanvasToDrawing
  7. FileVacuum
  8. FileSaveAs (requires a parameter, so that we don't have to overwrite the original)

Since Inkscape can edit any valid SVG, I'd rather look into other available SVG libraries, like this one for Python.

If you are OK with rasterising your image, take a look at this question. Inkscape unfortunately ignores the --export-area option when exporting to svg or pdf.

0
Nebula On

My – admittedly, unsatisfying – solution was to create a separate program to add a viewbox to the SVG text.

The program I made was implemented into a separate part of my project, so I don't have a good command line version, but if you plan on making one yourself, whatever XML editing library you have for your language of choice should be all you need. I used xmldom for Node.js with relative ease.

1
Peter On

I'm replying to this old question because I have come across the same problem and have found a different, easy solution. In the Inkscape, at least in the version 0.92, once you select your elements in the document, go to menu File > Document Properties (or simply Shift+Ctrl+D). On the "Page" tab, under the "Custom size" section look for the button "Resize page to drawing or selection". In my case this button solved the problem.