PDF generation with xhtml2Pdf in Django

35 views
Skip to first unread message

Oduwa Imade

unread,
May 11, 2023, 9:09:06 AM5/11/23
to Django users
Hello Guys! I am trying to generate a Pdf doc(reading data from a database) on Django with xhtml2pdf library. but i encounter this error when i hit the 'Create PDF' button: 

ValueError at /RecruitmentApp/createcvpdf
<PmlTable@0x1B92DB31D50 23 rows x 3 cols> with cell(0,0) containing
'<PmlKeepInFrame at 0x1b92db92e90> size=x' data error - 23 rows in data but 22 row heights .
 ......am not sure what the error is saying or what am doing wrong. Can anyone assist please with ideas on how to tackle this issue?

This is the method in the views.py that handles the pdf creation:

def createPdf(requst):
          #jobseekerID = request.session['rec_ID']
          jobseekerID = 2
         
          cv = CV.objects.select_related('f_key_REC').get(f_key_REC = jobseekerID)
          skill = Skill.objects.select_related('fk_sCV').get(fk_sCV = cv.id)
          qualification = Qualification.objects.select_related('fk_qCV').get(fk_qCV=cv.id)
          certification = Certification.objects.select_related('fk_cCV').get(fk_cCV = cv.id)
          experience = Experience.objects.select_related('fk_eCV').filter(fk_eCV = cv.id)

          template_path = 'Recruitment_App/createpdf_cv.html'
          context = {
               "cv": cv,
               "skills" : skill,
               "qualifications": qualification,
               "certification": certification,
               "experience": experience
          }
          print (cv)
          print(skill)
          print(experience)
          response = HttpResponse(content_type='application/pdf')
          response['Content-Disposition'] = 'filename="cv001.pdf"'
          template = get_template(template_path)
          html = template.render(context)

          pisa_status = pisa.CreatePDF(html, dest=response)
          print(template)
          if pisa_status.err:
               return HttpResponse('We had some errors <pre>' + html + '</pre>')
          return response
         

Vishesh Mangla

unread,
May 11, 2023, 9:25:03 AM5/11/23
to django...@googlegroups.com
Try weasyprint 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/42a1e5d3-718b-4a09-8dbd-b035756765c9n%40googlegroups.com.

Michael Edet

unread,
May 11, 2023, 9:42:12 AM5/11/23
to django...@googlegroups.com

Abdulrahman Abbas

unread,
May 11, 2023, 1:54:04 PM5/11/23
to django...@googlegroups.com

Oduwa Imade

unread,
May 11, 2023, 2:17:51 PM5/11/23
to Django users
Hello everyone, I would like to thank you for your concern and support regarding the issue I faced earlier. I am happy to inform you that I was able to resolve the error. I discovered that the xhtml2pdf library was unable to process or parse an HTML file that contained a table. I'm not exactly sure why this was causing an error:
ValueError at /RecruitmentApp/createcvpdf
<PmlTable@0x1B92DB31D50 23 rows x 3 cols> with cell(0,0) containing
'<PmlKeepInFrame at 0x1b92db92e90> size=x' data error - 23 rows in data but 22 row heights 

To fix the problem, I removed the table element from my HTML file and used <div> and <p> elements instead, while also utilizing Django tags for a structured layout. I was then able to successfully render the PDF.

Vishesh Mangla

unread,
May 11, 2023, 4:57:15 PM5/11/23
to django...@googlegroups.com
why don't you just use weasyprint?

Moussa Mallé

unread,
May 11, 2023, 6:26:52 PM5/11/23
to django...@googlegroups.com
I think you must replace requst by request on the parameters of your function createPdf 

Oduwa Imade

unread,
May 11, 2023, 7:24:20 PM5/11/23
to Django users
Ok, I will try this also...thanks!

Oduwa Imade

unread,
May 11, 2023, 7:25:13 PM5/11/23
to Django users
Wow! thanks for point out this error!
Reply all
Reply to author
Forward
0 new messages