PyMuPDF icon indicating copy to clipboard operation
PyMuPDF copied to clipboard

TextWrite.fill_textbox() error: wrong handling of empty input

Open koopmac opened this issue 4 years ago • 6 comments

Describe the bug (mandatory)

Writer.fill_textbox() is unusable for PyMuPPDF 1.19.2.

To Reproduce (mandatory)

  • Python 3.9.2
  • Pymupdf 1.19.2 writer = fitz.TextWriter(page.rect, color=color) writer.fill_textbox(new_area, variable, fontsize=fontsize, warn=True, align=align, font=font)

Expected behavior (optional)

Page should successfully redact as it did before upgrading to 1.19.2.

Screenshots (optional)

If applicable, add screenshots to help explain your problem.

Your configuration (mandatory)

  • Docker, Flask, python 3.9
  • Pymupdf 1.19.2

File "/app/prosebit/blueprints/api/pymupdf.py", line 968, in apply_overlay writer.fill_textbox(new_area, variable, fontsize=fontsize, File "/home/python/.local/lib/python3.9/site-packages/fitz/utils.py", line 4412, in fill_textbox return new_lines[i + 1 :] # return non-written lines UnboundLocalError: local variable 'i' referenced before assignmen

koopmac avatar Dec 08 '21 12:12 koopmac

Cannot reproduce: My test scripts work for 1.19.2 whether or not the target rectangle is large enough to contain all or some lines only.

JorjMcKie avatar Dec 08 '21 14:12 JorjMcKie

Page should successfully redact as it did before upgrading to 1.19.2.

What is the meaning of redactions (annotations?) in this context?

JorjMcKie avatar Dec 08 '21 14:12 JorjMcKie

BTW: UnboundLocalError can only happen, if the list new_lines is empty. I am going to change that part anyway for the next version. As a hotfix you may want to do i = 0 before the start of loop, in line 4385.

JorjMcKie avatar Dec 08 '21 15:12 JorjMcKie

Yes the issue was an empty string was being passed to fill_textbox to create an empty redaction. sending ' ' seemed to fix the issue.

koopmac avatar Dec 08 '21 16:12 koopmac

I will keep this issue open, because invalid input like yours should be coped with better. In addition, counting and returning lines that did not fit ninto the textbox was not correct.

JorjMcKie avatar Dec 08 '21 18:12 JorjMcKie

New version 1.19.3 is being uploaded to PyPI.

JorjMcKie avatar Dec 12 '21 12:12 JorjMcKie