大家好
我想请你帮忙。
我需要帮助在 pdf 中附加 img 文件,我们使用 WeasyPrint 从 html 生成 pdf。
我不知道在哪里添加base_url=request.build_absolute_uri()
到tasks.py。在文件 tasks.py 中的什么位置?
现在代替图像的是空的地方。
我尽我所能,但我没有成功。所以请帮忙。
.html 文件
<!DOCTYPE html>
<html lang="en">
{% load static %}
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>
<img src="{% static 'images/static.jpg' %}" alt="">
</div>
</body>
</html>
任务.py
from celery import task
from django.shortcuts import get_object_or_404
from oferty.models import Order
from django.template.loader import render_to_string
from django.core.mail import EmailMultiAlternatives
from django.conf import settings
import weasyprint
from weasyprint import default_url_fetcher, HTML
from io import BytesIO
@task
def order_created(order_id):
"""
Task to send an e-mail notification when an order is successfully created.
"""
order = Oferta.objects.get(id=order_id)
subject = 'xxx nr {}'.format(order.id)
html_content = '<p><strong>Hallow, {}!</p></strong><br><p>
email = EmailMultiAlternatives(subject,
html_content,'admin@xxx.com',
[order.email])
# generation PDF.
html = render_to_string('order/order/pdf.html', {'order': order})
out = BytesIO()
stylesheets = [weasyprint.CSS(settings.STATIC_ROOT + 'css/pdf.css')]
weasyprint.HTML(string=html).write_pdf(out,
stylesheets=stylesheets)
# attach PDF.
email.attach('order_{}.pdf'.format(order.id),
out.getvalue(),
'application/pdf')
email.attach_alternative(html_content, "text/html")
email.send()
我会很感激你的帮助