This repository was archived by the owner on Jun 27, 2025. It is now read-only.
forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 9
Arabic badly typed #10
Copy link
Copy link
Open
Labels
bugSomething isn't workingSomething isn't working
Description
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
it should look like this
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
mikkopiu
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working

