johnlemon
johnlemon

Reputation: 21449

Bash convert to pdf

How can I use both ls and convert to transform all images files in a directory to a pdf ? Also I need to put the files in a certain order for example files like AA1.png,AA11.png need to respect this logical order.

Update (ls) and (convert) are available , but how can I used them together ?

Upvotes: 8

Views: 15903

Answers (4)

datnoobuser
datnoobuser

Reputation: 11

If you have a lot of files:

convert -limit memory 1 -limit map 1 *.jpg foo.pdf

see here

or with compression

convert -limit memory 1 -limit map 1 -compress jpeg -quality 85 *.jpg foo.pdf

Upvotes: 1

Robert Fleming
Robert Fleming

Reputation: 1407

https://gitlab.mister-muffin.de/josch/img2pdf

In all of the proposed solutions involving ImageMagick (i.e. convert), the JPEG data gets fully decoded and re-encoded. This results in generation loss, as well as performance "ten to hundred" times worse than img2pdf.

Upvotes: 6

Orbling
Orbling

Reputation: 20602

To convert to a single PDF can be done in a single command:

convert -compress jpeg *.jpg my-jpegs.pdf

Remember to include the -compress jpeg flag, or it'll store the images uncompressed and result in a massive PDF.

ImageMagick (via convert) requires Ghostscript (gs) to be installed in order to process PDFs I believe. Beware of memory issues if you are adding a lot of JPEGs at once.

As for your logical ordering, you can use ls in combination with convert to get the list in order.

Something along the lines of:

convert -compress jpeg `ls *.png` my-jpegs.pdf

See ls --help for the various sorting options available.

Upvotes: 13

khachik
khachik

Reputation: 28703

for image in `ls *.png`; do
  # call convert or whatever here
  convert $image `basename $image .png`.pdf
done

Upvotes: 0

Related Questions