0

I am looking for a way of automating a manual conversion of directories containing images (jpgs & pngs) into PDF's. For example ..

I currently use the following command to achieve what I want but I have to do this manually for each directory so it can become a little time consuming, etc

convert DIRECTORY1/*jpg DIRECTORY1.pdf
convert DIRECTORY2/*jpg DIRECTORY2.pdf
convert DIRECTORY3/*png DIRECTORY3.pdf

I would like a way of converting all the directories in the current location to individual pdf files at once.

Any help would be appreciated.

derHugo
  • 3,356
  • 5
  • 31
  • 51

1 Answers1

0

Something like will walk in the tree, extract directories and do the conversion. Be aware this can have problems with directory names which contain space or special symbols:

for i in `find . -type d`
do
convert "${i}/*jpg" "${i}/*png" ${i}.pdf
done
  • Thank you for the reply and suggestion. I seem to get the following sytax error when running the script ..

    ./test.sh: line 2: syntax error near unexpected token $'do\r'' '/test.sh: line 2:do

    – Ben Robins Jun 26 '17 at 12:17
  • 1
    @BenRobins your script likely has Windows-style (CRLF) line endings - check your editor settings and make sure it is set to Unix style (LF) – steeldriver Jun 26 '17 at 12:19
  • Hi there, I used GNU nano to make it so it should be good. I have tried re making the script several times but still get this..

    './test.sh: line 2: syntax error near unexpected token `$'do\r'''

    – Ben Robins Jun 26 '17 at 12:25
  • @BenRobins, you can test with od -ax if there is windows-style EOL. Or run dos2unix on the script – Romeo Ninov Jun 26 '17 at 12:29
  • 1
    Ah thanks, dos2unix seems to have solved that issue. Running the script gives the following errors.. – Ben Robins Jun 26 '17 at 12:37
  • convert: unable to open image `find . -type d/*jpg': No such file or directory @ error/blob.c/OpenBlob/2712. – Ben Robins Jun 26 '17 at 12:38
  • convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501. – Ben Robins Jun 26 '17 at 12:38
  • @BenRobins, check if you have backticks around find . -type d command – Romeo Ninov Jun 26 '17 at 12:41
  • Ah thank you. Script works wonderfully!

    It prints the following errors but still manages to successfully create the PDFs

    – Ben Robins Jun 26 '17 at 12:48
  • convert: unable to open image `./*png': No such file or directory @ error/blob.c/OpenBlob/2712. – Ben Robins Jun 26 '17 at 12:49
  • convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501. – Ben Robins Jun 26 '17 at 12:49
  • convert: no images defined `..pdf' @ error/convert.c/ConvertImageCommand/3210. – Ben Robins Jun 26 '17 at 12:49
  • @BenRobins, it can be edited to add png images also. But please edit your questions accordingly – Romeo Ninov Jun 26 '17 at 12:55
  • Apologies, I will edit my main question above. Thank you. – Ben Robins Jun 26 '17 at 13:05
  • 1
    The script could probably benefit from a shopt -s nullglob to stop "${i}/*jpg" from being substituted literally when there are no .jpg files in a particular directory (that's likely what's causing many of the errors) – steeldriver Jun 26 '17 at 13:13
  • @steeldriver, as I am not familiar with shopt feel free to edit the answer :) – Romeo Ninov Jun 26 '17 at 13:15
  • I have noticed that the script seems to change something about the images once in the PDF, I believe that it is resizing the images during the conversion?

    Would there be a way of making it so that it is simply binding all images into PDF without making any changes to the images at all?

    – Ben Robins Jun 26 '17 at 16:57