convert pdf to pdf produces huge pdf

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
jauson
Posts: 36
Joined: 2015-05-04T21:05:35-07:00
Authentication code: 6789

convert pdf to pdf produces huge pdf

Post by jauson »

I'm trying to fatten a pdf and have good quality. My command is:

Code: Select all

convert -density 288 input.pdf output.pdf

My pdf of 5 megs becomes 130 megs. It's 6 pages, but they are full of color and images.

If I run this GS command on it, it shrinks it down to 930k.

Code: Select all

gs -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -sDEVICE=pdfwrite \
  -sColorConversionStrategy=/LeaveColorUnchanged  \
  -dAutoFilterColorImages=true \
  -dAutoFilterGrayImages=true \
  -dDownsampleMonoImages=true \
  -dDownsampleGrayImages=true \
  -dDownsampleColorImages=true \
  -sOutputFile=document_flat.pdf document_original.pdf

Can I achieve this with the single convert command above?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert pdf to pdf produces huge pdf

Post by fmw42 »

ImageMagick is a raster processor, not a vector to vector processor. So if you have a vector PDF and you convert it to PDF using ImageMagick, it will rasterize the PDF to pixels (not vectors) and imbed the raster image in a vector PDF shell. That will make the output much larger than the input. So if you want to go vector to vector, then you should use some other tool.

You might be able to achieve a similar result to using Ghostscript.

1) try adding -compress JPG -quality 80 ( or whatever quality you want for the jpg). I do not know if this will work.

2) modify your delegates.xml file to use the same arguments for gs in the appropriate line

Sorry, I am not an expert on either suggestion.

Perhaps if you post your input PDF, we can test with it.
jauson
Posts: 36
Joined: 2015-05-04T21:05:35-07:00
Authentication code: 6789

Re: convert pdf to pdf produces huge pdf

Post by jauson »

convert TestThis.pdf -compress JPG -quality 80 TestThis-converted.pdf
convert.exe: unrecognized image compression `JPG' @ error/convert.c/ConvertImageCommand/1119.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert pdf to pdf produces huge pdf

Post by fmw42 »

jauson
Posts: 36
Joined: 2015-05-04T21:05:35-07:00
Authentication code: 6789

Re: convert pdf to pdf produces huge pdf

Post by jauson »

convert TestThis.pdf -compress JPEG -quality 80 TestThis-converted.pdf

JPEG works, but it made the file even bigger
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert pdf to pdf produces huge pdf

Post by fmw42 »

Lower the quality value is all I can suggest. Sorry, I am not an expert on PDF files.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert pdf to pdf produces huge pdf

Post by snibgo »

jauson wrote:My pdf of 5 megs becomes 130 megs. It's 6 pages, but they are full of color and images.

If I run this GS command on it, it shrinks it down to 930k.
...
Can I achieve this with the single convert command above?
Without seeing your PDF file, we can only offer general guidance.

Converting vector to raster almost always increases the filesize.

If your PDF has vector data and you want to reduce the size while keeping quality, IM is not the right tool.

If your PDF has only raster data, IM might be able to reduce the filesize.
snibgo's IM pages: im.snibgo.com
jauson
Posts: 36
Joined: 2015-05-04T21:05:35-07:00
Authentication code: 6789

Re: convert pdf to pdf produces huge pdf

Post by jauson »

I'm trying to do this in a single command using pipes, but I'm not sure if its setting the format properly. After this command, i use another program to process this pdf and it says unsupported image type. it's a java program, throwing this error:

Code: Select all

Caused by: javax.imageio.IIOException: Unsupported Image Type
        at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1068)
        at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1039)

Code: Select all

convert -density 288 input.pdf - | gswin64c  \
  -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -sDEVICE=pdfwrite \
  -sColorConversionStrategy=/LeaveColorUnchanged  \
  -dAutoFilterColorImages=true \
  -dAutoFilterGrayImages=true \
  -dDownsampleMonoImages=true \
  -dDownsampleGrayImages=true \
  -dDownsampleColorImages=true \
  -sOutputFile=converted_flattened.pdf -
I tried adding

Code: Select all

PDF:-
and

Code: Select all

-compress JPEG
doesn't seem to fix it.

I think its a GS issue.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert pdf to pdf produces huge pdf

Post by fmw42 »

I do not see any point to piping from ImageMagick to Ghostscript to do further processing. If you want to change the density use

gs -rXRESxYRES

in Ghostscript.

Or add all your gs arguments to the delegates.xml file in ImageMagick for PS or PDF (wherever the sDEVICE=gs
Post Reply