2013-10-28 4 views
0

나는 피사로 생성 된 pdf를 이메일에 첨부하는 방법을 알아 내려고하고있다. 이전에는 버퍼를 사용하여 첨부 파일을 만들 수 있었지만 바로 그 때 바로 reportlab을 사용했습니다.django : 첨부 피사 이메일을 PDF로 생성

def pdfgenerate(request): 
    # Create the HttpResponse object with the appropriate PDF headers. 
    response = HttpResponse(content_type='application/pdf') 
    response['Content-Disposition'] = 'filename="invoicex.pdf"' 

    buffer = BytesIO() 

    # Create the PDF object, using the BytesIO object as its "file." 
    p = canvas.Canvas(buffer) 

    # Draw things on the PDF. Here's where the PDF generation happens. 
    # See the ReportLab documentation for the full list of functionality. 
    p.drawString(100, 100, "Hello world.") 

    # Close the PDF object cleanly. 
    p.showPage() 
    p.save() 

    # Get the value of the BytesIO buffer and write it to the response. 
    pdf = buffer.getvalue() 
    buffer.close() 

    email = EmailMessage('Hello', 'Body', '[email protected]', ['[email protected]']) 
    email.attach('invoicex.pdf', pdf , 'application/pdf') 
    email.send() 
    return HttpResponseRedirect(request.META.get('HTTP_REFERER')) 

이것은 내가 가지고있는 코드가, 피사를 사용하여, 지금까지입니다 : 내가 변환 된 PDF로 그 개념을 적용하는 방법을 알아낼 수 없습니다

이것은 단순히 reportlab를 사용하여 할 것입니다 방법입니다 생성 된 PDF :

def render_to_pdf(request, template_src, context_dict): 
    template = get_template(template_src) 
    context = Context(context_dict) 
    html = template.render(context) 
    result = StringIO.StringIO() 

    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result) 
    if not pdf.err: 
     response = HttpResponse(result.getvalue(), mimetype='application/pdf') 
     response['Content-Disposition'] = 'filename="invoicex.pdf"' 
     email = EmailMessage('Hello', 'Body', '[email protected]', ['[email protected]']) 
     email.attach('invoicex.pdf', pdf , 'application/pdf') 
     email.send() 
     return HttpResponseRedirect(request.META.get('HTTP_REFERER')) 
    return HttpResponse('We had some errors<pre>%s</pre>' % escape(html)) 

def labelsend(request, order_id): 
    labels = LabelOrder.objects.get(LabelOrderID=order_id) 
    args = {} 

    args['labels'] =labels 

    return render_to_pdf(request, 'labelsforprint.html', args) 

답변

1

당신이 필요로하는 result.getvalue() 하지 PDF

email.attach('invoicex.pdf', result.getvalue() , 'application/pdf')