Al.BQ
Al.BQ

Reputation: 11

Build sorted and annotated pdf from images

I am trying to build a pdf from a set of image files in the same folder from the bash. So far I've got this code:

ls *.jpg | sort > files.txt
ls *.jpg | sort | tr '\n' ' ' | sed 's/$/\ data_graphs.pdf/' | xargs convert -gravity North -annotate @files.txt
rm files.txt

This code collapses the image, but they are not properly sorted, and the annotation is the same for every image (the first one in the list).

Here is the ls * jpg |sort output for reference.

$ ls *.jpg | sort 
01.20.2014_A549_void.jpg
01.20.2014_EPOR_full_sorter.jpg
01.20.2014_EPOR_trunc_sorter.jpg
01.20.2014_WTGFP_sorter.jpg
01.27.2014_A549_void.jpg
01.27.2014_EPOR_full_I10412.jpg
01.27.2014_EPOR_full_sorter.jpg
01.27.2014_EPOR_trunc_I10412.jpg
01.27.2014_EPOR_trunc_sorter.jpg
01.27.2014_WTGFP_I10412.jpg
01.27.2014_WTGFP_sorter.jpg
02.03.2014_A549_void.jpg
02.03.2014_EPOR_full_sorter.jpg
02.03.2014_EPOR_trunc_sorter.jpg
02.03.2014_WTGFP_sorter.jpg

Upvotes: 1

Views: 93

Answers (1)

BMW
BMW

Reputation: 45243

How about this, no need generate the temporary file files.txt

convert -gravity North -annotate `ls *.jpg | sort -t . -k3.3n -k1.1n -k2.2n ` data_graphs.pdf

According the comments, these jpg files have time-stamp in file name (MM-DD-YYYY), I updated the sort command.

another way, convert each jpg file to pdf first, then use pdftk to merge them, I used pdftk for long years and know the software can do the job easily. Here is the pdftk server url : pdflabs.com/tools/pdftk-server.

Below script will convert jpg file to pdf one by one

for file in *jpg
do
  convert -gravity North -annotate "$file" "$file".pdf
done

Then run the pdftk command, if you have hugh number of pdf. With pdftk, you can merge every 10~20 into a small pdf, then merge the small pdf to final pdf. For example:

pdftk 1.pdf 2.pdf 3.pdf output m1.pdf

then you will get mXXX.pdf files, then run the pdftk again:

pdftk m1.pdf m2.pdf m3.pdf output final.pdf

Upvotes: 1

Related Questions