xhtml2pdf未正确转换css Django

2024-10-16 20:46:41 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,伙计们,我使用这个库已经有一段时间了,我刚刚遇到了一个错误,使用这个库没有正确地将我的html的css转换为pdf,我使用这个库为我的客户生成网上代金券,现在有人知道我如何解决这个问题吗?谢谢,这是我的密码

凭证.html

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <style type="text/css">
        
        * {
        box-sizing: border-box;
        }

        .row {
        display: flex;
        margin-left:-5px;
        margin-right:-5px;
        }

        .column {
        flex: 50%;
        padding: 5px;
        }

        table {
        border-collapse: collapse;
        border-spacing: 0;
        width: 100%;
        border: 1px solid #ddd;

        }

        th, td {
        text-align: left;
        padding: 16px;
        text-align: center;
        }

        tr:nth-child(even) {
        background-color: #f2f2f2;
        }
    @page {
        size: letter landscape;
        margin: 2cm;
    }
        </style>
    </head>
    <body>
        <div class="row">
        {% for data in akun %}
            <div class="column">
                <table>
                    <tr>
                        <th colspan="2">Voucher Internet 1 Hari</th>
                    </tr>
                    <tr>
                        <td>Username</td>
                        <td>{{akun.username}}</td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td>{{akun.password}}</td>
                    </tr>
                    <tr>
                        <td>Harga</td>
                        <td>{{akun.harga}}</td>
                    </tr>
                    <tr>
                        <td colspan="2">Mikadmin.net</td>
                    </tr>
                </table>
            </div>
        {% endfor %}
        </div>
    </body>
    </html>

view.py

    def voucher_profile(request):
        mikrotik_session = request.session.get("mikadmin")
        template = get_template('voucher.html')
        host     = mikrotik_session.get("host")
        username = mikrotik_session.get("username")
        password = mikrotik_session.get("password")
        con  = routeros_api.RouterOsApiPool(host=host,username=username,password=password,plaintext_login=True)
        api  = con.get_api()
        content = api.get_resource('ip/hotspot/user/profile')
        content_user = api.get_resource('ip/hotspot/user')
        username_paket_profile = request.POST['name-profile']
        valid = request.POST['valid']
        limit_bandwidth = request.POST['rate-limit']
        harga_voucher = request.POST['harga-voucher']
        count_generate = request.POST['count_generate']
        akun = []
        content.add(
            name=username_paket_profile,
            shared_users="1",
            rate_limit=limit_bandwidth,
            status_autorefresh="1m",
            transparent_proxy="yes",
            on_login="""{
            :local pengguna $user;
            :local date [/system clock get date];
            :local time [/system clock get time];
            :log warning "$pengguna telah login pada jam $time";
            :if ([/ip hotspot user find name=$pengguna limit-uptime=%s]="") do={
            /ip hotspot user set [find name=$pengguna] limit-uptime=%s
            /ip hotspot active remove [find user=$pengguna]
            };
            :if ([/system schedule find name=$pengguna]="") do={
            /system schedule add name=$pengguna interval=%s on-event="/ip hotspot user remove [find name=$pengguna]\r\n/ip hotspot active remove [find user=$pengguna]\r\n/system schedule remove [find name=$pengguna]"
            }
            }""" % (valid, valid, valid))

        for data in range(int(count_generate)):
            generate_name = "".join(random.choice(string.ascii_letters) for x in range(6))
            generate_password = "".join(random.choice(string.ascii_letters) for x in range(6))
            content_user.add(name=generate_name,password=generate_password,profile=username_paket_profile)
            ctx = {"username":generate_name,"password":generate_password,"harga":harga_voucher}
            akun.append(ctx)

        context = {"akun":akun}
        html  = template.render(context)
        nama_file = 'voucher paket generate.pdf'
        file = open(nama_file, "w+b")
        pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file,
                encoding='utf-8')

        file.seek(0)
        pdf = file.read()
        file.close()            
        return HttpResponse(pdf, 'application/pdf')

Tags: nameipgetrequesthtmlusernamepasswordprofile
1条回答
网友
1楼 · 发布于 2024-10-16 20:46:41

xhmtl2pdf只支持数量有限的css属性,如docs page中所述:

background-color
border-bottom-color, border-bottom-style, border-bottom-width
border-left-color, border-left-style, border-left-width
border-right-color, border-right-style, border-right-width
border-top-color, border-top-style, border-top-width
colordisplay
font-family, font-size, font-style, font-weight
height
line-height, list-style-type
margin-bottom, margin-left, margin-right, margin-top
padding-bottom, padding-left, padding-right, padding-top
page-break-after, page-break-before
size
text-align, text-decoration, text-indent
vertical-align
white-space
width
zoom

并添加了一些用于定义自己样式的功能:

-pdf-frame-border
-pdf-frame-break
-pdf-frame-content
-pdf-keep-with-next
-pdf-next-page
-pdf-outline
-pdf-outline-level
-pdf-outline-open
-pdf-page-break

也就是说,它不接受代码中的flexborder-collapseborder-spacing

使用它的pages and frames to define layout而不是flex列

doc还表示,它有一些页面的默认样式,您可以转储、编辑或替换这些样式,如here所述:

# dump
$ xhtml2pdf  css-dump > xhtml2pdf-default.css
# replace
$ xhtml2pdf  css=xhtml2pdf-default.css test.html

相关问题 更多 >