You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在borb中使用西里尔字母?低层级语法适配问题

在borb低层级语法中配置支持西里尔字母的自定义字体

我在borb中使用西里尔字母时,发现默认字体不支持该字符集。尝试了两种实现方案:

  • 基于Rectangle和Paragraph的方案:可正常显示西里尔字母,但处理速度慢,生成两段PDF需约5秒
  • 低层级语法方案:处理速度提升12倍(仅需0.4秒),但无法显示西里尔字母

推测问题原因:

  • 所用字体不支持西里尔字母
  • 低层级语法中(%b) Tj运算符的字节编码存在问题(西里尔字母为UTF-8双字节编码)

我已拥有支持西里尔字母的TimesNewRomanRegular.ttf字体,但不清楚如何在borb的低层级语法中配置该自定义字体,特此求助。


慢速方案代码

from pathlib import Path
from borb.pdf.canvas.layout.text.paragraph import Paragraph
from borb.pdf.canvas.layout.shape.rectangle import Rectangle
from borb.pdf.canvas.font.font import Font
from borb.pdf.canvas.font.simple_font.true_type_font import TrueTypeFont
from decimal import Decimal

font_path: Path = Path(__file__).parent / "TimesNewRomanRegular.ttf"
custom_font: Font = TrueTypeFont.true_type_font_from_file(font_path)
for i, s in enumerate(symb_arr):
    r: Rectangle = Rectangle(
        Decimal(s.x_coord),  # x: 0 + page_margin
        Decimal(s.y_coord),  # y: page_height - page_margin - height_of_textbox
        Decimal(s.width + 2),  # width: page_width - 2 * page_margin
        Decimal(s.height + 2),  # height
    )
    Paragraph(s.sym, font_size=Decimal(font_size), font=custom_font).paint(page, r)

Symbol类定义

class Symbol:
    def __init__(self, s, x, y, w, h, f):
        self.sym = s  # 字符
        self.x_coord = x
        self.y_coord = y
        self.width = w
        self.height = h
        self.font_size = f

快速低层级方案代码

from borb.pdf import Stream, Name, Dictionary, bDecimal
import zlib

# 创建内容流
content_stream = Stream()
content = b""""""
for s in symb_arr:
    content += b"""
        q
        BT
        /F1 %b Tf
        %b %b Td
        (%b) Tj
        ET
        Q
    """ % (bytes(format(s.font_size, '.4f'), 'utf-8'), bytes(format(s.x_coord, '.4f'), 'utf-8'),
           bytes(format(s.y_coord, '.4f'), 'utf-8'), bytes(str(s.sym), 'utf-8'))

content_stream[Name("DecodedBytes")] = content
content_stream[Name("Bytes")] = zlib.compress(content_stream["DecodedBytes"], 9)
content_stream[Name("Filter")] = Name("FlateDecode")
content_stream[Name("Length")] = bDecimal(len(content_stream["Bytes"]))

# 设置页面内容
page[Name("Contents")] = content_stream

# 设置字体
page[Name("Resources")] = Dictionary()
page[Name("Resources")] = doc.get_page(0)["Resources"]  # 从原始页面加载字体

borb文档中的低层级字体配置示例

# 设置字体
page[Name("Resources")] = Dictionary()
page["Resources"][Name("Font")] = Dictionary()
page["Resources"]["Font"][Name("F1")] = Dictionary()
page["Resources"]["Font"]["F1"][Name("Type")] = Name("Font")
page["Resources"]["Font"]["F1"][Name("Subtype")] = Name("Type1")
page["Resources"]["Font"]["F1"][Name("Name")] = Name("F1")
page["Resources"]["Font"]["F1"][Name("BaseFont")] = Name("Helvetica")
page["Resources"]["Font"]["F1"][Name("Encoding")] = Name("MacRomanEncoding")

内容的提问来源于stack exchange,提问作者Vladislav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 15:21:02