将生成的Pdf上传到Django模型

时间:2019-05-17 10:18:31

标签: django python-3.x django-models pdf-generation

我目前正在构建一个Web应用程序,其中使用reportlab库从Django模型中生成了pdf数据,并将其下载为HttpResponse(content_type ='application / pdf'),并且该程序正常运行。现在它要从视图返回,我想将其上传到Django模型,而不是下载它。有可能请帮我解决有关我的“毕业设计”的问题吗?我将非常感谢您。

我目前正在使用Django最新更新和reportlab库来生成pdf。

下面是生成pdf并将其重新创建为HttpResponse的函数。

def format1(personal_info,education,skills,experience,statement,languages,user):
    education_list = list()
    skills_list = list()
    experience_list = list()
    for i in education:
        temp_dict = dict()
        temp_dict["degree"] = i.Degree
        temp_dict["institute"] = i.Institute
        temp_dict["city"] = i.City
        temp_dict["grade"] = i.Grade 
        education_list.append(temp_dict)
    for i in skills:
        temp_dict = dict()
        temp_dict["skill"] = i.Skill_Title
        temp_dict["level"] = i.Level 
        temp_dict["description"] = i.Description 
        skills_list.append(temp_dict)
    for i in experience:
        temp_dict = dict()
        temp_dict["job"] = i.Job_Title
        temp_dict["organization"] = i.Organization
        temp_dict["city"] = i.City
        temp_dict["timeperiod"] = i.TimePeriod 
        experience_list.append(temp_dict)
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment;filename=' + user + ".pdf"    
    c = canvas.Canvas(response, pagesize=letter)
    width,height = letter
    # profile information
    c.setFont("Helvetica-Bold",22)
    c.drawString(0.5*inch,height-0.7*inch,personal_info[0].FullName)
    c.setFont("Helvetica",12)
    c.drawString(0.5*inch,height-0.9*inch,personal_info[0].PermanentAddress)
    c.drawString(0.5*inch,height-1.1*inch,personal_info[0].Email)
    c.drawString(0.5*inch,height-1.3*inch,personal_info[0].Phone)
    # horizontal line
    c.line(0.5*inch,height-1.5*inch,width-0.5*inch,height-1.5*inch)
    # education section
    c.setFont("Helvetica-Bold",18)
    c.drawString(0.5*inch,height-2*inch,"Education")
    count = 2.0
    for i in education_list:
        c.setFont("Helvetica-Bold",12)
        c.drawString( (width-0.5*inch)/2,height-(count)*inch, i['degree'] )
        c.setFont("Helvetica",12)
        c.drawString( (width-0.5*inch)/2,height-(count+0.2)*inch, i['institute'] + ", " + i['city'] )
        c.drawString( (width-0.5*inch)/2,height-(count+0.4)*inch, i['grade'] )
        count = count + 0.7
    # horizontal line
    c.line(0.5*inch,height-(count+0.2)*inch,width-0.5*inch,height-(count+0.2)*inch)
    count = count + 0.7
    # skills section
    c.setFont("Helvetica-Bold",18)
    c.drawString(0.5*inch,height-count*inch,"Skills")
    c.setFont("Helvetica",12)
    index = math.ceil(len(skills_list)/2)
    for i in range(math.ceil(len(skills_list)/2)):
        try:
            c.drawString( (width-0.5*inch)/2,height-(count)*inch,skills_list[i]['skill'])
            c.drawString( ((width-0.5*inch)/2) + inch*2,height-(count)*inch,skills_list[index]['skill'])
            index = index + 1
        except:
            c.drawString( (width-0.5*inch)/2,height-(count)*inch,skills_list[i]['skill'])
        count = count + 0.2
    # horizontal line
    c.line(0.5*inch,height-(count+0.2)*inch,width-0.5*inch,height-(count+0.2)*inch)
    count = count + 0.7
    # experience section
    c.setFont("Helvetica-Bold",18)
    c.drawString(0.5*inch,height-count*inch,"Work Experience")
    for i in experience_list:
        c.setFont("Helvetica-Bold",12)
        c.drawString( (width-0.5*inch)/2,height-(count)*inch, i['job'] )
        c.setFont("Helvetica",12)
        c.drawString( (width-0.5*inch)/2,height-(count+0.2)*inch, i['organization'] + ", " + i['city'] )
        c.drawString( (width-0.5*inch)/2,height-(count+0.4)*inch, i['timeperiod'] )
        count = count + 0.7
    # horizontal line
    c.line(0.5*inch,height-(count+0.2)*inch,width-0.5*inch,height-(count+0.2)*inch)
    # ----------------------------------------
    c.showPage()
    c.save()
    return response

下面是我的Django模型,我要在其中上传生成的pdf

class Application(models.Model):
    job = models.ForeignKey(Posts,on_delete=models.CASCADE)
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    status = models.BooleanField(default=False,blank=False)
    cv = models.FileField(upload_to="pdf-files")
    def __str__(self):
        return self.job.organization.Name

下面是我尝试将生成的pdf上传到模型中的代码

response = format1(person,education,skills,experience,statement,langs,username)
        file = ContentFile(response.content)
        print(file) # output = Raw Content
        obj = models.Application(user=user,job=job,cv=file)
        obj.save() 

我想将生成的pdf上传到该模型中,但是结果是运行该代码后该模型中的空白字段。

0 个答案:

没有答案
相关问题