Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I'm looking to convert a .jpg into a .pdf, so that the .jpg is positioned at a certain location: centered on (say) an A4 page, with a minimum border of 30 pixels all around between image and page borders.

My starting point is just this (it just creates a PDF the same size as the JPG):

convert image.jpg image.pdf
share|improve this question

2 Answers 2

up vote 6 down vote accepted

I assume you want to add a border of, say 30px, on all four sides. Use the -border option for the size and -bordercolor for the colour:

convert -border 30 -bordercolor white input.png output.pdf

More information can be found here: Imagemagick - Adding/Removing Image Edges

If you want the final PDF to be of a particular size, you can use the following options:

convert \
  -page A4 \
  -gravity northwest \
  -border 30 \
  -bordercolor white \
  input.png output.pdf
share|improve this answer
thanks, ya that puts it down 30pixels which i want, also looking to make the pdf a whole page, with just the image 30 pixels down at the top –  chris mccoy Jul 25 '12 at 18:12
I updated my answer, but it's hard to tell what you actually want. If it's still not what you are looking for, it might make sense to update your question, rephrase it, add more details or an image of what you want to achieve. –  Marco Jul 25 '12 at 18:54

Here is a little script that does what you want, and can do it to multiple .jpg files at the same time, converting each into its own one-page .pdf file.

Save the script as and call it with one or multiple filename arguments, like this:

./ myimg1.jpg myimg2.jpg img-*.jpg

How it works

  • Output filenames will correspond to input filenames with .jpg replaced with .pdf. So make sure it will not accidentally overwrite some files!

  • For the page to mount the image on, it uses A4 format. It calculates the A4 dimensions itself, since the A4 keyword is seemingly not supported in recent ImageMagick versions anymore.

  • The image is not resampled ("scaled") by the script, just shown centered with a certain resolution on the A4 PDF page canvas. So, no image information is lost by scaling down, and no useless file size added by scaling up.

  • Instead of a 30 pixel minimum margin all around, the script leaves a space between image and PDF page borders that is 5% of the page width resp. height. You can adapt it by adapting the resolution scaling factor, currently 1.1 below. The advantage of this over adding a white border to the image is that it does not add to the file size, and that you can extract the unmodified image later from the PDF if desired, using a command like pdfimages -j file.pdf img.

Other details

  • When calculating the pixel dimensions of an A4 page in the script below, the chosen density leads to a 5% of width resp. height minimum wide border all around the page. Assuming the case that the x dimension needs the higher resolution to leave that 5% border empty, this happens according to this formula: page_size_x = density * 8.27, replacing with density = img_size_x / 8.27 * 1.1 leads to: page_size_x = (img_size_x/8.27*1.1) * 8.27 = img_size_x * 1.1. This way, the resolution scaling factor of 1.1 leads to 5% borders at the start and end of a page's width, and you can adapt it to get other border widths.

  • Some people seem to need a -repage operation (like here) to prevent page size from being slightly "off". Did not need it, but if you do, try -repage ${page_size_x}x${page_size_y} or -repage A4 as last operation in the convert call.

Script source code

# Converts input images to one-page PDF files each, without changing image data.
# The image is centered on a A4 page with a 5% border.

# bc function to calculate maximum of two floats
define max(a,b) {
  if (a>b) {
  } else {
} ";

for file in "$@"; do \
  img_size_x=$(identify -format "%w" $file);
  img_size_y=$(identify -format "%h" $file);

  # Factors 1.1 create a 5% minimum border around on the A4 page.
  # Density is calculated in dpi: pixels/(A4 width in inch)
  min_density_x=$(echo "$img_size_x/8.27*1.1"  | bc -l);
  min_density_y=$(echo "$img_size_y/11.69*1.1" | bc -l);
  # Use the higher density to prevent any dimension exceeding A4 size.
  density=$(echo "$bc_functions max($min_density_x,$min_density_y)" | bc -l);

  # Calculate px dimensions of A4 (8.27x11.69") with chosen density.
  page_size_x=$(echo "$density*8.27"  | bc -l);
  page_size_y=$(echo "$density*11.69" | bc -l);

  # Center on a larger canvas of size given by "-extent".
  convert $file \
    -gravity center -extent ${page_size_x}x${page_size_y} \
    -units PixelsPerInch -density $density \
    -format pdf -compress jpeg \


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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