diff --git a/Dockerfile b/Dockerfile index 2a18842..173e2af 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,26 +1,24 @@ FROM debian:10-slim -LABEL maintainer="inf_sfb1288@lists.uni-bielefeld.de" +LABEL authors="Patrick Jentsch , Stephan Porada " ENV LANG=C.UTF-8 -RUN mkdir /usr/share/man/man1/ # needed to install pdftk https://github.com/geerlingguy/ansible-role-java/issues/64 + RUN apt-get update \ && apt-get install -y --no-install-recommends \ - ca-certificates \ imagemagick \ python3.7 \ - pdftk \ - zip + zip \ + && rm -r /var/lib/apt/lists/* -RUN rm -rf /var/lib/apt/lists/* -RUN rm -f /etc/ImageMagick-6/policy.xml +RUN cat /etc/ImageMagick-6/policy.xml && rm /etc/ImageMagick-6/policy.xml + COPY file-setup /usr/local/bin -COPY policy.xml /etc/ImageMagick-6 ENTRYPOINT ["file-setup"] diff --git a/file-setup b/file-setup index 2e71378..8832cc4 100755 --- a/file-setup +++ b/file-setup @@ -3,20 +3,21 @@ """ -merge_images - -Usage: For usage instructions run with option --help -Author: Stephan Porada +file-setup +Usage: For usage instructions run with option --help +Authors: Patrick Jentsch """ from argparse import ArgumentParser import os +import re import subprocess def parse_arguments(): - parser = ArgumentParser(description='Merges images into one .pdf file.') + parser = ArgumentParser(description='Merge images (JPEG, PNG or TIFF) into one PDF file.') parser.add_argument('-i', '--input-directory', help='Input directory', required=True) @@ -31,45 +32,37 @@ def parse_arguments(): return parser.parse_args() +def natural_sorted(iterable): + """ Sort the given list in the way that humans expect. + """ + convert = lambda text: int(text) if text.isdigit() else text + alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)] + return sorted(iterable, key=alphanum_key) + + def merge_images(input_dir, output_dir, output_file_base, zip): try: os.mkdir(output_dir) - tmp_dir = os.path.join(input_dir, 'tmp') - os.mkdir(tmp_dir) except FileExistsError: pass - try: - tmp_dir = os.path.join(input_dir, 'tmp') - os.mkdir(tmp_dir) - except FileExistsError: - pass - # Sort filenames into a list ordered with version flag -v - cmd = 'ls -Q -v "{i}"/*.* > "{i}"/file_list.txt'.format(i=input_dir) - subprocess.run(cmd, shell=True) - # Convert all image files into pdf files - cmd = ('mogrify -compress LZW -format pdf ' - + '-path "{}" @"{}"/file_list.txt'.format(tmp_dir, input_dir)) - subprocess.run(cmd, shell=True) - # remove file list - cmd = 'rm "{}"/file_list.txt'.format(input_dir) - subprocess.run(cmd, shell=True) - # join all pdfs into one pdf - cmd = ('pdftk "{}"/*.pdf cat '.format(tmp_dir) - + 'output "{}"/"{}".pdf').format(output_dir, output_file_base) - subprocess.run(cmd, shell=True) - # remove single pdf files - cmd = 'rm -r "{}"'.format(tmp_dir) + files = filter(lambda x: x.lower().endswith(('.jpg', '.jpeg', '.png', '.tif', '.tiff')), + os.listdir(input_dir)) + files = natural_sorted(files) + files = map(lambda x: os.path.join(input_dir, x), files) + output_file = os.path.join(output_dir, '{}.pdf'.format(output_file_base)) + # Convert input files to a single PDF + cmd = 'convert "{}" "{}"'.format('" "'.join(files), output_file) subprocess.run(cmd, shell=True) # zip stuff if zip is not None: # Remove .zip file extension if provided - if zip.endswith('.zip'): + if zip.lower().endswith('.zip'): zip = zip[:-4] zip = zip if zip else 'output' cmd = 'cd "{}"'.format(output_dir) cmd += ' && ' cmd += 'zip' - cmd += ' "{}".zip "{}".pdf'.format(zip, output_file_base) + cmd += ' "{}.zip" "{}.pdf"'.format(zip, output_file_base) cmd += ' && ' cmd += 'cd -' subprocess.run(cmd, shell=True) diff --git a/policy.xml b/policy.xml deleted file mode 100644 index f5ef390..0000000 --- a/policy.xml +++ /dev/null @@ -1,87 +0,0 @@ - - - - - - - - -]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/wrapper/file-setup b/wrapper/file-setup index 0cdfaf2..7dad642 100755 --- a/wrapper/file-setup +++ b/wrapper/file-setup @@ -29,4 +29,5 @@ if args.input_directory is not None: remaining_args.insert(0, '-i') cmd.append(CONTAINER_IMAGE) cmd += remaining_args + subprocess.run(cmd)