insert_textbox vs insert_htmlbox #3932
Replies: 4 comments 3 replies
-
All you have to do is using CSS source (= styling information). In any Case, MuPDF never looks at the fonts intalled on your machine. Either you accept the default behavior or you must actively provide fonts via file spec or memory equivalents. |
Beta Was this translation helpful? Give feedback.
-
insert_textbox can read fonts within the page, how can I make insert_htmlbox do the same? Is there a sample code to read fonts from memory? thanks |
Beta Was this translation helpful? Give feedback.
-
fname = "r:/jp_1.pdf" doc = pymupdf.open(fname) # open a document pfont = page.get_fonts(full=True) fontbuffer = doc.extract_font(11)[-1] arch = pymupdf.Archive(font.buffer, "myfont") # make font accessible via a name rect2 = pymupdf.Rect((100.0, 600.0, 300.0, 650.0)) rect1 = pymupdf.Rect((100.0, 550.0, 300.0, 600.0)) -> MuPDF error: format error: cannot locate font 'myfont' specified by css |
Beta Was this translation helpful? Give feedback.
-
This demo script works as desired: import pymupdf
from pprint import pprint
doc = pymupdf.open("test-doc-2.pdf")
buff = doc.extract_font(124)[-1]
doc.close() # original PDF no longer needed
font = pymupdf.Font(fontbuffer=buff)
arch = pymupdf.Archive(font.buffer, "myfont")
css = """ @font-face {font-family: myfont; src: url(myfont);}
* {font-family: myfont;}"""
doc = pymupdf.open()
page = doc.new_page()
page.insert_htmlbox((100, 100, 200, 200), "Text in custom font", css=css, archive=arch)
pprint(page.get_fonts()) |
Beta Was this translation helpful? Give feedback.
-
Hi, when i use insert_textbox get correct font, but insert_htmlbox get different result, may i know how to change args in insert_htmlbox? thanks
jp_1.pdf
jp_2.pdf
#pymupdf 1.24.8
`import pymupdf
fname = "r:/jp_1.pdf"
new_file = "r:/jp_2.pdf"
doc = pymupdf.open(fname) # open a document
page = doc[0]
pfont = page.get_fonts(full=True)
print(pfont)
#[(11, 'ttf', 'TrueType', 'ABKYFM+HelveticaNeue-Bold', 'TT0', 'WinAnsiEncoding', 0),
(14, 'ttf', 'TrueType', 'ABFMUM+HelveticaNeue', 'TT1', 'WinAnsiEncoding', 0)]
fontbuffer = doc.extract_font(11)[-1]
page.insert_font(fontname="TT0", fontbuffer=fontbuffer)\
#-> font:HelveticaNeue-Bold (display correct font)
rect1 = pymupdf.Rect((100.0, 550.0, 300.0, 600.0))
page.insert_textbox(rect1, buffer="Embracing", fontsize=24, fontname="TT0")
#-> font:CharisSIL (insert_htmlbox cannot detect HelveticaNeue-Bold)
rect2 = pymupdf.Rect((100.0, 600.0, 300.0, 650.0))
page.insert_htmlbox(rect2, 'Embracing', css="* {font-family: HelveticaNeue-Bold;font-size:24px;}")
doc.save(new_file)
doc.close()
`
Beta Was this translation helpful? Give feedback.
All reactions