简体   繁体   中英

converting html to pdf using pisa

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

It seems there are two main causes:

  1. Your queryset may not be producing any iterable values - kindly check.

OR

  1. If you have several tables in a page, then one of your tables is not formatting well with pisa eg you have a <tr>...<tr> instead of <tr>...</tr>. note the closing / eg you have a <tr>...<tr> instead of <tr>...</tr>. note the closing /

My approach to this problem is to split the whole page into partials (with each table as a partial) and load them in the browser one by one.

You are likely to identify the table causing the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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