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

标签 django python-3.x xhtml2pdf

我正在尝试使用 xhtml2pdf 从 django 项目中的 html 模板生成 pdf。一切正常,但 Bootstrap 不工作。我正在学习本教程 https://www.codingforentrepreneurs.com/blog/html-template-to-pdf-in-django/

View .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 时使用此处加载 Bootstrap

最佳答案

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

Supported CSS properties by xhtml2pdf

关于django - 在 django 项目中使用 xhtml2pdf 从 html 生成 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55178778/

相关文章:

python - Django模板和XML问题

Django REST Framework ManyToMany 过滤多个值

python - 类型错误 : cannot unpack non-iterable NoneType object

Python 3 FancyURLopener cookie 处理

django - 比萨pdf转换器在使用大表时非常慢

python - 如何让它在使用xhtml2pdf时能正常显示全部内容?

Django - pdf 响应编码错误 - xhtml2pdf

django - 如果我的 Django 应用程序使用基于缓存的 session 后端,删除 `django_session` 表是否安全?

django request.GET 在模型中

python - PyQt6在QSlider的paintEvent中设置自定义矩形