3

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)
Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54

1 Answers1

3

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

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
  • Exactly! Not every font is capable of displaying every character in every script. The 14 standard fonts in PDF date from a very western-Europe-centric age. They do not support non-western scripts (Arabic, Hindi, Tamil, etc). If you want to display these characters, you need to use a font that contains these characters. – Joris Schellekens Jun 03 '22 at 23:29