Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick PDF crop retain quality

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.

like image 258
Display name Avatar asked May 27 '16 11:05

Display name


2 Answers

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.

like image 113
KenS Avatar answered Oct 13 '22 20:10

KenS


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

like image 29
Destrif Avatar answered Oct 13 '22 20:10

Destrif