6

I have got some PDF files i need to crop (crop to trimbox etc), which I can do with the following command

convert -define pdf:use-trimbox=true -density 300 original.pdf outcome.pdf

It does the job however the outcome.pdf quality if not as sharp as original PDF. When I crop them on my desktop software (Acrobat Pro) the result it same quality but in ImageMagick I can not keep the same quality in the outcome.

My question is how can i crop a pdf page without compromising from the quality?

i have been searching and trying different settings for weeks but not been succesfull.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
Display name
  • 420
  • 5
  • 20
  • ImageMagick`s `convert` command employs Ghostscript in the background to convert the (possibly) vector PDF pages to raster images first. Then it applies the "trimbox" to the image and wraps a full page raster image again into PDF page containers. In other words, you whole piece of meat was converted to minced meat -- and there is no way to get back your whole piece. (You have to employ other tools which avoid the rasterization.) – Kurt Pfeifle May 27 '16 at 16:52

2 Answers2

8

Most likely the problem is that ImageMagick is having the PDF rendered to a bitmap by Ghostscript, and then exporting the bitmap wrapped up in a PDF file. Without seeing the original I can't say for sure, but if the original contained JPEG images, then most likely you are ending up with JPEG being applied twice, or simply rendering at all is causing the problem.

Your best bet is going to be to use a tool which can simply apply a CropBox to the page(s). You can do this with Ghostscript, for example (which may also modify the PDF in other ways, including the double JPEG quantisation, so beware).

gs -sDEVICE=pdfwrite \
   -sOutputFile=cropped.pdf \
   -dBATCH  -dNOPAUSE \
   -c "<</ColorImageFilter /FlateEncode>> setdistillerparams" \
   -f <input.pdf> \
   -c "[ /CropBox [ 0 0 100 100] /PAGES pdfmark" \
   -f

The first section between -c and -f tells the pdfwrite device to use FlateEncode for colour images, the default is JPEG, using Flate will ensure you don't get quantisation applied twice.

The second section between -c and -f tells the pdfwrite device to write a CropBox to the file and to make it 0,0 to 100,100. The units are the usual units in PDF; 1/72 inch, you can use fractional values.

I'm sure there are other tools which will do this, possible even more easily.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
KenS
  • 30,202
  • 3
  • 34
  • 51
1

Have you tryed to increase the density? That's the purpose:

http://www.imagemagick.org/script/command-line-options.php#density

Otherwise try:

-quality 100

From: Convert PDF to image with high resolution

Community
  • 1
  • 1
Destrif
  • 2,104
  • 1
  • 14
  • 22
  • hi destriff, i have tried quality , increased the density up to 600 but it is still not dealing with it perfectly – Display name May 27 '16 at 11:58
  • I am sorry, but I do not see any other options that might get rid of this problem in the documentation. You can increase quality to 1024, as documentation says. – Destrif May 27 '16 at 11:59