0

views.py

The download function using an API to convert a page to pdf using the URL. The pdf is saved in the project folder but i want it to be downloaded using HttpResponse and if possible not saved in the folder but a variable instance in code pdf=file.write(response.content).

def downloadpdf(request, feedback_id):
    apiKey = '*****************'
    resume = Personal_Details.objects.get(feedback_id=feedback_id)
    response = requests.post(
    'https://api.restpdf.io/v1/pdf',
    headers = {
        'X-API-KEY'   : apiKey,
        'content-type': 'application/json'
    },
    json = {
        "output": "data",
        "url": "https://github.com/chryzcode"
    }   
    )
                                    
    if response.status_code == 200:
        with open(f'{resume.resume_name}.pdf', 'wb') as file:
            file.write(response.content)
            return redirect('Resume', feedback_id=feedback_id)

    else:
        print("There was an error converting the PDF")
                                    

enter image description here

chryzcode
  • 49
  • 2
  • 9
  • Does this answer your question? [How to send file to response in Django?](https://stackoverflow.com/questions/50479804/how-to-send-file-to-response-in-django) – sahasrara62 Jan 26 '23 at 15:35

1 Answers1

0
 response = HttpResponse(pdf, content_type='application/pdf')
 response['Content-Disposition'] = 'attachment; filename="' + filename + '"'

you can return file like this