5

(similar question exists, is explicitly for another platform and doesn't mention that no OCR is needed)

I need a command line tool (or a PDF viewer which supports this as a display option) which can remove the white border of a pdf file. No OCR or anything, just perfectly white borders. The input PDF files are vector graphics, i.e. white is always perfectly white. Ideally the output files would also be vector graphics/pdf so as not to waste disk space and so I can still zoom.

It needs to be a command line tool which supports at least Linux.

In a similar question this is an answer for Imagemagick:

From the formats page:

PDF - RW - Portable Document Format: Requires [Ghostscript][4] to read. By default, ImageMagick sets the page size to the MediaBox. Some PDF files, however, have a CropBox or TrimBox that is smaller than the MediaBox and may include white space, registration or cutting marks outside the CropBox or TrimBox. To force ImageMagick to use the CropBox or TrimBox rather than the MediaBox, use -define (e.g. -define pdf:use-cropbox=true or -define pdf:use-trimbox=true). Use -density to improve the appearance of your PDF rendering (e.g. -density 300x300). Use -alpha remove to remove transparency. To specify direct conversion from Postscript to PDF, use -define delegate:bimodel=true. Use -define pdf:fit-page=true to scale to the page size.

Well, I can't get it to work, neither

convert input.pdf -define pdf:use-trimbox=true output.png

nor the command with "cropbox" removes the borders of a standard LaTeX generated .pdf file.

Rationale: My use case is documents/books which are typeset in LaTeX. No, I don't have the source and it's not practical to get it. The (large) white border is nice and all when printing. But when working with a pdf file on a 10" netbook then space is precious and zooming past the borders can make the difference between being able to read small print (in formulas, exponents, indices, etc.) comfortably or hardly at all (I can also just use half of the screen for the pdf because those are mostly university assignments where I need to see exercise and my solution so far at the same time or compare something with a text book, stuff like that).

2

2 Answers 2

8

pdfcrop

Have you checked out pdfcrop?

See: http://manpages.ubuntu.com/manpages/gutsy/man1/pdfcrop.1.html

It is described in more details here: https://askubuntu.com/questions/124692/command-line-tool-to-crop-pdf-files

krop

krop is what I love to use: http://arminstraub.com/software/krop

I use the GUI, but it can be run via CLI as well - maybe that is what you are looking for:

:~$ krop -h
usage: krop [-h] [-v] [-o OUTPUT] [--rotate {0,90,180,270}]
            [--whichpages WHICHPAGES] [--initialpage INITIALPAGE] [--autotrim]
            [--selections {all,evenodd,individual}] [--no-kde] [--no-PyPDF2]
            [file]

krop: A tool to crop PDF files

Copyright (C) 2010-2015 Armin Straub, http://arminstraub.com

positional arguments:
  file                  PDF file to open

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -o OUTPUT, --output OUTPUT
                        where to save the cropped PDF
  --rotate {0,90,180,270}
                        how much to rotate the cropped pdf clockwise (default: 0)
  --whichpages WHICHPAGES
                        which pages (e.g. "1-5" or "1,3-") to include in cropped PDF (default: all)
  --initialpage INITIALPAGE
                        which page to open initially (default: 1)                                                                                                                                         
  --autotrim            create a selection for the entire initial page minus blank margins                                                                                                                
  --selections {all,evenodd,individual}                                                                                                                                                                   
                        to which pages should selections apply                                                                                                                                            
  --no-kde              do not use KDE libraries (default: use if available)                                                                                                                              
  --no-PyPDF2           do not use PyPDF2 instead of pyPdf (default: use PyPDF2 if available)  
4
  • As I wrote in my comment, indeed I succesfully used pdfcrop, as suggested by Martin Schröder
    – Nobody
    Apr 9, 2016 at 15:49
  • Damn. Finally used pdfcrop on my files: It removes the table of contents and in-pdf links. I guess then I'll try krop.
    – Nobody
    Apr 12, 2016 at 6:05
  • Also just noticed pdfcrop blows up filesizes by factors between 5 and 10. That's not really a practical problem, don't have that many pdfs, but it's really unsatisfying.
    – Nobody
    Apr 17, 2016 at 7:02
  • krop --autotrim test.pdf -o crop.pdf doesn't work. It crops more than just the whitespace (and I can't script it, it opens it's window, where I have to click a button to get it to work).
    – Nobody
    Apr 17, 2016 at 7:11
3

Have you ever tried Briss and K2pdfopt (Reference)? Both are open source and I believe they support command line usage .

1
  • Please describe what each of them does and give some details of why you think that they might be an answer. Also please reword your answer as an answer rather than a question, i.e. "You can do this with" rather than "Have you ever tried?" Apr 8, 2016 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.