I am trying to generate a PDF using an already existing DJANGO HTML template. I am passing context data dynamically into the template then trying to convert it to PDF.
I however keep on running into this
File "C:\Users\EliteBook\Desktop\Virtual_Environment\app\lib\site-packages\reportlab\platypus\tables.py", line 265, in init raise ValueError("%s data error - %d rows in data but %d in row heights" % (self.identity(),nrows, len(rowHeights))) ValueError: with cell(0,0) containing ' size=x' data error - 16 rows in data but 15 in row heights
when this line of code is executed...
pdf = pisa.pisaDocument(BytesIO(html.encode("cp1252", 'ignore')), result)
I have looked into PISA and REPORTLAB forums but do not seem to find a suitable solution.
Below is the code that attempts to generate the PDF
def make_pdf(student):
entry = Transaction.objects.filter(student=student)
credits = entry.filter(t_type='C')
debits = entry.filter(t_type='D')
c_count = entry.filter(t_type='C').count()
d_count = entry.filter(t_type='D').count()
e_count = entry.count()
name = student.name
context = {
'entries':entry,
'student':student,
'credits' : credits,
'debits':debits,
'c_count': c_count,
'd_count': d_count,
'e_count': e_count,
'name':name
}
return render_to_pdf('payment/statement.html', context)
The render_to_pdf function is defined in different file as thus:
from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return None
I have removed most of the traceback data to remove clutter so that you can see the actual error when the below line of code is executed
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1", 'ignore')), result)
File "C:\Users\EliteBook\Desktop\Virtual_Environment\app\lib\site-packages\reportlab\platypus\tables.py", line 265, in init raise ValueError("%s data error - %d rows in data but %d in row heights" % (self.identity(),nrows, len(rowHeights))) ValueError: with cell(0,0) containing ' size=x' data error - 16 rows in data but 15 in row heights
this link shows the html site I wish to convert to pdf, I have also attached the HTML file