I have currently generated a Trial Balance with Pastel Database items on a web page as per the below image.
I need to add a button that will be able to download the exact same thing onto a pdf document.
trb.html:
JavaScript
x
52
52
1
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">
2
{% extends "main/base.html"%}
3
4
{% block content%}
5
<h1>Kyle Database Trial Balance</h1>
6
<br>
7
<br>
8
<div class="container">
9
<div class="row mb-0">
10
11
<div class="col">
12
<h3>Account</h3>
13
<br>
14
{% for accountNo in accountNo %}
15
<p style="font-size:10px">{{ accountNo }}</p>
16
{% endfor %}
17
<br>
18
<b><p style='font-size:11px' style='font'>Totals</p></b>
19
</div>
20
21
<div class="col-5">
22
<h3>Description</h3>
23
<br>
24
{% for description in description %}
25
<p style="font-size:10px">{{ description }}</p>
26
{% endfor %}
27
</div>
28
29
<div class="col">
30
<h3>Debit</h3>
31
<br>
32
{% for debit in debit %}
33
<p style="font-size:10px">{{ debit }}</p>
34
{% endfor %}
35
<br>
36
<b><p style="font-size:11px">{{ totalDebit }}</p></b>
37
</div>
38
39
<div class="col">
40
<h3>Credit</h3>
41
<br>
42
{% for credit in credit %}
43
<p style="font-size:10px">{{ credit }}</p>
44
{% endfor %}
45
<br>
46
<b><p style="font-size:11px">{{ totalCredit }}</p></b>
47
</div>
48
49
</div>
50
</div>
51
{% endblock %}
52
Views.py:
JavaScript
1
34
34
1
def Kyletrb(request):
2
desc = "SELECT Description FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
3
4
cursor = cnxn.cursor();
5
cursor.execute(desc);
6
description = [tup[0] for tup in cursor.fetchall()]
7
8
accNo = "SELECT Account FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
9
10
cursor.execute(accNo);
11
accountNo = [tup[0] for tup in cursor.fetchall()]
12
13
deb = "SELECT Debit FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
14
15
cursor.execute(deb);
16
debit = [tup[0] for tup in cursor.fetchall()]
17
18
cred = "SELECT Credit FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
19
20
cursor.execute(cred);
21
credit = [tup[0] for tup in cursor.fetchall()]
22
23
Debtotal = "SELECT SUM(Debit) AS total FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
24
25
cursor.execute(Debtotal);
26
totalDebit = [tup[0] for tup in cursor.fetchall()]
27
28
Credtotal = "SELECT SUM(Debit) AS total FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
29
30
cursor.execute(Credtotal);
31
totalCredit = cursor.fetchall()
32
33
return render(request , 'main/Kyletrb.html' , {"description":description , "accountNo":accountNo , "debit":debit , "credit":credit , "totalDebit":totalDebit , "totalCredit":totalCredit})
34
What it needs to print:
If anybody has any way to assist with this or easy way to implement this button , please share.
Advertisement
Answer
I had a similar problem and this is how I solved I used xhtml2pdf library like this,
view
JavaScript
1
22
22
1
from xhtml2pdf import pisa
2
def render_pdf_view(request,*args,**kwargs):
3
pk=kwargs.get('pk')
4
#invoice=Invoice.objects.get(pk=pk)
5
invoice=get_object_or_404(Invoice,pk=pk)
6
receipt = Receipt.objects.filter(invoice=invoice.id)
7
for nn in receipt:
8
number=nn.id
9
template_name='template_name.html'
10
11
context={"invoice":invoice,"receipt":receipt,"number":number}
12
response=HttpResponse(content_type='application/pdf')
13
response['Content-Disposition']='filename="report.pdf"'
14
template=get_template(template_name)
15
html=template.render(context)
16
17
pisa_status=pisa.CreatePDF(html,dest=response)
18
19
if pisa_status.err:
20
return HttpResponse('we have some error',+html)
21
return response
22
In your HTML file. use the current one you have
This will generate a pdf