8

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 ?

1
  • Do you mean separate PDFs or a single PDF? Commented Nov 20, 2010 at 18:04

4 Answers 4

13

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Good tip, but the sort order is the problem.
Right, I've added more for your sorting issue.
The ls in backticks is not doing anything useful by itself. If you add a useful sorting option, it might be warranted, though.
@tripleee: I did state the need for a sorting option and how to go about finding them in the answer.
ImageMagick needlessly decodes and re-encodes the JPEG data, resulting in generation loss and poor performance.
|
6

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.

4 Comments

I cannot see a license on this code. Did you write it yourself? Is it MIT, GPL, etc?
I didn't write that. Its author's e-mail address is hidden in his test_comp.sh if you want to contact him.
Not particularly, just thought it is unusable generally without a license. Which is a shame, as it's no doubt useful.
Author of img2pdf here - I added a license a while ago but forgot to push my changes :) You can now see that it is released under the terms of GPL3+. I didnt think it would ever be useful for anybody which explains the long lack of a license.
1

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

Comments

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

2 Comments

I need a single pdf file with all images order by name ( logical order image1.png,image2.png,...image11.png)
The ls in backticks is an antipattern. for image in *.png; do ... is both simpler and more correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.