Semi-Autocrop PDF using Inkscape command line

Programs intend to save a whole page for everything that is saved as PDF, even if the content just takes 5% of a page. If you then have to crop a large batch of PDFs manually it is really a pain, but Inkscape offers a nice way to do this using the command line.

Just create a batch file i.e. semiautocrop.bat, put the following code inside (don’t forget to change to path to you Inkscape folder, just place the path without brackets i.e. c:inkscape) and place it into the folder of your PDFs.

The batch will run through all of the PDFs in the current folder and open Inkscape to crop the PDFs. Unfortunately I didn’t find a way to omit the dialogs for importing the PDF, saving and closing Inkscape without saving a SVG.

But nonetheless it’s way faster then doing everything manually. If you have an idea how to improve this leave me a comment.

  • Whitewizard

    HI! I make a .txt file and I put inside
    for /r %cd% %%i in (*.pdf) do C:Program Files (x86)Inkscapeinkscape.exe –verb=FitCanvasToSelectionOrDrawing –verb=FileSave –verb=FileClose %%i
    then, save and rename the .txt in .bat
    I put the .bat in the same folder with pdf, double click on the bat, just appears/disappear a dos windows, no other effects. I wonder, where I’m wrong?
    I need something like this to cut away white borders from pdf, in order to have all the pdf straighten. that’s my sake. can you help me?

    • Matthias Tylkowski

      The best way to check this is to use the console.
      Start -> Run -> type: “cmd”
      Then move with the “cd” command to the location of your “.bat”- file. And run it from there. If something is wrong there will propably be some error messages.

      • Matthias Tylkowski

        and you should put the path to inkscape in quotes

        • Whitewizard

          thank you for your answer.

          for /r %cd% %%i in (*.pdf) do “C:Program Files (x86)Inkscapeinkscape.exe” –verb=FitCanvasToSelectionOrDrawing –verb=FileSave –verb=FileClose %%i

          that’s the new sintax you suggest. I tried from CMD both the *.bat with and without quotes, the result is an italian message, which translation must be PDF NOT ATTEND. (pdf non atteso.)
          if it can be useful I’m running a seven 64bit.
          thank you again for your time

          • Matthias Tylkowski

            I updated the command and added quotes around %cd%, the path to inkscape and the last %%i

            On my 64bit vista it worked like that.

          • Whitewizard

            thank you again for your efforts but it still doesn’t work.
            if I write this sintax

            for /r “%cd%” %%i in (*.pdf) do “C:Program Files (x86)Inkscapeinkscape.exeinkscape.exe” –verb=FitCanvasToSelectionOrDrawing
            (that is as the command line above) I receive the message Name Directory Not Valid.

            If I remove inkscape.exe, with this other sintax
            for /r “%cd%” %%i in (*.pdf) do “C:Program Files (x86)Inkscapeinkscape.exe” –verb=FitCanvasToSelectionOrDrawing
            I receive as answer
            “C:program files inkscapeinkscape.exe” –verb=FitCanvasToSelectionOrDrawing

            then Inkscape start itself with a new document 1 open, but there are no change, no crop in the pdf I put in the same folder of the *.bat

            any suggestion, please?

          • Matthias Tylkowski

            Please use the full command.
            It is correct that Inkscape starts. It should open a window for pdf-import. Hit ok there.

            Then you should see a pdf saving window. Hit ok there too.

            At last inkscape asks you to save the file as svg. You may hit “close without saving” here, since you just need the pdf.

            Then it should start with the next file with the same process.

            Thats why I called it semi-auto-crop, since user interaction is needed. I didn’t figure out how it works fully automatical.

            Hope this helps you.

          • Whitewizard

            no, inkscape starts with the new document. no window pdf import. anyway thank you again for your efforts ^_^

          • Matthias Tylkowski

            This should be in your bat-file.

            for /r “%cd%” %%i in (*.pdf) do “C:Program Files (x86)Inkscapeinkscape.exe” –verb=FitCanvasToSelectionOrDrawing –verb=FileSave –verb=FileClose “%%i”

          • Whitewizard

            you’re right, now the *.bat works well.
            I still have a problem: inkscape crops the white areas only on a vectorial pdf. is there a way to have the same results with raster pdf?
            anyway thank you so much for your file, your patience and your time

          • Matthias Tylkowski

            Sorry for that you should consult google. Maybe this is for you:

            Good Luck.