Login page getting downloaded as pdf instead of the other page url.

20 views
Skip to first unread message

Aadil Rashid

unread,
Feb 6, 2022, 9:52:27 AM2/6/22
to Django users

Hello Django Family, I hope you all are well and in good Health. 

I am trying to generating pdf from webpages, using popen subprocess , but when i call its

view it generates only login page as pdf,


Here is my view


from projName.settings import BASE_DIR

from subprocess import Popen, PIPE, STDOUT

from wsgiref.util import FileWrapper



class DownloadAsPDF(View):

     def post(self, request, *args, **kwargs):

          base = str(BASE_DIR)

          file_name = str( base + '/testreport.pdf')

          post_url = request.POST.get("page_url")

          url = str('http://' + request.get_host() + post_url)

          chrome_args = ['/path/google-chrome', '--headless', '--virtual-time-buget=20000',

                    '--print-to-pdf=/path/testreport.pdf', URL]

          external_process = Popen(chrome_args, stdout=PIPE, stderr=STDOUT)

          external_process.wait()

          return_file = FileWrapper(open('{}'.format(file_name), 'r'))

          download_file_name = 'report'

          response = FileResponse(return_file, content_type='application/force-download')

          response['Content-Disposition'] = f'attachment; filename=                         
                    {download_file_name}.pdf'

          return response


I was thinking of creating the popen subprocess in the same browser session so that it will directly go to the said url page, instead of login page, 

I couldn't find any good resources to implement this functionality, as it is headless chrome here may be it's not possible to open in the same browser session. 


If this can't be the perfect solution, then please if anyone have some other solution I will be very thankful,

Thanks you very much

Reply all
Reply to author
Forward
0 new messages