Django httpResponse open pdf then print window in navigator

I'm running a django app in which i created a view to open a pdf file in the browser for printing purpose.

Until this, everything works fine. For a better UX, i'm trying to automatically open the print window (specific to each computer). However, i'm struggling to do so. Aas long as the new window open a pdf but not a html file , i cannot insert JS in my code...

Any idea to do so ?

Here is my code (i deleted the intermediate steps where i combine some pdfs or add blank page for recto/verso printing...)

class CombinePdf (View):

def post(self, request, *args, **kwargs):
        
        response = HttpResponse(content_type='application/pdf')
        response['Content-Transfer-Encoding'] = 'binary'
        file = open("my_pdf.pdf", 'rb')

        # generate pdf in response
        try:
            file.write(response)
        except Exception as e: 
            return e
        

        #open pdf in new window
        return response

Thanks for your help !

Back to Top