Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arabic letters in python

Tags:

python

pdf

borb

Is it possible to write Arabic letters using borb?

I have tried the 14 possible fonts in borb and no one of them was able to display the Arabic letters.

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)
like image 284
Abdallah Hossam Avatar asked Dec 29 '25 19:12

Abdallah Hossam


1 Answers

I think you will need to use a custom font, either an Arabic font already on your computer or a downloaded one from something like Google Fonts.

Below is an example using 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

like image 72
Sash Sinha Avatar answered Jan 01 '26 11:01

Sash Sinha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!