import base64
import json
import tempfile
from werkzeug.wrappers import Request, Response
from executor import execute


@Request.application
def application(request):
    """
    To use this application, the user must send a POST request with
    base64 encoded HTML content and the wkhtmltopdf Options in
    request data, with keys 'base64_html' and 'options'.
    The application will return a response with the PDF file.
    """
    if request.method != 'POST':
        return

    with tempfile.NamedTemporaryFile(
        suffix='.html', prefix='trytond_', delete=False
    ) as source_file:
        options = json.loads(request.form['options'])
        file_name = source_file.name
        source_file.write(base64.b64decode(request.form['base64_html']))
        source_file.close()

        # Evaluate argument to run with subprocess
        args = '/usr/local/bin/wkhtmltopdf.sh'
        # Add Global Options
        if options:
            for option, value in options.items():
                args += ' --%s' % option
                if value:
                    args += ' "%s"' % value

        # Add source file name and output file name
        args += ' %s %s.pdf' % (file_name, file_name)
        # Execute the command using executor
        execute(args)
        return Response(base64.b64encode(open(file_name + '.pdf').read()))

if __name__ == '__main__':
    from werkzeug.serving import run_simple
    run_simple(
        '127.0.0.1', 5000, application, use_debugger=True, use_reloader=True
    )

@@ -1,41 +1,272 @@
-import base64%0Aimport json%0Aimport temp
+#! /usr/bin/env python%0A%22%22%22%0A    WSGI APP to convert wkhtmltopdf As a webservice%0A%0A    :copyright: (c) 2013 by Openlabs Technologies & Consulting (P) Limited%0A    :license: BSD, see LICENSE for more details.%0A%22%22%22%0Aimport json%0Aimport tempfile%0A%0Afrom werkzeug.wsgi import wrap_
 file
@@ -479,16 +479,32 @@
  base64 
+or form encoded 
 encoded 
@@ -723,16 +723,77 @@
 return%0A%0A
+    request_is_json = request.content_type.endswith('json')%0A%0A
     with
@@ -821,25 +821,16 @@
 aryFile(
-%0A        
 suffix='
@@ -839,106 +839,273 @@
 tml'
-, prefix='trytond_', delete=False%0A    ) as source_file:%0A        options = json.loads(request.form%5B
+) as source_file:%0A%0A        if request_is_json:%0A            # If a JSON payload is there, all data is in the payload%0A            payload = json.loads(request.data)%0A            source_file.write(payload%5B'contents'%5D.decode('base64'))%0A            options = payload.get(
 'opt
@@ -1109,17 +1109,20 @@
 options'
-%5D
+, %7B%7D
 )%0A      
@@ -1127,36 +1127,80 @@
     
-file_name = source_file.name
+elif request.files:%0A            # First check if any files were uploaded
 %0A   
@@ -1196,32 +1196,36 @@
 ploaded%0A        
+    
 source_file.writ
@@ -1230,81 +1230,174 @@
 ite(
-base64.b64decode(request.form%5B'base64_html'%5D))%0A        source_file.close(
+request.files%5B'file'%5D.read())%0A            # Load any options that may have been provided in options%0A            options = json.loads(request.form.get('options', '%7B%7D')
 )%0A%0A 
@@ -1461,16 +1461,17 @@
  args = 
+%5B
 '/usr/lo
@@ -1493,16 +1493,18 @@
 opdf.sh'
+%5D%0A
 %0A       
@@ -1615,22 +1615,25 @@
     args
- += ' 
+.append('
 --%25s' %25 
@@ -1638,16 +1638,17 @@
 %25 option
+)
 %0A       
@@ -1690,22 +1690,25 @@
     args
- += ' 
+.append('
 %22%25s%22' %25 
@@ -1712,16 +1712,17 @@
  %25 value
+)
 %0A%0A      
@@ -1779,32 +1779,54 @@
     
-args += ' %25s %25s.pdf' %25 (
+file_name = source_file.name%0A        args += %5B
 file
@@ -1841,17 +1841,27 @@
 ile_name
-)
+ + %22.pdf%22%5D%0A
 %0A       
@@ -1918,13 +1918,24 @@
 ute(
+' '.join(
 args)
+)%0A
 %0A   
@@ -1959,58 +1959,125 @@
 nse(
-base64.b64encode(open(file_name + '.pdf').read()))
+%0A            wrap_file(request.environ, open(file_name + '.pdf')),%0A            mimetype='application/pdf',%0A        )%0A
 %0A%0Aif

Make the app more flexible