最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

python - Borb text triggers "AssertionError: A Rectangle must have a non-negative width." - Stack Overflow

matteradmin5PV0评论

I am interested to change the font of some text that is in documents.

I used the #581-filtering-by-font example and added re-submitting to SimpleFindReplace and it is triggering the assertion that a rectangle cannot have a negative width.

I have confirmed the initial font 'VFMBOX+HelveticaNeueLTStd-Roman' is in the document.

Borb version 2.1.25

Python version - 3.11

This is the code -

*`#!chapter_005/src/snippet_017.py
import typing

from borb.pdf import Document
from borb.pdf import PDF
from borb.toolkit import FontNameFilter
from borb.toolkit import SimpleTextExtraction
from borb.toolkit import SimpleFindReplace

def main():

    # create FontNameFilter
    l0: FontNameFilter = FontNameFilter("VFMBOX+HelveticaNeueLTStd-Roman")

    # filtered text just gets passed to SimpleTextExtraction
    l1: SimpleTextExtraction = SimpleTextExtraction()
    l0.add_listener(l1)

    # read the Document
    doc: typing.Optional[Document] = None
    with open("C:\Data\PDFFontChange\PD-1239-G_RevA_PR.pdf", "rb") as in_file_handle:
        doc = PDF.loads(in_file_handle, [l0])

    # check whether we have read a Document
    assert doc is not None

    templist = (l1.get_text()[0]).splitlines()
    # print(templist)
    i = 0 
    for x in templist :
        print(i, ' : ', x)
        i += 1
        doc = SimpleFindReplace.sub(x, x, doc, repl_font="Helvetica")

    # store
    with open("output2.pdf", "wb") as pdf_file_handle:
        PDF.dumps(pdf_file_handle, doc)

if __name__ == "__main__":
    main()

Here is the full error traceback & I have included the first numbered (printed as 0 : ) text found by the FontNameFilter from the PDF that is resubmitted by the code into the SimpleFindReplace that triggers the error.

0  :  At the heart of your pump or dispenser,
Traceback (most recent call last):
  File "c:\Python\Glenn_Begin\PDF_Workings\BORB_PDF_FontNameAlterTrajan.py", line 43, in <module>
    main()
  File "c:\Python\Glenn_Begin\PDF_Workings\BORB_PDF_FontNameAlterTrajan.py", line 36, in main
    doc = SimpleFindReplace.sub(x, x, doc, repl_font="Helvetica")
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\toolkit\text\simple_find_replace.py", line 72, in sub
    ] = RegularExpressionTextExtraction.get_matches_for_pdf(pattern, doc)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\toolkit\text\regular_expression_text_extraction.py", line 375, in get_matches_for_pdf
    CanvasStreamProcessor(page, Canvas(), []).read(page_source, [cse])
  File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\canvas_stream_processor.py", line 277, in read
    raise e
  File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\canvas_stream_processor.py", line 271, in read
    operator.invoke(self, operands, event_listeners)
  File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\operator\text\show_text_with_glyph_positioning.py", line 84, in invoke
    l._event_occurred(tri)
  File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\toolkit\text\regular_expression_text_extraction.py", line 326, in _event_occurred
    self._render_text(event)
  File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\toolkit\text\regular_expression_text_extraction.py", line 338, in _render_text
    for e in text_render_info.split_on_glyphs():
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\event\chunk_of_text_render_event.py", line 177, in split_on_glyphs
    e._baseline_bounding_box = Rectangle(
                               ^^^^^^^^^^
  File "C:\Python\Glenn_Begin\.venv\Lib\site-packages\borb\pdf\canvas\geometry\rectangle.py", line 30, in __init__
    assert width >= 0, "A Rectangle must have a non-negative width."
           ^^^^^^^^^^
AssertionError: A Rectangle must have a non-negative width.
Post a comment

comment list (0)

  1. No comments so far