I have been following this tutorial which helped me in generating pdf files in Django using xhtml2pdf. Now what I want is to save the generated file to disk without having to prompt the user to download the file.

Below is the code I am using to generate the pdf file in utils.py file and views.py file.

#utils.py file

from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa

def render_to_pdf(template_src, context_dict={}):

    template = get_template(template_src)
    html  = template.render(context_dict)
    result = BytesIO()
    pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return None 

#views.py snippet of code

html = template.render(context)
pdf = render_to_pdf('tdn.html', context)
if pdf: 
    response = HttpResponse(pdf, content_type='application/pdf')
    filename = "TDN_%s.pdf" %("12341231")
    content = "inline; filename='%s'" %(filename)
    download = request.GET.get("download")
    if download:
        content = "attachment; filename='%s'" %(filename)
        response['Content-Disposition'] = content
    return response

return HttpResponse("Not found")

Any pointers on how I can write to disk the generated pdf will be greatly appreciated