2

编辑. 找到了解决方案:似乎 pdf 文档必须是 base64 编码的。像这样编辑异步视图:

import base64
result = export_pdf(request)
encoded = base64.b64encode(result.getvalue())
email = mandrill.Mandrill(settings.MANDRILL_API_KEY)
email.messages.send_template(mandrill_template, [], {'to':[{'email':user.email}], 'subject':subject, 'text':message, attachments':[{'type':'application/pdf', 'name':'pdf file', 'content':encoded}] })

编辑结束

我在发送使用 xhtml2pdf/pisa 创建的 pdf 文档作为 Mandrill 中的附件时遇到问题。该应用程序的工作方式如下:一个 content_view 调用一个async_viewwhich

  1. 生成 pdf(通过调用 export_pdf 函数)和
  2. 发送带有附件的电子邮件。

pdf 文档似乎有效(如果我修改async_viewHttpResponsepdf 而不是发送它,我会看到一个有效的 pdf 文档)。但是将它发送到 Mandrill 会返回一个编码错误:

UnicodeDecodeError:“utf8”编解码器无法解码位置 11 中的字节 0x93:无效的起始字节。

我尝试按照此处提到的方式解决它(Python: Sanitize a string for unicode?)。在这种情况下,不再引发错误,但我作为电子邮件附件获得的 pdf 被视为“文本/纯文本”并且不可读。谢谢您的帮助!

异步视图

pdf_doc = export_pdf(request).getvalue() # .decode("windows-1252").encode("utf-8", 'ignore') would not raise an error but would be considered as plain text
email = mandrill.Mandrill(settings.MANDRILL_API_KEY)
email.messages.send_template(mandrill_template, [], {'to':[{'email':user.email}], 'subject':subject, 'text':message, attachments':[{'type':'application/pdf', 'name':'pdf file', 'content':pdf_doc}] })

# return HttpResponse(pdf_doc,  mimetype='application/pdf') would return a well formatted pdf

export_pdf 函数

def export_pdf(request):

    html  = render_to_string('pdf.html', { 'pagesize' : 'A4', }, context_instance=RequestContext(request,{
                'text': text,
            },))
    result = cStringIO.StringIO()       
    pdf = pisa.pisaDocument(StringIO.StringIO(html), dest=result, encoding='UTF-8')
    if not pdf.err:
        return result
    else:
        return None

回溯的有趣部分是

(...)
File "/home/raphael/Applications/Virtualenvs/apps/local/lib/python2.7/site-packages/mandrill.py", line 1476, in send_template
    return self.master.call('messages/send-template', _params)
File "/home/raphael/Applications/Virtualenvs/apps/local/lib/python2.7/site-packages/mandrill.py", line 137, in call
    params = json.dumps(params)
File "/home/raphael/Applications/Virtualenvs/apps/local/lib/python2.7/site-packages/simplejson/__init__.py", line 354, in dumps
    return _default_encoder.encode(obj)
File "/home/raphael/Applications/Virtualenvs/apps/local/lib/python2.7/site-packages/simplejson/encoder.py", line 262, in encode
    chunks = self.iterencode(o, _one_shot=True)
File "/home/raphael/Applications/Virtualenvs/apps/local/lib/python2.7/site-packages/simplejson/encoder.py", line 340, in iterencode
    return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x93 in position 11: invalid start byte
4

1 回答 1

2

找到了解决方案:似乎 pdf 文档必须是 base64 编码的。像这样编辑异步视图:

import base64
result = export_pdf(request)
encoded = base64.b64encode(result.getvalue())
email = mandrill.Mandrill(settings.MANDRILL_API_KEY)
email.messages.send_template(mandrill_template, [], {'to':[{'email':user.email}], 'subject':subject, 'text':message, attachments':[{'type':'application/pdf', 'name':'pdf file', 'content':encoded}] })
于 2016-02-26T14:32:06.540 回答