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

Fix ZeroDivisionError on broken image files #723

Merged
merged 3 commits into from Oct 8, 2023

Conversation

timobrembeck
Copy link
Collaborator

Short description

This error happened on images where the width could not be correctly identified:

Traceback (most recent call last):
  File "/home/xhtml2pdf/document.py", line 196, in pisaDocument
    doc.build(context.story)
  File "/home/.venv/lib/python3.11/site-packages/reportlab/platypus/doctemplate.py", line 1082, in build
    self.handle_flowable(flowables)
  File "/home/.venv/lib/python3.11/site-packages/reportlab/platypus/doctemplate.py", line 931, in handle_flowable
    if frame.add(f, canv, trySplit=self.allowSplitting):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/.venv/lib/python3.11/site-packages/reportlab/platypus/frames.py", line 212, in _add
    flowable.drawOn(canv, self._x + self._leftExtraIndent, y, _sW=aW-w)
  File "/home/.venv/lib/python3.11/site-packages/reportlab/platypus/flowables.py", line 112, in drawOn
    self._drawOn(canvas)
  File "/home/.venv/lib/python3.11/site-packages/reportlab/platypus/flowables.py", line 93, in _drawOn
    self.draw()#this is the bit you overload
    ^^^^^^^^^^^
  File "/home/xhtml2pdf/xhtml2pdf_reportlab.py", line 716, in draw
    Paragraph.draw(self)
  File "/home/xhtml2pdf/reportlab_paragraph.py", line 1212, in draw
    self.drawPara(self.debug)
  File "/home/xhtml2pdf/reportlab_paragraph.py", line 1769, in drawPara
    dpl(tx, offset, lines[0], noJustifyLast and nLines == 1)
  File "/home/xhtml2pdf/reportlab_paragraph.py", line 379, in _leftDrawParaLineX
    _putFragLine(offset, tx, line)
  File "/home/xhtml2pdf/reportlab_paragraph.py", line 219, in _putFragLine
    drawing = cbDefn.image.getDrawing(w, h)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xhtml2pdf/xhtml2pdf_reportlab.py", line 538, in getDrawing
    scale_x = width / drawing.width
              ~~~~~~^~~~~~~~~~~~~~~
ZeroDivisionError: float division by zero

@stefan6419846
Copy link
Contributor

Do you have an example for this to provide a corresponding test as well? Where is the width of zero coming from?

@timobrembeck
Copy link
Collaborator Author

@stefan6419846 if the image file is broken, in my case if was an incorrectly configured web server which returns html instead of an image until the cookies are accepted:
https://foresight4youth.com/wp-content/uploads/2018/07/Logo-Caritas-Dortmund-300x124.jpg

This error also happens when normal text files are embedded as images.

Of course, this is a content problem and not necessarily task of the library to prevent such mistakes, but in my opinion it would be more convenient if the PDF would be created nevertheless and only throw a warning in the logs, not cause an internal server error.

@timobrembeck
Copy link
Collaborator Author

@stefan6419846 Do you think this will do? Unfortunately we don't have access to the file name in that scope, and it would be a bit complicated to escalate the exception down to the scope that has access to the filename...

@stefan6419846
Copy link
Contributor

We do not have the filename there yet, yes. But shouldn't we be able to extend PmlImage to hold the src value as well? (We might have to account for non-filenames in the src attribute for display, but this should be doable as well.)

@timobrembeck timobrembeck force-pushed the bugfix/zero-division branch 2 times, most recently from 044e8a5 to 6dec979 Compare October 8, 2023 15:33
@timobrembeck
Copy link
Collaborator Author

@stefan6419846 cool idea, maybe like this?

@stefan6419846
Copy link
Contributor

LGTM, as it makes the error message more helpful/direct.

@timobrembeck timobrembeck merged commit 96ec58d into master Oct 8, 2023
16 checks passed
@timobrembeck timobrembeck deleted the bugfix/zero-division branch October 8, 2023 17:37
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

Successfully merging this pull request may close these issues.

None yet

2 participants