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?
2 Answers
Using ImageMagick, you can try:
convert page.png page.pdf
For multiple images:
convert page*.png mydoc.pdf
-
8what 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– vcarelJul 17, 2013 at 0:29
-
46To 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 -
37FYI 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 exampleconvert $(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.– SixMay 6, 2015 at 12:49 -
23This 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
– jlhNov 18, 2015 at 17:40 -
23ImageMagick decodes the JPEG, resulting in generation loss. Use img2pdf instead; it's also 10–100 times faster. Jan 19, 2017 at 20:29
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.
img2pdf img1.png img2.jpg -o out.pdf
-
49ImageMagick decodes the JPEG, resulting in generation loss. Use img2pdf instead; it's also 10–100 times faster. Jan 19, 2017 at 20:30
-
18
-
@RobertFleming, Kelvin, your suggestions are awesome, too bad we cannot add them as a proper answer to this thread. Cheers– AzurtreeApr 4, 2022 at 11:44
-
-
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 viatesseract
. Meanwhile, I've used your answer in my work-around. Jun 28, 2022 at 5:19
sudo apt-get install gscan2pdf
for simple and easy use.img2pdf $(find . -iname '*.jpg' | sort -V) -o ./document.pdf
will give youdocument.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 forsort
) so there is no need to add any leading zeros when numbering image files.