[Django]-Generate pdf from html using xhtml2pdf in django project

2👍

Because xhtml2pdf supports limited numbers of standard CSS properties.

Supported CSS properties by xhtml2pdf

0👍

It will not load the bootstrap inside your html. I had the same issue – Try to load static in your template – {% load static %}.

Here is the example where i have added styling in <style> tag.

{% 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>

Hope this will help you.

👤Cipher

Leave a comment