【发布时间】:2021-09-29 10:28:06
【问题描述】:
我目前正在尝试使用我的网络应用程序打印财务报告 - 这需要通过单击按钮来实现。但是,当我单击该按钮时,该应用程序仅打印 1 个文档(第一个文档) p>
这是我目前尝试过的:
import tempfile
def printReports(request , reports_pk):
pkForm = get_object_or_404(SettingsClass , pk=reports_pk)
complexName = pkForm.Complex
if pkForm.Trial_balance_Year_To_Date == True:
### Printing Trial Balance PDF
response = HttpResponse(content_type= 'application/pdf')
response['Content-Disposition']= 'attachment; filename=TrialBalance' + \
str(datetime.now()) + '.pdf'
response['Content-Transfer-Encoding'] = 'binary'
content = {"x_AlltrbYTD":x_AlltrbYTD , 'xCreditTotal':xCreditTotal , 'xDebitTotal':xDebitTotal , 'complexName':complexName , 'openingBalances': openingBalances ,'printZero':printZero , 'printDesc':printDesc , 'printAcc':printAcc}
html_string=render_to_string('main/reports/trialBalanceYear.html' , content)
html=HTML(string=html_string)
result=html.write_pdf()
with tempfile.NamedTemporaryFile(delete=True) as output:
output.write(result)
output.flush()
output.seek(0)
response.write(output.read())
return response
if pkForm.Trial_balance_Monthly == True:
### Printing Trial Balance PDF
response = HttpResponse(content_type= 'application/pdf')
response['Content-Disposition']= 'attachment; filename=TrialBalanceMonthly' + \
str(datetime.now()) + '.pdf'
response['Content-Transfer-Encoding'] = 'binary'
content = {"xtrbMonth":xtrbMonth , 'xCreditTotalM':xCreditTotalM , 'xDebitTotalM':xDebitTotalM , 'complexName':complexName , 'printZeroM':printZeroM}
html_string=render_to_string('main/reports/trialBalanceMonthly.html' , content)
html=HTML(string=html_string)
result=html.write_pdf()
with tempfile.NamedTemporaryFile(delete=True) as output:
output.write(result)
output.flush()
output.seek(0)
response.write(output.read())
else:
printTrialBalanceMonth = False
程序只打印第一个 if 语句显示的内容,而不打印第二个 .pdf 文档。有谁知道这可能是什么原因造成的?
【问题讨论】:
标签: python django pdf httpresponse temporary-files