Thursday, March 6, 2014

One-liner command for changing pdf size

The commands in this post require the GraphicsMagick package (see this for an explanation). GraphicsMagick commands start with gm in front of them.

One use of these programs is to resize pdf files. I found a solution here, which was really helpful. I will repeat and expand on it.

 TLDR Version

Use the following command in terminal:
gm convert -density 300x300 -quality 25  -compress jpeg  originalFile.pdf targetFile.pdf

 Explanation

Now let's see what that command does. First the bare bones:
gm convert  -compress jpeg  originalFile.pdf targetFile.pdf
This command will compress your pdf file for you, but will use default values for the output's density and compression quality. Unfortunately, the resulting output is overly compressed and unusable for most uses.

-compress jpeg defines the type of image compression. Read more about it here.

Adding the -density 300x300 makes sure that the pdf's resolution is satisfactory. Read more about it here.

If the output file's size is till too big, you can either lower the above resolution, or increase the compression level of the pdf (and lower its quality). That is what the -quality 25 command does. The default value is at the higher quality of 75. The number can be anything from 0 (lowest quality) to 100 (highest quality).
Read more about -quality here.

No comments:

Post a Comment