title: Using ReportLab To Create A PDF Then Save It To A User Model
body: <p>For a few days Ive been trying to get this working and, while I think im getting close, I still havent got it working yet.</p>
  
  <p>I have a function which uses ReportLab to generate a SimpleDocTemplate pdf and I want to return it to the calling view so I can save it to the uesrs Profile model.</p>
  
  <p>This is my code so far:</p>
  
  <p>model.py</p>
  
  <pre><code>class Profile(models.Model):
      user = models.OneToOneField(User, on_delete=models.CASCADE)
      profile_pdf = models.FileField(upload_to='pdfs/user_pdfs')
  </code></pre>
  
  <p>the error message:</p>
  
  <pre><code>'HttpResponse' object has no attribute 'read'
  </code></pre>
  
  <p>view</p>
  
  <pre><code>def final_question(request):
      if request.method == 'POST':
          # this is the final form they complete
          form = FinalQuestionForm(request.POST, request.FILES, instance=request.user.finalquestion)
  
          if form.is_valid():
              form.save()
  
              # generate pdf containing all answers to enrolment questions
              users_pdf = utils.generate_user_pdf(request)
  
              # here i get the users model
              # hard coded user_id for testing
              users_profile = Profile.objects.get(user_id=1)
              # then i get the field I want to save to
              pdf = users_profile.profile_pdf
              # then i save the generated pdf to the field
              pdf.save('user_1_pdf', users_pdf)
  </code></pre>
  
  <p>my pdf generation function</p>
  
  <pre><code>def generate_user_pdf(request):
      response = HttpResponse(content_type='application/pdf')
      response['Content-Disposition'] = 'attachment; filename = "test.pdf"'
  
      doc = SimpleDocTemplate(response)
  
      elements = []
  
      data= [
                   ['---TEST DATA---'],
                   ['test data'],
            ]
  
      [.. cont ..]
  
      doc.build(elements)
  
      return response
  </code></pre>
  
  <p>Thank you.</p>
  
  <p>--EDIT--</p>
  
  <p>error:</p>
  
  <pre><code>argument should be a bytes-like object or ASCII string, not 'FileResponse'
  </code></pre>
  
  <p>utils:</p>
  
  <pre><code>def generate_pdf(request):
      buffer = io.BytesIO()
  
      doc = SimpleDocTemplate('test')
      story = []
  
      doc.build(story)
  
      response = FileResponse(buffer.getvalue())
  
      return response
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code># generate pdf containing all answers
              user_pdf = utils.generate_pdf(request)
  
              # get users enrolment_pdf field
              # temp hard-coded
              user_profile = Profile.objects.get(user_id=1)
              field = user_profile.user_pdf
  
              # save the generated pdf to the user in the database
              file_data = ContentFile(base64.b64decode(user_pdf))
              field.save('test', file_data)
  </code></pre>
  
  <p>Thank you</p>