【问题标题】:Attach img file in PDF Django Weasyprint在 PDF Django Weasyprint 中附加 img 文件
【发布时间】:2017-06-23 11:34:27
【问题描述】:

朋友们好

我想请你帮忙。

我需要帮助在 pdf 中附加 img 文件,我们使用 Wea​​syPrint 从 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>

tasks.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()

感谢您的帮助

【问题讨论】:

    标签: python django pdf weasyprint


    【解决方案1】:

    我解决了这个问题,方法是用 base64 编码图像并通过上下文发送到模板,然后用 CSS 渲染。你可以在使用 HTML 之前放置你的 uri 定义

    所以,我的任务是这样的:

        from django.template.loader import get_template
        from weasyprint import HTML
    
        with open("path/to/image.png", "rb") as image_file:
            encoded_string = base64.b64encode(image_file.read())               
    
        context = {..., "img":encoded_string , ...}
    
        html_template = get_template('my_template_to_PDF.html')  
        html = html_template.render(context).encode(encoding="UTF-8")
        uri = request.build_absolute_uri()
        pdf_file = HTML(string=html,base_url=uri,encoding="UTF-8").write_pdf('path/to/output.pdf')
    

    模板:

    <style>
    ...
    div.image {
      width:150px;
      height:50px;
      background-image:url(data:image/png;base64,{{img}});
    }
    ...
    </style>
    ...
    <div class = 'image'></div>
    ...
    

    希望对你有帮助!

    【讨论】:

    • 太棒了!让我知道使用您的方法是否方便。
    • 感谢您的解决方案。图像未显示“weasyprint 方式”。我只是在发送到上下文之前添加了 encoded_string.decode()。
    猜你喜欢
    • 2017-09-18
    • 2016-12-23
    • 1970-01-01
    • 2021-08-23
    • 2023-03-07
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多