How to show print diagloue box in browser in Django?

I am trying to get the print dialogue box in my Django app. Currently its downloading in my local computer. But when I am serving in apache server, its not working.

my views.py

def download(request,lothash):
    lot=lotnumber.objects.get(lot_hash=lothash)
    postdata = request.POST
    array = postdata.get("array", None).split(",")
    template=lot.template_type
    f = open(f"/home/username/public_html/nitin2/media/{template.template_file}", "r")
    txt=(f.read())
    df=pd.read_excel("/home/username/public_html/nitin2/media/"+str(lot.file))
    df = df.reset_index()
    n=1
    k=0
    for index, row in df.iterrows():
        if n>len(array)+1:
            break

        if str(n) in array:
            txt = txt.replace("(As Provided by Bank)", str(row['RGN_A']))
            txt = txt.replace("(Customer Name)", str(row['Applicant Name']))
            txt = txt.replace("(Customer Address)", str(str(row['Address1']) + " " + str(row['Address2']) + " " + str(row['Address3'])))
            txt = txt.replace("(Product)", str(row['Product']))
            txt = txt.replace("(LAN)", str(row['LAN']))
            txt = txt.replace("(Notice Amount)", str(row['Notice Amount']))
            
            # save FPDF() class into a
            # variable pdf
            pdf = FPDF()
            
            # Add a page
            pdf.add_page()
            
            # set style and size of font
            # that you want in the pdf
            pdf.set_font("Arial", size = 12)
            
            # insert the texts in pdf
            newtxt=splitlinesoftext(txt)
            for x in newtxt:
                pdf.multi_cell(0, 5, txt = x, align = 'L',border=0)
            
           
            downloads_path = str(Path('/home/username/public_html/nitin2/media/') / "Downloads/nitin").replace('\\','/')
            if not os.path.isdir(f"{downloads_path}/{str(row['Applicant Name'])}"):
                os.makedirs(f"{downloads_path}/{str(row['Applicant Name'])}")

            pdf.output(f"{downloads_path}/{str(row['Applicant Name'])}/document.pdf", 'I')  

            #pdf.output(f'document.pdf', 'D')  

            template=lot.template_type
            f = open(f"/home/username/public_html/nitin2/media/{template.template_file}", "r")
            txt=(f.read())

        k=k+1
        n=n+1

    return JsonResponse({'context': "done"})

I want now to show download dialogue box. Currently if i change -

`

 # save the pdf with name .pdf
            downloads_path = str(Path('/home/username/public_html/nitin2/media/') / "Downloads/nitin").replace('\\','/')
            if not os.path.isdir(f"{downloads_path}/{str(row['Applicant Name'])}"):
                os.makedirs(f"{downloads_path}/{str(row['Applicant Name'])}")

            pdf.output(f"{downloads_path}/{str(row['Applicant Name'])}/document.pdf", 'I') 

`

to

** pdf.output(f'document.pdf', 'D')**

Its not working. Now showing anything on browser..

Also if I change the path to path.home downloads_path = str(Path.home() / "Downloads/nitin").replace('\\','/')

its showing me permission error /var/www/downlaods/

Back to Top