Page.insert_box:If the text has too many characters, it will not be displayed completely. #3113
-
Description of the bugt1 = "text with rotate = 0.can't see me",Can't display... import fitz
doc = fitz.open() # new or existing PDF
page = doc.new_page() # new page, or choose doc[n]
# write in this overall area
rect = fitz.Rect(100, 100, 300, 150)
# partition the area in 4 equal sub-rectangles
CELLS = fitz.make_table(rect, cols=4, rows=1)
t1 = "text with rotate = 0.can't see me" # these texts we will written
t2 = "text with rotate = 90."
t3 = "text with rotate = 180."
t4 = "text with rotate = 270."
text = [t1, t2, t3, t4]
red = fitz.pdfcolor["red"] # some colors
gold = fitz.pdfcolor["gold"]
blue = fitz.pdfcolor["blue"]
shape = page.new_shape() # create Shape
for i in range(len(CELLS[0])):
shape.draw_rect(CELLS[0][i]) # draw rectangle
shape.insert_textbox(
CELLS[0][i], text[i], fontname="hebo", color=blue, rotate=90 * i
)
shape.finish(width=0.3, color=red, fill=gold)
shape.commit() # write all stuff to the page
doc.save("D:/tst/xxx.pdf") How to reproduce the bugPyMuPDF version1.23.19 Operating systemWindows Python version3.11 |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 3 replies
-
This is normal! If you do not want to care about whether something fits or not, use |
Beta Was this translation helpful? Give feedback.
-
import fitz_new as fitz
# Example text
text = """
<p>Hello World!</p>
<p class="a">你好世界!</p>
<span class="b">你好世界!我是span!</span>
"""
arch = fitz.Archive("D:/")
# These statements define which font file to use for regular, bold,
# italic and bold-italic text.
# We assign an arbitary common font-family for all 4 font files.
# The Story algorithm will select the right file as required.
# We request to use "comic" throughout the text.
css = """
@font-face {font-family: 'SimSun-ExtB Regular'; src: url(simsunb_reduce.ttf);}
@font-face {font-family: 'Arial Regular'; src: url(arial_reduce.ttf);}
@font-face {font-family: 'XinYuGongZhangJiaSongA'; src: url(OPPOSans.ttf);}
* {font-family: 'Arial Regular';}
.a {font-family: 'XinYuGongZhangJiaSongA';}
.b {font-family: 'SimSun-ExtB Regular';}
"""
doc = fitz.Document()
page = doc.new_page(width=300, height=300) # make small page
page.insert_htmlbox(page.rect, text, css=css, archive=arch)
doc.subset_fonts(verbose=True) # build subset fonts to reduce file size
doc.save("D:/123_new.pdf") Is this normal? |
Beta Was this translation helpful? Give feedback.
-
Yes - set verbose to False if you don't want to see the messages. Your save statement however is too careless! |
Beta Was this translation helpful? Give feedback.
-
'NotoSerif-Regular' and 'Droid Sans Fallback Regular',I did not set these two font. |
Beta Was this translation helpful? Give feedback.
-
This is one of MuPDF's default fonts when no other font-family name is applicable. Depending on the Unicodes encountered, either "NotoSerif-Regular" is chosen or "CharisSIL-Serif..." in such a case. But you can find out yourself what has happened: extract the text via "dict" or "rawdict" (respectively "json"/"rawjson") and you will which font has been used for what. |
Beta Was this translation helpful? Give feedback.
-
I have something can't handle about import fitz_new as fitz
doc = fitz.open("C:/tst/old.pdf") # open document
page = doc[0] # get the 1st page of the document
rect2=fitz.Rect(100,156,220,170)
text = """<div class="cs">分切尺寸:200*300mm</div>"""
arch = fitz.Archive("D:/")
CSS = """
@font-face {font-family: 'simsun'; src: url(simsun.ttc);}
@font-face {font-family: 'arial'; src: url(arial.ttf);}
# @font-face {font-family: 'OPPOSans'; src: url(OPPOSans.ttf);}
* {
font-family: 'simsun',sans-serif;
font-size:7.98pt;
color:red;
margin:0px;
padding:0px;
border-width:0px;
}
.cs {
font-family: arial,sans-serif;
# font-size:7.98pt;
}
"""
page.insert_htmlbox(
rect2,
text,
css=CSS,
archive=arch,
rotate=0,
)
print(page.get_text("dict", sort=False))
doc.subset_fonts(verbose=True)
doc.ez_save("C:/tst/new.pdf")
doc.close() I want the text start at x0=100, but I runned the code above ,It gives me
The text just starts at the 101 ! And I changed html tag: It gives me another position,and the fontsize=6.359182357788086.....:
|
Beta Was this translation helpful? Give feedback.
The method makes an internal default decision about the margin:
"body {margin:1px;}" + css
, wherecss
is your parameter in the method.So overwrite with desired margin value.