'Arabic letters in python
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)
Solution 1:[1]
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

