0

I have two images named x.jpg & y.jpg. I want to find/locate them and convert them to .pdf. I locate and convert to .pdf one image by:

convert $(locate x.jpg) file.pdf

But for converting two images I can't do that. I do :

convert $(locate x.jpg) && convert $(locate y.jpg) file.pdf

But without success. I couldn't have done that with for/do loop either:

for i in $(locate x.jpg && locate y.jpg); do convert $i file.pdf; done

This loop finds and converts the first image but then an error says that the second image was not found.

I want to find two images, and put both of them in one .pdf file.

2
  • 1
    convert $(locate x.jpg) && ... what happened to x.pdf there? And what for/do loop did you try?
    – muru
    May 31, 2018 at 14:34
  • 1
    Do you want to combine the two images into a single pdf? May 31, 2018 at 14:35

2 Answers 2

4

You can do that with find like so:

find path/ -type f -name '[xy]\.jpg' -exec convert {} {}\.pdf \; 

The files will then be named x.jpg.pdf and y.jpg.pdf. find takes regex for a name pattern so you can do that in batch for many files, you just need the proper regex. This however only creates you two single PDF files for each image.

To put them all into one PDF file you can use the following:

# create a directory in /tmp
mkdir /tmp/output
# find all the images and convert them to single standing PDF files
# then move them to the output directory
find path/ -type f -name '[xy]\.jpg' -exec convert {} {}\.pdf \; -exec mv {}\.pdf /tmp/output/ \;
# now join them all together to a single file
pdfunite /tmp/output/* ~/output.pdf
# normally the system clean up /tmp on restart, but we clean up ourselves
rm -r /tmp/output

Here as well you need to proper regex for finding all your files, but the rest is not limited on how many files you search like this.

Of course you can do a bash script from that:

#!/bin/bash
# create a directory in /tmp
tmpdir="$(mktemp -d)"
# find all the images and convert them to single standing PDF files
# then move them to the output directory
find "$1" -type f -name "$2" -exec convert {} {}\.pdf \; -exec mv {}\.pdf "$tmpdir" \;
# now join them all together to a single file
pdfunite "$tmpdir"/* "$3"
# normally the system clean up /tmp on restart, but we clean up ourselves
rm -r "$tmpdir"

Save it as script.sh and do chmod 755 script.sh now you can call it like this:

# usage script.sh [PATH] '[PATTERN]' [OUTPUT-FILE]
./script.sh path/ '[xy]\.jpg' ~/output.pdf

To incorperate @dessert's proposed changes the script becomes this but works the same:

#!/bin/bash
# create a directory in /tmp
tmpdir="$(mktemp -d)"
# find all the images and convert them to single standing PDF files
# then move them to the output directory
find "$1" -type f -name "$2" -exec sh -c 'convert "$1" "$0/${1##*/}.pdf"' $tempdir "{}" \;
# now join them all together to a single file
pdfunite "$tmpdir"/* "$3"
# normally the system clean up /tmp on restart, but we clean up ourselves
rm -r "$tmpdir"

Have fun.

1
  • 1
    Thanks for your good answer, finally i found a simpler way: convert $(locate x.jpg && locate y.jpg) Newfile.pdf
    – user833907
    Jun 1, 2018 at 10:49
2

You could run something along the lines of:

echo convert $(locate x.jpg) $(locate y.jpg) +append out.pdf

and if it sound OK, remove the echo

convert $(locate x.jpg) $(locate y.jpg) +append out.pdf