Converting hundreds of jpg to PDF using Terminal
Solution 1
My first instinct for batch processing files is almost always find
. It's excellent if you need to build in any sort of filtering (which you don't here) but it's still a favourite. This will also recurse into subdirectories unless you tell it (with -maxdepth 1
or other):
find -name '*.jpg' -exec convert "{}" "{}.pdf" \;
rename 's/\.jpg\.pdf$/.pdf/' *.jpg.pdf
The find
/convert
statement will output a load of .jpg.pdf
files. The second cleans this up.
Perhaps a slightly more elegant approach in such a simple case:
for file in *.jpg ; do convert "$file" "${file/%jpg/pdf}"; done
This doesn't recurse and you don't have to mess around cleaning up the filenames.
And I almost forgot, ImageMagick has a numerical output which might fit your use-case perfectly. The following will just stick a three-digit identifier (000
, 001
, 002
, etc) on the end of the "myPDF":
convert *.jpg myPDF%03d.pdf
Obviously if you're dealing with more than a thousand entries, increase the number. If you don't want it zero-padded, remove the leading zero.
Solution 2
Unfortunately, convert
changes the image quality before "packing it" into the PDF. So, to have minimal loss of quality, is better to put the original jpg
, (works with .png
too) into the PDF, you need to use img2pdf
.
I use these commands:
With time I discovered a simpler, shorter one liner solution also using img2pdf
that doesn't need pdftk
as in the original answer
Make PDF
img2pdf *.jp* --output combined.pdf
(optional) OCR the output PDF
ocrmypdf combined.pdf combined_ocr.pdf
Below is the original answer commands with more command and more tools needed:
This to make a
pdf
file out of everyjpg
image without loss of either resolution or quality:ls -1 ./*jpg | xargs -L1 -I {} img2pdf {} -o {}.pdf
Here you will have the
*.pdf
s as*.jpg.pdf
, so will do a little renamingmmv "*.jpg.pdf" "#1.pdf"
You need to have installed img2pdf and mmv
sudo apt install img2pdf mmv
Related videos on Youtube
![Physics_maths](https://i.stack.imgur.com/VUdxq.jpg?s=256&g=1)
Physics_maths
Updated on September 18, 2022Comments
-
Physics_maths almost 2 years
I know that the command
convert *.jpg myPdf1.pdf
can convert multiple JPEG files into a single PDF.But I would like to convert multiple JPEGs into multiple PDFs, for example:
myJPG1.jpg → myPDF1.pdf myJPG2.jpg → myPDF2.pdf myJPG3.jpg → myPDF3.pdf
Is there any decent way to manage something like that?
-
jobin about 10 yearsHow would you order them?
-
Physics_maths about 10 yearsWell suppose they are called myJPG1, myJPG2 and so on, and you want to convert them to myPDF1, myPDF2 and so on.
-
jobin about 10 yearsThe question isn't clear, do you have multiple jpgs or pdfs? You can convert multiple jpgs to a single pdf using the command you have used in your question.
-
Physics_maths about 10 yearsIt should be clearer now, I just want a bunch of PDF's with names myPDF1 myPDF2 and so on. But Your suggestion, into a single PDF, also sounds nice. Thanks.
-
-
Yauhen Yakimovich over 9 yearsfind -name '*.jpg' | parallel 'convert {} {.}.pdf'