简体   繁体   English

下载 pdf 文件 Django

[英]Download pdf file Django

views.py视图.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) .下载 function 使用 API 将页面转换为 pdf 使用 URL。pdf 保存在项目文件夹中,但我希望使用HttpResponse下载它,如果可能的话不保存在文件夹中,而是代码pdf=file.write(response.content)中的变量实例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 + '"'

you can return file like this你可以像这样返回文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM