Skip to content
This repository was archived by the owner on Jun 27, 2025. It is now read-only.

Arabic badly typed #10

@AmmarHaddadi

Description

@AmmarHaddadi

What were you trying to do?

To write a text in arabic (RTL language).

How did you attempt to do it?

import { PDFDocument, StandardFonts, rgb } from 'pdf-lib'
import fontkit from '@pdf-lib/fontkit'

const pdfDoc = await PDFDocument.create()

//font
pdfDoc.registerFontkit(fontkit)
const font = Bun.file(import.meta.dir + "/NotoKufiArabic-Regular.ttf")
const customFont = await pdfDoc.embedFont(await font.arrayBuffer())

// const pages = pdfDoc.getPages()
const page = pdfDoc.addPage()

const text = "اللغة العربية"
const fontSize = 22;
const textWidth = customFont.widthOfTextAtSize(text, fontSize)
const textHeight = customFont.heightAtSize(fontSize)
const { width, height } = page.getSize();
page.drawText(text, {
	x: 300,
	y: height - 7 * fontSize,
	size: fontSize,
	color: rgb(0, 0.53, 0.71),
	font: customFont,
})

const final = await pdfDoc.save()
await Bun.write("t.pdf", final)

What actually happened?

Letters are detached/spaced out too much, the dots are in wrong places

Image

it should look like this

Image

font used:
https://fonts.google.com/noto/specimen/Noto+Kufi+Arabic?script=Arab

What did you expect to happen?

Arabic text should be readable

How can we reproduce the issue?

import { PDFDocument, StandardFonts, rgb } from 'pdf-lib'
import fontkit from '@pdf-lib/fontkit'

const pdfDoc = await PDFDocument.create()

//font
pdfDoc.registerFontkit(fontkit)
const font = Bun.file(import.meta.dir + "/NotoKufiArabic-Regular.ttf")
const customFont = await pdfDoc.embedFont(await font.arrayBuffer())

[t.pdf](https://github.com/user-attachments/files/19170944/t.pdf)

const page = pdfDoc.addPage()

const text = "اللغة العربية"
const fontSize = 22;
const textWidth = customFont.widthOfTextAtSize(text, fontSize)
const textHeight = customFont.heightAtSize(fontSize)
const { width, height } = page.getSize();
page.drawText(text, {
	x: 300,
	y: height - 7 * fontSize,
	size: fontSize,
	color: rgb(0, 0.53, 0.71),
	font: customFont,
})

const final = await pdfDoc.save()
await Bun.write("t.pdf", final)

Link to font (can't upload here): https://fonts.google.com/noto/specimen/Noto+Kufi+Arabic?script=Arab

Version

1.17.1

What environment are you running pdf-lib in?

Other

Checklist

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

I use bunjs

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions