在Google App Engine中使用pdfkit生成PDF会引发Popen的AttributeError

时间:2016-11-09 12:25:18

标签: python google-app-engine pdf flask pdfkit

我正在尝试使用Flask中的pdfkit生成PDF。 pdfkit在本地工作正常,但是当我在Google App Engine中使用它时会引发以下错误。

Traceback (most recent call last):
  File "C:\Python27\flaskapp\flask\app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python27\flaskapp\flask\app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python27\flaskapp\flask\app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python27\flaskapp\flask\app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Python27\flaskapp\main.py", line 17, in yout
    pdf = pdfkit.from_string(my_string,False , configuration=config)
  File "C:\Python27\flaskapp\pdfkit\api.py", line 68, in from_string
    return r.to_pdf(output_path)
  File "C:\Python27\flaskapp\pdfkit\pdfkit.py", line 92, in to_pdf
    result = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
AttributeError: 'module' object has no attribute 'Popen'
from flask import Flask, make_response, request, render_template
import pdfkit

path_wkthmltopdf = r'C:\Python27\flaskapp\wkhtmltopdf\bin\wkhtmltopdf.exe'

config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
app = Flask(__name__)

@app.route('/pdf', methods=['POST', 'GET'])
def pdf():
    if request.method == 'POST':
        my_string = request.form['text11']
        pdf = pdfkit.from_string(my_string, False, configuration=config)

        response = make_response(pdf)
        response.headers['Content-Type'] = 'application/pdf'
        response.headers['Content-Disposition'] = 'inline; filename=out.pdf'

        return response

    return render_template('pdff.html')

1 个答案:

答案 0 :(得分:3)

此代码不会在Google App Engine上运行,因为App Engine的Python runtime environment不允许调用subprocess.Popen或任何其他启动其他进程的调用。