在Django项目中使用xhtml2pdf从html生成pdf

时间:2019-03-15 08:53:51

标签: django python-3.x xhtml2pdf

我正在尝试使用xhtml2pdf从Django项目中的html模板生成pdf。一切正常,但引导程序不起作用。我正在关注本教程https://www.codingforentrepreneurs.com/blog/html-template-to-pdf-in-django/

Views.py

def generate_pdf(request, inflow_id):
    template = 'cashflow/invoice2.html'
    inflow = get_object_or_404(Inflow, pk=inflow_id)
    context = {
        "invoice_id": inflow.id,
        "customer_name": inflow.student,
        "amount": inflow.amount,
        "today": inflow.time,
        "fee_type": inflow.fee_type,
    }
    pdf = render_to_pdf(template, context)
    if pdf:
        response = HttpResponse(pdf, content_type='application/pdf')
        filename = "Invoice_%s.pdf" % inflow.student
        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")
utils.py中的

render_to_pdf函数

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

invoice2.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>duck</title>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-sm-4 col-md-3">
                <div class="card" style="width: 24rem;">
                  <img class="card-img-top" src='https://cdn.bulbagarden.net/upload/thumb/0/0d/025Pikachu.png/250px-025Pikachu.png' alt="Card image cap">
                  <div class="card-body">
                    <p class="card-text">Please enter all the fee payments here.</p>
                  </div>
                </div>
            </div>
            <div>
            <h1>Invoice id: {{ invoice_id }}</h1>
            <h2>Customer name: {{ customer_name }}</h2>
            <h3>Amount: {{ amount }}</h3>
            <h4>Time: {{ today }}</h4>
            <h5>Fee Type: {{ fee_type }}</h5>
        </div>
        </div>
    </div>
    <div class="container">
        <div class="row">
            <div class="well col-xs-10 col-sm-10 col-md-6 col-xs-offset-1 col-sm-offset-1 col-md-offset-3">
                <div class="row">
                    <div class="col-xs-6 col-sm-6 col-md-6">
                        <address>
                            <strong>Elf Cafe</strong>
                            <br>
                            2135 Sunset Blvd
                            <br>
                            Los Angeles, CA 90026
                            <br>
                            <abbr title="Phone">P:</abbr> (213) 484-6829
                        </address>
                    </div>
                    <div class="col-xs-6 col-sm-6 col-md-6 text-right">
                        <p>
                            <em>Date: 1st November, 2013</em>
                        </p>
                        <p>
                            <em>Receipt #: 34522677W</em>
                        </p>
                    </div>
                </div>
                <div class="row">
                    <div class="text-center">
                        <h1>Receipt</h1>
                    </div>
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Product</th>
                                <th>#</th>
                                <th class="text-center">Price</th>
                                <th class="text-center">Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td class="col-md-9"><em>Baked Rodopa Sheep Feta</em></h4></td>
                                <td class="col-md-1" style="text-align: center"> 2 </td>
                                <td class="col-md-1 text-center">$13</td>
                                <td class="col-md-1 text-center">$26</td>
                            </tr>
                            <tr>
                                <td class="col-md-9"><em>Lebanese Cabbage Salad</em></h4></td>
                                <td class="col-md-1" style="text-align: center"> 1 </td>
                                <td class="col-md-1 text-center">$8</td>
                                <td class="col-md-1 text-center">$8</td>
                            </tr>
                            <tr>
                                <td class="col-md-9"><em>Baked Tart with Thyme and Garlic</em></h4></td>
                                <td class="col-md-1" style="text-align: center"> 3 </td>
                                <td class="col-md-1 text-center">$16</td>
                                <td class="col-md-1 text-center">$48</td>
                            </tr>
                            <tr>
                                <td>   </td>
                                <td>   </td>
                                <td class="text-right">
                                <p>
                                    <strong>Subtotal: </strong>
                                </p>
                                <p>
                                    <strong>Tax: </strong>
                                </p></td>
                                <td class="text-center">
                                <p>
                                    <strong>$6.94</strong>
                                </p>
                                <p>
                                    <strong>$6.94</strong>
                                </p></td>
                            </tr>
                            <tr>
                                <td>   </td>
                                <td>   </td>
                                <td class="text-right"><h4><strong>Total: </strong></h4></td>
                                <td class="text-center text-danger"><h4><strong>$31.53</strong></h4></td>
                            </tr>
                        </tbody>
                    </table>
                    <button type="button" class="btn btn-success btn-lg btn-block">
                        Pay Now   <span class="glyphicon glyphicon-chevron-right"></span>
                    </button>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

请告诉我在生成pdf时可以在此处进行哪些更改或在其中加载引导程序

2 个答案:

答案 0 :(得分:0)

它不会在HTML内加载引导程序。我遇到了同样的问题-尝试在模板中加载静态-{% load static %}

在此示例中,我在styling标签中添加了<style>

{% load static %}
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Sales Report</title>
    <style type="text/css">
        @page {
            size: A4;
            margin: 1cm;
        }
        .table, td, th {  
        border-bottom: 1px solid #ddd;
        border-top: 1px solid #ddd;
        text-align: left;
        }

        .table {
        border-collapse: collapse;
        width: 100%;

        }

        .table th, td {
        padding: 5px;
        text-align: center;
        }

        .td{
        word-break: inherit;
        }

        .list-group h3{
            font-size: 3em;
        }
        .list-group p {
            font-size: 1em;
        }

        .table1 {
            width: 100%;
            max-width: 100%;
            margin-bottom: 5px;
            background-color: #fff;
            border: none;
            text-align: center;
        }

        .table1 td {
            border: none;
        }

    </style>
</head>
<body>

<div class="container">
    <div class="card">
        <table class="table1">
            <td><img src="https://professionalcipher.com/img/logos/django.png" alt="logo" /></td>
            <td>
                <div class="list-group">
                    <h3>Company Report</h3>
                    <p>Date - {% now "jS F Y H:i" %}</p>
                </div>
            </td>
        </table>
        <br/>
        <table class="table">
            <thead>
                <tr>
                  <th>
                    #
                  </th>
                  <th>
                    Company Name
                  </th>
                  <th>
                   Name
                  </th>
                  <th>
                    Email
                  </th>
                  <th>
                    Phone
                  </th>
                  <th>Status</th>
                  <th>Created Date </th>

                </tr>
              </thead>
              <tbody>
                {% for users in users %}
                <tr>
                  <td>
                      {{ forloop.counter }}
                  </td>
                  <td>
                    {{ users.userprofile.user_company }}

                 </td>

                  <td>
                        {{ users.first_name }} {{ users.last_name }}
                  </td>

                  <td>
                        {{ users.email }}

                  </td>
                  <td>{{ users.userprofile.user_phone }}</td>
                  <td>{{ users.userprofile.user_status }}</td>
                  <td>{{ users.userprofile.user_created |date:"M d, Y" }}</td>

                </tr>
                {% endfor %}
              </tbody>
        </table>
    </div>
</div>

</body>
</html>

希望这会对您有所帮助。

答案 1 :(得分:0)

因为xhtml2pdf支持有限数量的标准CSS属性。

Supported CSS properties by xhtml2pdf