diff --git a/Dockerfile b/Dockerfile index 0a0b057d..c8a0ed59 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,8 +2,10 @@ FROM openlabs/docker-wkhtmltopdf:latest MAINTAINER Sharoon Thomas # Install dependencies for running web service -RUN apt-get install -y python-pip -RUN pip install werkzeug executor gunicorn +RUN apt-get update \ + && apt-get install -y python-pip \ + && pip install werkzeug executor gunicorn \ + && rm -rf /var/lib/apt/lists/* ADD app.py /app.py EXPOSE 80 diff --git a/app.py b/app.py index 273f15b6..5458b7e1 100644 --- a/app.py +++ b/app.py @@ -7,6 +7,7 @@ """ import json import tempfile +import os from werkzeug.wsgi import wrap_file from werkzeug.wrappers import Request, Response @@ -26,7 +27,8 @@ def application(request): request_is_json = request.content_type.endswith('json') - with tempfile.NamedTemporaryFile(suffix='.html') as source_file: + workdir=os.environ.get('WKHTMLTOPDF_WORKDIR') + with tempfile.NamedTemporaryFile(suffix='.html', dir=workdir, delete=True) as source_file: if request_is_json: # If a JSON payload is there, all data is in the payload @@ -42,8 +44,10 @@ def application(request): source_file.flush() # Evaluate argument to run with subprocess - args = ['wkhtmltopdf'] - + args = [] + + env_options = os.environ.get('WKHTMLTOPDF_OPTS') or '' + # Add Global Options if options: for option, value in options.items(): @@ -55,13 +59,18 @@ def application(request): file_name = source_file.name args += [file_name, file_name + ".pdf"] - # Execute the command using executor - execute(' '.join(args)) + try: + # Execute the command using executor + execute('wkhtmltopdf ' + env_options + ' ' + ' '.join(args)) + + pdf = open(file_name + '.pdf') + return Response( + wrap_file(request.environ, pdf), + mimetype='application/pdf', + ) + finally: + os.remove(file_name + '.pdf') - return Response( - wrap_file(request.environ, open(file_name + '.pdf')), - mimetype='application/pdf', - ) if __name__ == '__main__':