279

I have a scanning server I wrote in CGI and Bash. I want to be able to convert a bunch of images (all in one folder) to a PDF from the command line. How can that be done?

9
  • See also How to generate a PDF from a series of images? on superuser.
    – zrajm
    Dec 13, 2013 at 10:21
  • 2
    Related: Converting multiple image files from JPEG to PDF format at unix SE
    – kenorb
    Feb 26, 2015 at 15:59
  • 18
    Use img2pdf, not ImageMagick. ImageMagick decodes the JPEG, resulting in generation loss and is 10–100 times slower than img2pdf. Jan 19, 2017 at 20:27
  • 1
    sudo apt-get install gscan2pdf for simple and easy use.
    – Haziq
    Jan 18, 2018 at 6:31
  • 4
    img2pdf $(find . -iname '*.jpg' | sort -V) -o ./document.pdf will give you document.pdf containing all images with jpg or JPG extension in the current dir - one image per page. document.pdf will have all images ordered as pages naturally (-V option for sort) so there is no need to add any leading zeros when numbering image files.
    – Jimmix
    Apr 11, 2020 at 18:52

2 Answers 2

495

Using ImageMagick, you can try:

convert page.png page.pdf

For multiple images:

convert page*.png mydoc.pdf
15
  • 8
    what if page*.png does not sort the images in the way you want ? e.g. page_1.png, page_2.png ... page_10.png -> page_10 will appear before page_1
    – vcarel
    Jul 17, 2013 at 0:29
  • 46
    To sort the files, you can use: ls page*.png | sort -n | tr '\n' ' ' | sed 's/$/\ mydoc.pdf/' | xargs convert Feb 7, 2014 at 13:01
  • 37
    FYI you almost never need to use ls for anything apart from displaying files... i.e. do not parse it's output. find is a much more suitable tool. Here is an example convert $(find -maxdepth 1 -type f -name 'page*.png' | sort -n | paste -sd\ ) output.pdf. Keep in mind that the aforementioned command will not work if your pathnames contain spaces. The addition of characters that need to be escaped makes things a little more complicated.
    – Six
    May 6, 2015 at 12:49
  • 23
    This is simple and works very well, thank you! To avoid generating huge PDF files, use something like convert -compress jpeg -quality 85 *.png out.pdf
    – jlh
    Nov 18, 2015 at 17:40
  • 23
    ImageMagick decodes the JPEG, resulting in generation loss. Use img2pdf instead; it's also 10–100 times faster. Jan 19, 2017 at 20:29
41

Use convert from http://www.imagemagick.org. (Readily supplied as a package in most Linux distributions.)

10 years later...

Agreed with Robert and others here. Use something like img2pdf instead.

Usage:

img2pdf img1.png img2.jpg -o out.pdf
5
  • 49
    ImageMagick decodes the JPEG, resulting in generation loss. Use img2pdf instead; it's also 10–100 times faster. Jan 19, 2017 at 20:30
  • 18
    Note: img2pdf has moved to gitlab.mister-muffin.de/josch/img2pdf.
    – kelvin
    May 17, 2019 at 1:55
  • @RobertFleming, Kelvin, your suggestions are awesome, too bad we cannot add them as a proper answer to this thread. Cheers
    – Azurtree
    Apr 4, 2022 at 11:44
  • To install: sudo apt update && sudo apt install img2pdf Jun 28, 2022 at 5:03
  • I have an issue open in my pdf2searchablepdf project to allow images to be used as inputs too, so they can be converted to searchable PDFs via tesseract. Meanwhile, I've used your answer in my work-around. Jun 28, 2022 at 5:19

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