【问题标题】:attach img file in pdf weasyprint在 pdf weasyprint 中附加 img 文件
【发布时间】:2017-09-18 06:41:28
【问题描述】:

我需要有关在 pdf 中附加 img 文件的帮助。我们使用 Wea​​syPrint 库从 html 生成 pdf。

在html中像这样连接img文件

<img src="1.png" alt="">
<img src="2.png" alt="">
<img src="3.png" alt="">

但它不起作用。我看不到图片。

【问题讨论】:

    标签: python django pdf-generation weasyprint


    【解决方案1】:

    图片文件路径使用静态

      {% load static %}
        <img src="{% static 'images/static.jpg' %}" alt="">
    

    并在views.py的HTML类中传递base_url

    pdf_file = HTML(string=rendered_html, base_url=request.build_absolute_uri())
    

    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>
    

    views.py

    from django.template.loader import get_template
    from weasyprint import HTML, CSS
    from django.conf import settings
    from django.http import HttpResponse
    
    
    def generate_pdf(request):
        html_template = get_template('latest/html_pdf.html')
        user = request.user
        rendered_html = html_template.render().encode(encoding="UTF-8")
        pdf_file = HTML(string=rendered_html, base_url=request.build_absolute_uri()).write_pdf(stylesheets=[CSS(settings.STATIC_ROOT +  '/css/generate_html.css')])
    
    
    
        http_response = HttpResponse(pdf_file, content_type='application/pdf')
        http_response['Content-Disposition'] = 'filename="generate_html.pdf"'
    
        return http_response
    

    【讨论】:

    • NameError: 全局名称“请求”未定义
    • 你必须在views.py文件的任何函数中调用这个文件,因为你只有在views.py文件中有请求实例。否则你必须使用一些第三方包或其他东西
    • 如果您只是想确保图像包含在纯 python 中并且您不使用 django 或模板做任何事情,您所需要的只是 base_url 如果您在本地工作,则可以是 .
    • 并非在所有情况下都有效:Weasyprint 将获取 url,但当远程图像受密码保护或仅需要身份验证时,Weasyprint 将无法下载图像!
    【解决方案2】:

    如果发送带有 pdf 附件的电子邮件,可以将路径从视图传递到专用于电子邮件的功能。 views.py

    [...]
    path = request.build_absolute_uri()          # build absolute path
    order_confirmation.delay(order.id, path)     # pass to func
    [...]
    

    tasks.py

    @app.task
    def order_confirmation(order_id, path):       # receive path
        order = Order.objects.get(id=order_id)
        subject = f"Order nr. {order.id}"
        email_from = settings.EMAIL
        email_to = order.get_email
        message = (...)
        email = EmailMessage(subject, message, email_from, [email_to])
        html = render_to_string('pdf.html', {'order' : order, 'company': company})
        out = BytesIO()
        stylesheets=[weasyprint.CSS(settings.STATIC_ROOT + '/css/pdf.css')]
        weasyprint.HTML(string=html, base_url=path).write_pdf(out, stylesheets=stylesheets)      # use here
        email.attach(f'order_{order.id}.pdf',
            out.getvalue(),
            'application/pdf')
        email.send()
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多