@@ -2385,7 +2385,7 @@ def insert_htmlbox(
23852385 oc = 0 ,
23862386 opacity = 1 ,
23872387 overlay = True ,
2388- ) -> float :
2388+ ) -> tuple :
23892389 """Insert text with optional HTML tags and stylings into a rectangle.
23902390
23912391 Args:
@@ -2448,16 +2448,18 @@ def insert_htmlbox(
24482448 if not fit .big_enough : # there was no fit
24492449 return (- 1 , scale_low )
24502450
2451- filled = fit .filled
2452- scale = 1 / fit .parameter # shrink factor
2451+ filled = pymupdf .Rect (fit .filled )
2452+ # final adjustment if filled rect is wider than fit rect
2453+ if filled .width > fit .rect .width :
2454+ h = filled .width / fit .rect .width * fit .rect .height
2455+ fit .rect .x1 = filled .x1
2456+ fit .rect .y1 = h
2457+ fit .parameter = fit .rect .x1 / temp_rect .x1
24532458
2454- spare_height = fit .rect .y1 - filled [3 ] # unused room at rectangle bottom
2455- # Note: due to MuPDF's logic this may be negative even for successful fits.
2456- if scale != 1 or spare_height < 0 : # if scaling occurred, set spare_height to 0
2457- spare_height = 0
2459+ spare_height = max ((fit .rect .y1 - filled .y1 ) / fit .parameter , 0 )
24582460
24592461 def rect_function (* args ):
2460- return fit .rect , fit .rect , pymupdf . Identity
2462+ return fit .rect , fit .rect , None
24612463
24622464 # draw story on temp PDF page
24632465 doc = story .write_with_links (rect_function )
@@ -2477,7 +2479,8 @@ def rect_function(*args):
24772479 # -------------------------------------------------------------------------
24782480 # re-insert links in target rect (show_pdf_page cannot copy annotations)
24792481 # -------------------------------------------------------------------------
2480- # scaled center point of fit.rect
2482+ # scaled center point of fit rect
2483+ scale = 1 / fit .parameter
24812484 mp1 = (fit .rect .tl + fit .rect .br ) / 2 * scale
24822485
24832486 # center point of target rect
0 commit comments