首页 > 解决方案 > pdf格式输出分散在django中

问题描述

您好我想使用创建发票WeasyPrint。我想得到格式良好的打印作为发票的输出。

我正在使用以下函数来创建 pdf:

from weasyprint import CSS, HTML

def render_to_pdf(context, file_name):
    file_data = render_to_string('order_detail.html', context)
    html = HTML(string=file_data).write_pdf(
        target='collections/%s' % (file_name),
        stylesheets = [CSS(string='body { font-size: 7px }')]
    )
    fs = FileSystemStorage("")
    with fs.open("collections/" + file_name) as pdf:
        response = HttpResponse(pdf, content_type="application/pdf")
        response['Content-Disposition'] = '"attachment; filename="%s"'% (file_name)
        print("pdf working")
        return response

在我使用的最新版本中,stringformat但之前我尝试使用普通的 jinja 模板并且输出是相同的。这是我的html:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Email</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    <style>
        .container {
            border: 1px solid #585696;
            padding: 0;
        }
        .header {
            background-color: #585696;
            padding: 1.5rem;
        }
        h1.text-center {
            color: #fff;
            text-align: center;
        }
        .text-center {
            text-align: center;
            color: #585696;
        }
        .text-content {
            padding: 2rem;
        }
        .order-header{
            color: #585696;
            margin-top: 1rem;
        }
        .table-stripped{
            width: 100%;
            border: 1px solid #585696;
        }
        .title-bar {
            background-color: #585696;
            color: #fff;
        }
        th, td {
            text-align: center;
            border-bottom: 1px solid #585696;
        }
        tr {
            border-bottom: 1px solid #585696;
        }
        .address-time {
            margin-top: 0;
        }
        .footer{
            margin: 1rem auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header text-center">
            <h1 class="text-white text-center"> Order Confirmation [ # {{order.id|stringformat:"i" }}]</h1>
        </div>

        <div class="text-content">
            <h4 class="text-user">
                <strong> Dear {{ order.user.email }}, </strong>
                <br>
                <p>
                    We received your order and are now being processed. Your order details are shown below for your reference.
                </p>
            </h4>
            <h1 class="order-header">
                <strong> Your Order: #{{ order.id|stringformat:"i" }} </strong>
            </h1> 

            <table class="table-stripped">
                <thead class="title-bar">
                    <th> Sr. no. </th>
                    <th> Product Name </th>
                    <th> Rate </th>
                    <th> Quantity </th>
                    <th> Amount </th>
                </thead>

                <tbody>
                    {% for item in orders %}
                    <tr style="border-bottom: 1px solid #585696;">
                        <td> {{ forloop.counter|stringformat:"i" }} </td>
                        <td> {{ item.product.name }} </td>
                        <td> $ {{ item.price|stringformat:".2f" }} </td>
                        <td> {{ item.quantity|stringformat:"i" }} </td>
                        <td> $ {{ item.line_cost|stringformat:".2f" }} </td>
                    </tr>
                    {% endfor %}
                    <tr class="title-bar" style="border-bottom: 1px solid #585696;">
                        <td></td>
                        <th> Cart Total </th>
                        <td></td>
                        <td></td>
                        <th> $ {{ order.total_cost }} </th>
                    </tr>
                    <tr style="border-bottom: 1px solid #585696;">
                        <td></td>
                        <th>  Shipping Charges </th>
                        <td></td>
                        <td></td>
                        <th> $ {{ order.shipping_charges }} </th>
                    </tr>
                    {% if discounts %}
                    <tr style="border-bottom: 1px solid #585696;">
                        <td></td>
                        <th>  Discounts </th>
                        <td></td>
                        <td></td>
                        <th> $ {{ total_doscount }} </th>
                    </tr>
                    {% endif %}
                    <tr class="title-bar" style="border-bottom: 1px solid #585696;">
                        <td></td>
                        <th>  Total </th>
                        <td></td>
                        <td></td>
                        <th> $ {{ order.total_cost }} </th>
                    </tr>
                </tbody>
            </table>

            <h1 class="order-header"> Customer Details </h1>

            <table class="table-stripped">
                <thead class="title-bar">
                    <th> Name </th>
                    <th> Phone Number </th>
                    <th> Payment Method </th>
                </thead>

                <tbody>
                    <tr>
                        <td> {{ order.first_name }} {{ order.last_name }} </td>
                        <td> {{ order.user.phone_number }} </td>
                        <td> {{ order.payment_method }} </td>
                    </tr>
                </tbody>
            </table>

            <h1 class="order-header"> Address and Delivery </h1>

            <table class="table-stripped">
                <thead class="title-bar">
                    <th> Billing Address </th>
                    <th> Shipping Address </th>
                    <th> Date and Time </th>
                </thead>

                <tbody>
                    <tr>
                        <td> {{ order.billing_address }} </td>
                        <td> {{ order.shipping_address }} </td>
                        <td> {{ order.delivery_datetime }} </td>
                    </tr>
                </tbody>
            </table>

            <h3 class="notes order-header"> Notes: </h3>
            <p> {{ order.notes }}</p>

            <div class="footer">
                <p> If you have any questions, contact us at care@estreetmart.sg or call us on +65 69781677 </p>
                <h3 class="text-center"> Thank you for purchasing at <a href="https://www.estreetmart.sg"> https://www.estreetmart.sg </a></h3>
            </div>

        </div>
    </div>
</body>
</html>

这是我得到的pdf的示例输出: 我得到的样本输出

是否有任何技巧或库可用于在 django 中获取 pdf 输出?请任何帮助清理此 pdf 文件将不胜感激。谢谢。

标签: python-3.xdjangopdfdjango-rest-frameworkweasyprint

解决方案


推荐阅读