vivek85
vivek85

Reputation: 411

How to delete the hidden cropped elements from a pdf from the command line?

How can I delete the hidden cropped elements from a pdf from command line? I've tried many solutions to crop an element on a pdf page based on the coordinates, but the resulting pdf is of the same size and the extra elements are just masked instead of removed. I've found this solution, but I'm looking for a command line tool.

Upvotes: 3

Views: 3893

Answers (3)

mayeulk
mayeulk

Reputation: 150

"Printing to pdf" usually does a complete reflow of the document. This is the only solution that worked for me, but this is only partly a command line solution. Run this at the Linux shell:

okular --print-and-exit my_file.pdf

This will open the file with Okular directly on the print dialog. You still have to use the mouse to select "Print in a file (PDF)", optionnaly change the suggested filename, click OK. Okular will exit automatically. Maybe you can automate the above, or find another "pdf to pdf printer" working on the command line.

To check the effect: As seen in Scribus, below, before (left) and after (right) PDF-printing with Okular. Control in Scribus

Upvotes: 1

Cami
Cami

Reputation: 39

I found some solutions which may or may not meet your needs :

  • Use pdfimages in command-line to extract the images of the pdf : see https://askubuntu.com/a/150106

    pdfimages -all in.pdf .

  • Using Inkscape GUI : open the pdf with inkscape, select the desired part, right click on selection, group, select everything (CTRL+A), unselect the grouped part (shift + left click), save as pdf. See https://inkscape.org/

  • Using Inkscape with command line (same margin for all sides), it is possible to crop and erase the border of a pdf by adding a negative margin (manual at https://inkscape.org/doc/inkscape-man.html). Example with 20 mm :

    inkscape in.pdf --export-margin=-20 -o out.pdf

  • Using Inkscape with command line (custom margins) : use a generic method to crop the pdf (see How can I easily crop a PDF page?, these methods just hide the content) then, use Inkscape to re-export the cropped pdf (this will erase the content that is outside the page) :

    inkscape input.pdf -o output.pdf

Note that using Inkscape, the document may vary, especially if you don't have the same fonts than the original document.

Upvotes: 0

Kurt Pfeifle
Kurt Pfeifle

Reputation: 90285

The only method I'm aware of that can do this while preserving all PDF objects inside the CropBox working on the commandline (Windows, Linux, MacOSX) is a commercial/payware software by callassoftware.

If this is acceptable to you, please indicate that in a comment, and I'll update the answer with some more details.

Otherwise another method is to convert the (cropped) PDF page(s) to images (f.e. to a TIFF with the help of Ghostscript), and then re-convert the image back to PDF. All previous PDF objects will be gone, and the only (graphical) "object" that's there now will be an image that's embedded on the PDF page(s).

If this is acceptable to you, indicate this in a comment, and I'll update the answer with some more details.

Upvotes: 1

Related Questions