django - 以 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()
解决方案
首先 -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()
推荐阅读
- node.js - Mongoose:另一个嵌套数组元素内的数组更新未按预期工作
- sql - 如何从许多表中找到交集的组合?
- python - 获取特定年龄的平均值
- javascript - 等效的 Regex 不支持的后向断言 IOS Safari
- snowflake-cloud-data-platform - 您是否可以使用 AWS Glue 获得永久 IP 地址,以便将其列入 Snowflake 中的白名单?
- html - 添加 div 时,Bootstrap 4 元素不对齐
- postman - 如何在 Postman 中使用不同的变量值运行端点测试?
- stm32 - GNU链接器(ld)中包含的“当前目录”搜索路径?
- c++ - 在 SYCL 中为 2D std::vector 创建缓冲区
- python - 无法在 discord.py 中将 Embed 类型对象作为参数传递