Home > Software engineering >  Generating Multiple Documents (Python TempFile Module)
Generating Multiple Documents (Python TempFile Module)

Time:10-07

I am currently trying to print financial reports with my web app - this needs to happen by the click of a button. When I click the button, however, the app only prints 1 document (the first one) I don't understand what this could be as there are 2 different returns for each if statement that re-direct to 2 different .HTML pages

This is what I have tried at the moment:

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

The program only prints what the first if-statement shows and doesn't print the second .pdf document. Does anyone know what might be causing this?

CodePudding user response:

You are using return in your first if block.

Django generates a response and the code below return never runs (by the way it returns nothing, so you won't have a response even if pkForm.Trial_balance_Monthly == True.

Modern IDE, such as Pycharm will show you a colored warning here, but if you are not using such IDE you can trace what's happening in your code using a debugger.

To make such interaction as you've described (only one button and 2 reports) you need:

  1. Make 2 different endpoints for 2 reports
  2. Write JS handler that listens to button's click and call for a 2 enpoints. Handle response as usual.

Alternative scenario:

Make a temp zip file with two reports and create a response with only one zip file. Every OS has unzip feature, so your user will find this very intuitive. Here is zipfile documentation

  • Related