簡體   English   中英

xhtml2pdf沒有正確轉換css django

[英]xhtml2pdf not converting css properly django

嘿伙計們,所以我已經使用這個庫有一段時間了,我只是遇到了這個錯誤,通過使用這個庫,它沒有將我的 html 的 css 正確轉換為 Z437175BA4191210EE004E1D937494D09,是否有人使用此庫為我的客戶端生成憑證,現在我該如何解決這個問題? 謝謝,這是我的代碼

憑證.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>

視圖.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')

xhmtl2pdf 僅支持有限數量的 css 屬性,如文檔頁面中所述:

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

使用它的頁面和框架來定義布局而不是彈性列。

doc還說它有一些默認的頁面樣式,你可以像這里描述的那樣轉儲、編輯或替換:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM