首页 > 解决方案 > 以 django 格式附加邮件错误:不是 pdf 或损坏

问题描述

对于我的家庭作业,我正在尝试将附件 pdf 发送到电子邮件。我已经生成了要发送的 pdf。但是,当我下载 pdf 文件时出现格式错误:不是 pdf 或在文件打开时损坏。

render_to_pdf 的定义

实用程序.py

from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa

def render_to_pdf(template_src, context_dict={}):
    template = get_template(template_src)
    html  = template.render(context_dict)
    result = BytesIO()
    pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return None

pdf生成器函数

class GeneratePDF(View):
    context_object_name = 'services'
    template_name = 'pdf.html'
    def get(self, request, *args, **kwargs):
        template = get_template('configuration/pdf.html')
        f=Reservation.objects.all().filter(Q(valide=True, option__in=Option.objects.all().filter(Q(code_option='GTR', 
        posseder_niveau__in=Posseder_Niveau.objects.all().filter(niveau_id = 1))))).order_by('date_du_jour_reserve')
        c = Plage_Horaire.objects.all()

        context = {
            "c":c,
            'f':f,

        }
        html= template.render(context)
        pdf = render_to_pdf('configuration/pdf.html', context)
        if pdf:
            response = HttpResponse(pdf, content_type='application/pdf')
            filename = "emploidetemps.pdf" 
            content = "inline; filename=%s " %filename
            download = request.GET.get("download")
            if download:
                content = "attachment; filename=%s" %(filename)
            response['Content-Disposition'] = content
            return response
        return HttpResponse("Not found")

邮件功能

def mailjoin(request):

    GeneratePDF.as_view()
    email = EmailMessage()
    email.subject = "test"
    email.body='emploi de temps'
    email.from_email = settings.EMAIL_HOST_USER
    email.to = ['xxxxxxxx@gmail.com' ]
    email.attach("emploidetemps.pdf", 'application/pdf')
    email.send()

标签: django

解决方案


首先 -GeneratePDF.as_view()返回一个可调用的视图,而不是 pdf 文件调用 -

file = GeneratePDF.as_view()(request)

其次,您GeneratePDF返回HttpResponse,您必须保存 pdf,然后添加完整路径,或者不要使用它。

如果您不保存,请执行以下操作 -

def mailjoin(request):
    template = get_template('configuration/pdf.html')
        f=Reservation.objects.all().filter(Q(valide=True, option__in=Option.objects.all().filter(Q(code_option='GTR', 
        posseder_niveau__in=Posseder_Niveau.objects.all().filter(niveau_id = 1))))).order_by('date_du_jour_reserve')
        c = Plage_Horaire.objects.all()
        context = {
            "c":c,
            'f':f,
        }
    html= template.render(context)
    pdf = render_to_pdf('configuration/pdf.html', context)
    email = EmailMessage()
    email.subject = "test"
    email.body='emploi de temps'
    email.from_email = settings.EMAIL_HOST_USER
    email.to = ['xxxxxxxx@gmail.com' ]
    email.attach('emploidetemps.pdf', pdf, 'application/pdf')
    email.send()

推荐阅读