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:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous"> {% extends "main/base.html"%} {% block content%} <h1>Kyle Database Trial Balance</h1> <br> <br> <div class="container"> <div class="row mb-0"> <div class="col"> <h3>Account</h3> <br> {% for accountNo in accountNo %} <p style="font-size:10px">{{ accountNo }}</p> {% endfor %} <br> <b><p style='font-size:11px' style='font'>Totals</p></b> </div> <div class="col-5"> <h3>Description</h3> <br> {% for description in description %} <p style="font-size:10px">{{ description }}</p> {% endfor %} </div> <div class="col"> <h3>Debit</h3> <br> {% for debit in debit %} <p style="font-size:10px">{{ debit }}</p> {% endfor %} <br> <b><p style="font-size:11px">{{ totalDebit }}</p></b> </div> <div class="col"> <h3>Credit</h3> <br> {% for credit in credit %} <p style="font-size:10px">{{ credit }}</p> {% endfor %} <br> <b><p style="font-size:11px">{{ totalCredit }}</p></b> </div> </div> </div> {% endblock %}
Views.py:
def Kyletrb(request): desc = "SELECT Description FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''" cursor = cnxn.cursor(); cursor.execute(desc); description = [tup[0] for tup in cursor.fetchall()] accNo = "SELECT Account FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''" cursor.execute(accNo); accountNo = [tup[0] for tup in cursor.fetchall()] deb = "SELECT Debit FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''" cursor.execute(deb); debit = [tup[0] for tup in cursor.fetchall()] cred = "SELECT Credit FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''" cursor.execute(cred); credit = [tup[0] for tup in cursor.fetchall()] Debtotal = "SELECT SUM(Debit) AS total FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''" cursor.execute(Debtotal); totalDebit = [tup[0] for tup in cursor.fetchall()] Credtotal = "SELECT SUM(Debit) AS total FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''" cursor.execute(Credtotal); totalCredit = cursor.fetchall() return render(request , 'main/Kyletrb.html' , {"description":description , "accountNo":accountNo , "debit":debit , "credit":credit , "totalDebit":totalDebit , "totalCredit":totalCredit})
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
from xhtml2pdf import pisa def render_pdf_view(request,*args,**kwargs): pk=kwargs.get('pk') #invoice=Invoice.objects.get(pk=pk) invoice=get_object_or_404(Invoice,pk=pk) receipt = Receipt.objects.filter(invoice=invoice.id) for nn in receipt: number=nn.id template_name='template_name.html' context={"invoice":invoice,"receipt":receipt,"number":number} response=HttpResponse(content_type='application/pdf') response['Content-Disposition']='filename="report.pdf"' template=get_template(template_name) html=template.render(context) pisa_status=pisa.CreatePDF(html,dest=response) if pisa_status.err: return HttpResponse('we have some error',+html) return response
In your HTML file. use the current one you have
This will generate a pdf