如何在谷歌应用引擎中创建PDF格式的收据文件?

时间:2015-02-17 14:48:15

标签: python google-app-engine reportlab

我正在使用谷歌应用引擎(python)创建一个在线商店。 我需要创建一个简单的pdf收据并发送给客户。

请推荐我最简单的方法。 我已经尝试过reportlab,但是我在导入reportlab中遇到了问题:

  2015-02-17 20:36:43.152
Traceback (most recent call last):

  File "/base/data/home/apps/s~xxx/1.382295241559540129/kay/app.py", line 361, in get_response
    view_func = import_string(view_func)

  File "/base/data/home/apps/s~xxx/1.382295241559540129/kay/lib/werkzeug/utils.py", line 422, in import_string
    __import__(modname)

  File "/base/data/home/apps/s~xxx/1.382295241559540129/myapp/views.py", line 48, in <module>
    from reportlab.lib import colors

  File "/base/data/home/apps/s~xxx/1.382295241559540129/myapp/reportlab/lib/colors.py", line 43, in <module>
    from reportlab import isPy3

ImportStringError: import_string() failed for 'myapp.views.order1'. Possible reasons are:

- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;

Debugged import:

- 'myapp' found in '/base/data/home/apps/s~xxx/1.382295241559540129/myapp/__init__.pyc'.
- 'myapp.views' not found.

Original exception:

ImportError: No module named reportlab
W 2015-02-17 20:36:43.153
500: import_string() failed for 'myapp.views.order1'. Possible reasons are:

- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;

Debugged import:

- 'myapp' found in '/base/data/home/apps/s~xxx/1.382295241559540129/myapp/__init__.pyc'.
- 'myapp.views' not found.

Original exception:

ImportError: No module named reportlab
I 2015-02-17 20:36:43.157
Saved; key: __appstats__:003000, part: 24 bytes, full: 1598 bytes, overhead: 0.000 + 0.003; link: http://xxx.appspot.com/_ah/stats/details?time=1424173003069

2 个答案:

答案 0 :(得分:2)

我们使用xhtml2pdf包中的pisa。

使用pip install xhtml2pdf -t .将其安装到项目目录中(它也应安装所有依赖项,但您应该检查)。

from xhtml2pdf import pisa
output = StringIO()  # we cannot use filesystem on gae, so write it in the memory
pdf = pisa.CreatePDF('<strong>Hello, world!</strong>', output, encoding='utf-8')
pdf_data = pdf.dest.getvalue()
print pdf_data

答案 1 :(得分:1)

Reportlab很好,看起来你的问题是包应该在你的根目录中,与app.yaml一起,它将处理导入错误。

您可以获取源代码here,请记住仅复制reportlab下名为src的目录。

Here's a simple tutorial,第一个例子应该没​​有任何问题,如果&#34;安装&#34;做得很好。