将数据库项目输出到 pdf 文档

Outputting database items to a pdf document

我目前已经按照下图在网页上生成了带有 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 库,

查看

    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