使用borb PDF库生成表格:€符号对齐与下划线截断问题求助
解决borb PDF库中特殊字符导致的对齐和边框问题
问题描述
使用Python borb库生成PDF表格时遇到两个格式异常:
- 包含€符号的
TableCell文本无法实现右对齐(替换为$符号则正常生效) - 包含重音字母(如é)的文本,其
border_bottom边框无法延伸至文本末尾
测试代码
from borb.pdf import Document from borb.pdf import Page from borb.pdf import SingleColumnLayout from borb.pdf import PageLayout from decimal import Decimal from borb.pdf import FixedColumnWidthTable from borb.pdf import Paragraph from borb.pdf import Alignment from borb.pdf import HexColor from borb.pdf import TableCell from borb.pdf import PDF def main(): # Create document pdf = Document() # Add page page = Page() pdf.add_page(page) # create PageLayout page_layout: PageLayout = SingleColumnLayout(page) page_layout.vertical_margin = page.get_page_info().get_height() * Decimal(0.02) table_001 = FixedColumnWidthTable(number_of_rows=2, number_of_columns=2) table_001.add( Paragraph( 'Direccion Facturacion:', font_size=Decimal(9), border_bottom=True ) ) table_001.add( Paragraph( "Direccion de Envio:", font_size=Decimal(9), border_bottom=True ) ) table_001.add(TableCell(Paragraph("190,00", font_size=Decimal(9)), background_color=HexColor("BBBBBB"))) table_001.add(TableCell(Paragraph("190,00", font_size=Decimal(9), horizontal_alignment=Alignment.RIGHT), background_color=HexColor("BBBBBB"))) table_002 = FixedColumnWidthTable(number_of_rows=2, number_of_columns=2) table_002.add( Paragraph( 'Dirección Facturación:', font_size=Decimal(9), border_bottom=True ) ) table_002.add( Paragraph( "Dirección de Envío:", font_size=Decimal(9), border_bottom=True ) ) table_002.add(TableCell(Paragraph("190,00 €", font_size=Decimal(9)), background_color=HexColor("BBBBBB"))) table_002.add(TableCell(Paragraph("190,00 €", font_size=Decimal(9), horizontal_alignment=Alignment.RIGHT), background_color=HexColor("BBBBBB"))) page_layout.add(table_001) page_layout.add(Paragraph(" ")) page_layout.add(table_002) # store with open("test.pdf", "wb") as out_file_handle: PDF.dumps(out_file_handle, pdf) if __name__ == "__main__": main()
测试结果
- table_001(无特殊字符):第一行
border_bottom显示正常,第二行文本右对齐生效 - table_002(含€和重音字母):第一行
border_bottom截断,第二行文本右对齐失效
解决方案
问题根源是borb默认使用的字体缺少特殊字符的字形,导致文本宽度计算错误。加载支持完整Unicode字符的字体即可解决。
修改后的代码
from borb.pdf import Document from borb.pdf import Page from borb.pdf import SingleColumnLayout from borb.pdf import PageLayout from decimal import Decimal from borb.pdf import FixedColumnWidthTable from borb.pdf import Paragraph from borb.pdf import Alignment from borb.pdf import HexColor from borb.pdf import TableCell from borb.pdf import PDF from borb.pdf import Font, FontFamily def main(): # 加载支持特殊字符的字体(示例使用Noto Sans,需确保字体文件路径正确) # 可从系统字体目录或本地文件加载 try: noto_font = Font.true_type_font_from_file("/usr/share/fonts/truetype/noto/NotoSans-Regular.ttf") except FileNotFoundError: # 替换为你的字体文件路径 noto_font = Font.true_type_font_from_file("NotoSans-Regular.ttf") font_family = FontFamily(noto_font) # Create document pdf = Document() # Add page page = Page() pdf.add_page(page) # create PageLayout page_layout: PageLayout = SingleColumnLayout(page) page_layout.vertical_margin = page.get_page_info().get_height() * Decimal(0.02) table_001 = FixedColumnWidthTable(number_of_rows=2, number_of_columns=2) table_001.add( Paragraph( 'Direccion Facturacion:', font_size=Decimal(9), border_bottom=True, font=font_family.get_font("Regular") ) ) table_001.add( Paragraph( "Direccion de Envio:", font_size=Decimal(9), border_bottom=True, font=font_family.get_font("Regular") ) ) table_001.add(TableCell(Paragraph("190,00", font_size=Decimal(9), font=font_family.get_font("Regular")), background_color=HexColor("BBBBBB"))) table_001.add(TableCell(Paragraph("190,00", font_size=Decimal(9), horizontal_alignment=Alignment.RIGHT, font=font_family.get_font("Regular")), background_color=HexColor("BBBBBB"))) table_002 = FixedColumnWidthTable(number_of_rows=2, number_of_columns=2) table_002.add( Paragraph( 'Dirección Facturación:', font_size=Decimal(9), border_bottom=True, font=font_family.get_font("Regular") ) ) table_002.add( Paragraph( "Dirección de Envío:", font_size=Decimal(9), border_bottom=True, font=font_family.get_font("Regular") ) ) table_002.add(TableCell(Paragraph("190,00 €", font_size=Decimal(9), font=font_family.get_font("Regular")), background_color=HexColor("BBBBBB"))) table_002.add(TableCell(Paragraph("190,00 €", font_size=Decimal(9), horizontal_alignment=Alignment.RIGHT, font=font_family.get_font("Regular")), background_color=HexColor("BBBBBB"))) page_layout.add(table_001) page_layout.add(Paragraph(" ")) page_layout.add(table_002) # store with open("test_fixed.pdf", "wb") as out_file_handle: PDF.dumps(out_file_handle, pdf) if __name__ == "__main__": main()
说明
- 选择支持Unicode的字体(如Noto Sans、Arial Unicode MS等),确保包含€符号和重音字母的字形
- 在创建所有
Paragraph时指定该字体,让borb能正确计算文本宽度,从而解决对齐和边框截断问题
内容的提问来源于stack exchange,提问作者Davi_2k2
相关产品推荐
相关产品推荐

