hey guys so i have been using this library for a while now and i just encounter this error that by using this library its not converting my css of my html properly to pdf, i used this library to generate voucher internet for my client, does anybody now how can i fix this? thanks, here's my code
voucher.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')
xhmtl2pdf supports only limited number of css properties as described in the docs page:
And adds few of its own for defining its own styling:
Namely it does not accept
flex,border-collapseandborder-spacingfrom your code.Use its pages and frames to define layout instead of flex columns.
Also doc says that it has some default styling for the page, which you may dump, edit or replace like described here: