Take the 2-minute tour ×
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. For that, it will use filenames made up by replacing .jpg with .pdf.

For the page to mount the image on, I use A4 format. I calculate the A4 dimensions in the script, since the A4 keyword is not supported in recent ImageMagick versions anymore (?).

The image is not resized, just shown centered in a certain resolution on the A4 PDF page canvas. Instead of a 30px minimum margin all around, I leave a 5% space between image and PDF page borders (adapt it with the resolution scaling factor that is now 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 you could extract the unmodified image later from the PDF if desired).

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

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

Script source code:

#!/bin/bash
# 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
bc_functions="
define max(a,b) {
  if (a>b) {
    return(a)
  } else {
   return(b)
  }
} ";

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 \
    ${file/.jpg/.pdf};
done;

Details

When calculating the px dimensions of an A4 page in the script above, the chosen density (img_size_x / 8.27 * 1.1) leads to a 5% border all around as follows: page_size = density * 8.27 = (img_size_x/8.27*1.1) * 8.27 = img_size_x * 1.1. (In the case that the x dimension needs the higher resolution. Could also be y dimension though, so we can't do it directly like that in the script.)

Also note that some peope 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.

References

share|improve this answer

Your Answer

 
discard

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.