Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZeroDivisionError: float division by zero #20

Closed
shen139 opened this issue Aug 3, 2011 · 12 comments
Closed

ZeroDivisionError: float division by zero #20

shen139 opened this issue Aug 3, 2011 · 12 comments

Comments

@shen139
Copy link

shen139 commented Aug 3, 2011

Hi,
I get this error while trying to parse an HTML containing the following piece of code.
I'm using the latest versions of all packages needed:

  • html5lib-0.90
  • pyPdf-1.13
  • reportlab-2.5
  • xhtml2pdf-0.0.3

and Python 2.7 (2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)])

Python Code:
-[
import cStringIO as StringIO
from xhtml2pdf import pisa
....

html = '''
<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="2">
<TR>
<TD></TD>
</TR>
</TABLE>
'''
dest = file('test.pdf', "wb")
pdf = pisa.CreatePDF(
StringIO.StringIO(html),
dest,
log_warn = 1,
log_err = 1
)
]-

Note: If I put something inside the TD (example: ".... <TD>... some stuff..... </TD>........") or I change the value of the attr cellpadding, it works!!!

Traceback:
-[
Traceback (most recent call last):
File "C:\tmp\test.py", line 95, in
log_err = 1
File "C:\Python27\lib\site-packages\xhtml2pdf\document.py", line 131, in pisaDocument
doc.build(context.story)
File "C:\Python27\lib\site-packages\reportlab\platypus\doctemplate.py", line 880, in build
self.handle_flowable(flowables)
File "C:\Python27\lib\site-packages\reportlab\platypus\doctemplate.py", line 763, in handle_flowable
if frame.add(f, canv, trySplit=self.allowSplitting):
File "C:\Python27\lib\site-packages\reportlab\platypus\frames.py", line 174, in _add
flowable.drawOn(canv, self._x + self._leftExtraIndent, y, _sW=aW-w)
File "C:\Python27\lib\site-packages\reportlab\platypus\flowables.py", line 108, in drawOn
self._drawOn(canvas)
File "C:\Python27\lib\site-packages\reportlab\platypus\flowables.py", line 89, in _drawOn
self.draw()#this is the bit you overload
File "C:\Python27\lib\site-packages\reportlab\platypus\tables.py", line 1302, in draw
self._drawCell(cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight))
File "C:\Python27\lib\site-packages\reportlab\platypus\tables.py", line 1393, in _drawCell
w, h = self._listCellGeom(cellval,colwidth,cellstyle,W=W, H=H,aH=rowheight)
File "C:\Python27\lib\site-packages\xhtml2pdf\xhtml2pdf_reportlab.py", line 710, in _listCellGeom
return Table._listCellGeom(self, V, w, s, W=W, H=H, aH=aH)
File "C:\Python27\lib\site-packages\reportlab\platypus\tables.py", line 377, in _listCellGeom
vw, vh = v.wrapOn(canv, aW, aH)
File "C:\Python27\lib\site-packages\reportlab\platypus\flowables.py", line 119, in wrapOn
w, h = self.wrap(aW,aH)
File "C:\Python27\lib\site-packages\xhtml2pdf\xhtml2pdf_reportlab.py", line 693, in wrap
return KeepInFrame.wrap(self, availWidth, availHeight)
File "C:\Python27\lib\site-packages\reportlab\platypus\flowables.py", line 970, in wrap
W, H = func(s1)
File "C:\Python27\lib\site-packages\reportlab\platypus\flowables.py", line 951, in func
W /= x
ZeroDivisionError: float division by zero
]-

Thanks for your great job,
Shen139

@elena
Copy link

elena commented Oct 15, 2011

Had this same error and after quite a lot of reduction found the cause in the html.

With a lot of crazy stuff in the html (<script>, <style>, @, doctype) it turns out that the bug was set off by one line of css in a style tag.

I can't work out why this exact line of css was broken -- but it was definitely cause of this error:

td {border: 1px solid #aaa; padding: 2px 5px; margin: 0;}

The error was resolved when the 'padding' style was removed:

td {border: 1px solid #aaa; margin: 0;}

Regards!


Note that I have the following line further up that did not cause this bug:

h3 {padding: 10px 5px 5px 5px; font-family: Verdana, Arial, Helvetica, sans-serif; margin: 0; color: #333; border-top: 1px solid #666; }

@uoramas
Copy link

uoramas commented Dec 2, 2011

Hi. I had this problem only when trying to render a table which contained a lot of rows. I confirm that the above solution worked for me. Thanks!

@vanschelven
Copy link

I can confirm that the above error happens (in the case of a large table, combined with a "padding: 4px" css).

Removing the line "works", i.e. makes it no longer crash (but costs us dearly in the layout).
A real solution would be even better.

@vanschelven
Copy link

It seems to me the division by zero is caused by the variable H being 0 at some point.

Similarly, the value of s1 will be 0 in that case as well, and trigger and error further down even if wrapping the above lines in if statements

s = _qsolve(maxHeight*(1-f),_hmodel(s0,s1,H0,H1))

will trigger

a21 = 1./s1**2 to fail further down

@afriapps
Copy link

I had the same issue, paading fixed it. But this is not a fix!

@jodybeggs
Copy link

I had the same issue but with empty html tags

Fixed it with a non breaking space in every empty tag.

Sorry I wanted to leave an example but the html disappears need a link to the comment docs.

@mabroor
Copy link

mabroor commented Apr 17, 2013

any updates on this issue? I have found that the culprit is the top and bottom padding, minimum usable value is 1px

@pztrick
Copy link

pztrick commented Mar 24, 2014

Another culprit...

<div class="pdf-report" style="width: 100%;"> ... </div>

@cristianocca
Copy link

Any news on this? No padding nor width with percentages yet I get this error with a lot of data, and it happens randomly (sometimes fails, sometimes ok)
Is this project at least alive?

@ribalba
Copy link
Contributor

ribalba commented Nov 12, 2014

Hello, yes it is alive :) Can you maybe send me the files so I can try to recreate the error. didi@ribalba.de Cheers

@cristianocca
Copy link

Looks like it is fixed on 0.0.6, at least for my case, but since there are many possible options causing it I can't tell exactly.

dominicmontreuil added a commit to dominicmontreuil/django-pdfutils that referenced this issue Apr 1, 2015
Bring xhtml2pdf to the current 0.0.6 version. 

It is compatible with the project and it solves issues when generating reports with large amount of rows that caused "Division by zero" errors caused by the CSS even though the css was valid. 

See for reference on the error:
xhtml2pdf/xhtml2pdf#20

I have had to revisit my css as the structure of the generation is slightly different, but not much work required.
@benjaoming
Copy link
Contributor

Not seeing this issue anymore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests