Как создать многостраничную накладную, используя python, шаблон Jinja в html - PullRequest
0 голосов
/ 27 апреля 2020

Я надеюсь, что кто-то может пролить свет на мою проблему, просто на голову, я новичок ie в HTML / CSS / JavaScript Разработка.

У меня есть сценарий Python, который заполняет в шаблоне JinJa HTML для создания счета. по этому счету у меня около 50 позиций. Создание счета работает. У меня есть одна HTML страница с заголовком, текстом (строки счета) и нижним колонтитулом. но я хочу создать многостраничную накладную с номерами страниц. Например, каждая страница должна содержать не более 15 позиций, затем снова создайте раздел «Верхний колонтитул», «Тело» и «Нижний колонтитул» и продолжайте до тех пор, пока позиции не будут завершены. Я могу создать несколько страниц с номерами страниц, но содержимое каждой страницы одинаково, и я не могу придумать, как этого добиться.

Мой python код:

from jinja2 import Environment, FileSystemLoader
import os
root = os.path.dirname(os.path.abspath(__file__))
templates_dir = os.path.join(root, 'templates')
env = Environment( loader = FileSystemLoader(templates_dir) )
template = env.get_template('invoice.html')
invoice_id = "BI-09994"

filename = os.path.join(root, 'html', invoice_id + '.html')
with open(filename, 'w') as fh:
fh.write(template.render(
    lines    = [("101211036", 'Volvo D13 FH12 Oil Filter', 3, "1000.00" , "3000.00"), ("101211037", 
'Volvo2', 1, "20.00", "20.00"), ("101211038", 'Volvo3', 1, "3000.00", "3000.00"),("101211036", 
'Volvo1', 3, "1000.00" , "3000.00"), ("101211037", 'Volvo2', 1, "20.00", "20.00"), ("101211038", 
'Volvo3', 1, "3000.00", "3000.00"),("101211036", 'Volvo1', 3, "1000.00" , "3000.00"), ("101211037", 
'Volvo2', 1, "20.00", "20.00"), ("101211038", 'Volvo3', 1, "3000.00", "3000.00"),("101211036", 
'Volvo1', 3, "1000.00" , "3000.00"), ("101211037", 'Volvo2', 1, "20.00", "20.00"), ("101211038", 
'Volvo3', 1, "3000.00", "3000.00"),("101211036", 'Volvo1', 3, "1000.00" , "3000.00"), ("101211037", 
'Volvo2', 1, "20.00", "20.00"), ("101211038", 'Volvo3', 1, "3000.00", "3000.00"),("101211036", 
'Volvo1', 3, "1000.00" , "3000.00"), ("101211037", 'Volvo2', 1, "20.00", "20.00"), ("101211038", 
'Volvo3', 1, "3000.00", "3000.00")],
     pages = [(1),(2),(3)],
    invoice_id = invoice_id,
    credted_date = "2020/04/22",
    due_date = "2020/04/29",
    comp_name = "BI - Applications",
    comp_addr1 = "305 Kerkenberg Avenue",
    comp_addr2 = "Villieria, Pretoria",
    comp_addr3 = "South - Africa, 0187",
    client_name = "Tecfinity (PTY) LTD",
    client_addr1 = "8 Osborn Lane",
    client_addr2 = "East Gate, Johannesburg",
    client_addr3 = "South - Africa, 0189",
    po_no = "Derick Mulder",
    ps_no = "BPS98007",
))

Мой код шаблона Jinja:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>{{invoice_id}}</title>

<style>
.invoice-box {
    max-width: 800px;
    margin: auto;
    padding: 30px;
    border: 1px solid #eee;
    box-shadow: 0 0 10px rgba(0, 0, 0, .15);
    font-size: 16px;
    line-height: 24px;
    font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;
    color: #555;

}

.invoice-box table {
    width: 100%;
    line-height: inherit;
    text-align: left;

}

.invoice-box table td {
    padding: 2px;
    vertical-align: middle;

}

.invoice-box table tr td:nth-child(2) {
    text-align: right;

}

.invoice-box table tr.top table td {
    padding-bottom: 20px;

}

.invoice-box table tr.top table td.title {
    font-size: 45px;
    line-height: 45px;
    color: #333;
}

.invoice-box table tr.information table td {
    padding-bottom: 40px;

}

.invoice-box table tr.heading td {
    background: #eee;
    border-bottom: 1px solid #ddd;
    font-weight: bold;
}

.invoice-box table tr.details td {
    padding-bottom: 20px;
}

.invoice-box table tr.item td{
    border-bottom: 1px solid #eee;

}

.invoice-box table tr.item.last td {
    border-bottom: none;
}

.invoice-box table tr.total td:nth-child(5) {
    border-top: 2px solid #eee;
    font-weight: bold;
    text-align: right;
}

@media only screen and (max-width: 600px) {
    .invoice-box table tr.top table td {
        width: 100%;
        display: block;
        text-align: center;
    }

    .invoice-box table tr.information table td {
        width: 100%;
        display: block;
        text-align: center;
    }
}

/** RTL **/
.rtl {
    direction: rtl;
    font-family: Tahoma, 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;
}

.rtl table {
    text-align: right;
    table-layout:fixed;
}

.rtl table tr td:nth-child(2) {
    text-align: left;
}
</style>
</head>
{% for page in pages %}
<body>
<div class="invoice-box">
    <table cellpadding="0" cellspacing="0">
        <tr class="top">
            <td colspan="5">
                <table>
                    <tr>
                        <td class="title">
                            <img src="https://www.bi-applications.co.za/wp-content/uploads/2020/04/Embliam-only.png" style="width:100%; max-width:150px;">
                        </td>

                        <td>

                            Invoice #: {{invoice_id}}<br>
                            Created: {{credted_date}}<br>
                            Due: {{due_date}}<br>
                            P/O: {{po_no}}<br>
                            P/S: {{ps_no}}
                        </td>
                    </tr>
                </table>
            </td>
        </tr>

        <tr class="information">
            <td colspan="5">
                <table>
                    <tr>
                        <td>
                            {{comp_name}}<br>
                            {{comp_addr1}}<br>
                            {{comp_addr2}}<br>
                            {{comp_addr3}}
                        </td>

                        <td>
                            {{client_name}}<br>
                            {{client_addr1}}<br>
                            {{client_addr2}}<br>
                            {{client_addr3}}
                        </td>
                    </tr>
                </table>
            </td>
        </tr>

        <tr class="top">
            <td colspan="5">
                <table>
                    <tr>
                        <td class="title" align="center">
                            TAX INVOICE
                        </td>

                    </tr>
                </table>
            </td>
        </tr>

        <tr class="heading">
            <td>
                Part No
            </td>

            <td style="text-align:left">
                Description
            </td>

            <td style="text-align:right">   
                Qty
            </td>

            <td style="text-align:right">
                Unit
            </td>

            <td style="text-align:right">
                Price
            </td>

        </tr>

        {% for stk in lines %}
        <tr class="item">
            <td >
                {{stk[0]}}
            </td>
            <td style="text-align:left; word-break:break-all;" >
                {{stk[1]}}
            </td>
            <td style="text-align:right" >
                {{stk[2]}}
            </td>
            <td style="text-align:right">
                {{stk[3]}}
            </td>
            <td style="text-align:right">
                {{stk[4]}}
            </td>

        </tr>
             {% endfor %}


        <tr class="total">
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td colspan="4">
               Total: R 38 500.00
            </td>
        </tr>

    </table>
        {{ page[0] }}
        Page : {{page}}
</div>

</body>

{% endfor %}
</html>

javaScript поможет мне с этим? например, при загрузке динамически создайте заголовок, начните заполнять позиции в al oop, пока не будет достигнуто максимальное количество строк на страницу, создайте раздел нижнего колонтитула, раздел заголовка и продолжайте с текстом

Надеюсь, что вышеприведенное имеет смысл

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...