2

There are many times I get single page PDFs that I want to convert to JPEGs and crop the excess whitespace off of.

Here is the current set of commands I have which accomplishes what I want:

gm convert -density 300 -trim INPUT.PDF TMP.PNG
gm convert -bordercolor white -border 10 TMP.PNG OUTPUT.JPG
rm TMP.PNG

I am trying to figure out how to condense these commands into a single command, and avoid creating the temporary TMP.PNG for processing.

This is my current attempt at consolidating the above commands:

gm convert INPUT.PDF -density 300 -trim -bordercolor white -border 10 OUTPUT.JPG

The problem I have with this command is that it generates a very blurry JPEG. Below, the first image (on the left) is a sample of the undesired result generated by my single-command attempt. The second image (on the right) is a sample of the crisp, high-quality result I am looking for that I currently have to use multiple commands to achieve. What is the correct way to consolidate the commands at the beginning of my post?

sample from image generated by single-command solution sample from image generated by multi-command solution

2

One of the few things I've learned the hard way about ImageMagick is that the order of arguments can be vital. In particular, you are providing an input pdf file, then suggesting a density to use when converting it to an image, whereas you need to set the density before reading the pdf. Simply invert those 2 items and you should find the same resolution of output as before:

gm convert -density 300 INPUT.PDF -trim -bordercolor white -border 10 OUTPUT.JPG

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.