python - python中的阿拉伯字母

标签 python pdf borb

是否可以使用 borb 编写阿拉伯字母?

我已经在 borb 中尝试了 14 种可能的字体,但没有一种能够显示阿拉伯字母。

from borb.pdf import Document
from borb.pdf import PDF
from borb.pdf import Page
from borb.pdf import Paragraph
from borb.pdf import SingleColumnLayout

def  main():

    # Create an empty Document.
    pdf = Document()

    # Add an empty Page.
    page = Page()
    pdf.append_page(page)

    # Use a PageLayout (SingleColumnLayout in this case).
    layout = SingleColumnLayout(page)

    # Add a Paragraph
    layout.add(Paragraph("Hello World", font="Helvetica"))

    # Store the PDF.
    with open(Path('output.pdf'), 'wb') as pdf_file_handle:
        PDF.dumps(pdf_file_handle, pdf)

最佳答案

我认为您需要使用自定义字体,可以是计算机上已有的阿拉伯字体,也可以是从 Google Fonts 之类的网站下载的字体。 .

下面是一个使用 IBM Plex Sans Arabic 的例子:

from pathlib import Path

from borb.pdf import Document
from borb.pdf import PDF
from borb.pdf import Page
from borb.pdf import Paragraph
from borb.pdf import SingleColumnLayout
from borb.pdf.canvas.font.simple_font.true_type_font import TrueTypeFont


def main() -> None:
    # Create an empty Document.
    pdf = Document()

    # Add an empty Page.
    page = Page()
    pdf.append_page(page)

    # Use a PageLayout (SingleColumnLayout in this case).
    layout = SingleColumnLayout(page)

    # Construct a Font object.
    font_path = Path(__file__).parent / 'IBMPlexSansArabic-Regular.ttf'
    custom_font = TrueTypeFont.true_type_font_from_file(font_path)

    # Add a Paragraph object.
    longest_unicode_character = '﷽'
    layout.add(Paragraph(longest_unicode_character, font=custom_font))

    # Store the PDF.
    with open(Path('output.pdf'), 'wb') as pdf_file_handle:
        PDF.dumps(pdf_file_handle, pdf)


if __name__ == '__main__':
    main()

output.pdf

关于python - python中的阿拉伯字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72355941/

相关文章:

Python - 如何从 __init__ 方法中引用类变量或方法?

python - 为什么 sys.getrefcount() 返回 2?

python - 索引错误: list index out of range when selecte values in openerp

Python 类 : Why can't I use the method len() inside __eq__(self, 其他)?

java - 在Java中使用itext将pdf保存到bytearray

python - ImportError : cannot import name 'Document' from 'borb. pdf.document

c# - PDFSharp 填写表单域

python - 如何在PDF文件末尾添加空白页?

django - 如何使用 borb 库和 Django 从静态文件将图像添加到 Pdf 文件