Скачать pdf файл Джанго
views.py
Функция загрузки использует API для преобразования страницы в pdf по URL. pdf сохраняется в папке проекта, но я хочу, чтобы он загружался с помощью HttpResponse
и, если возможно, не сохранялся в папке, а был переменной в коде 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")
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="' + filename + '"'
вы можете вернуть файл следующим образом