Вывод элементов базы данных в документ pdf

В настоящее время я создал пробный баланс с элементами базы данных Pastel на веб-странице, как показано ниже.

Мне нужно добавить кнопку, которая сможет загрузить то же самое в документ pdf.

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})

Что нужно напечатать:

введите описание изображения здесь

Если у кого-то есть способ помочь с этим или простой способ реализовать эту кнопку, пожалуйста, поделитесь.

У меня была похожая проблема, и вот как я ее решил. Я использовал библиотеку xhtml2pdf следующим образом,

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

В вашем HTML файле. используйте текущий, который у вас есть

Это создаст pdf

Вернуться на верх