Document, pages and text
The constructor, document settings, pages, fonts, Arabic and Unicode text, colors, text and paragraphs.
Constructor#
pdf = new PDFWriter()Creates a new PDF document with one blank A4 page. All 14 PDF standard fonts are pre-registered and ready to use — no font files required.
Document Settings#
| Method | Description |
|---|---|
setPageSize(size) | Page size constant — see Page Sizes table |
setOrientation(orient) | PDF_PORTRAIT or PDF_LANDSCAPE |
setMargins(left, top, right, bottom) | Margins in points (72 pt = 1 inch). Default: 72 all sides |
setTitle(text) | Document title |
setAuthor(text) | Author name |
setSubject(text) | Subject |
setKeywords(text) | Keywords |
setCreator(text) | Creator application name |
Page Sizes#
| Constant | Points | Physical |
|---|---|---|
PDF_A4 | 595.28 × 841.89 | 210 × 297 mm |
PDF_A3 | 841.89 × 1190.55 | 297 × 420 mm |
PDF_A5 | 419.53 × 595.28 | 148 × 210 mm |
PDF_LETTER | 612 × 792 | 8.5 × 11 in |
PDF_LEGAL | 612 × 1008 | 8.5 × 14 in |
PDF_TABLOID | 792 × 1224 | 11 × 17 in |
Page Management#
| Method | Returns | Description |
|---|---|---|
addPage() | self | Add a new page and switch to it |
selectPage(n) | self | Switch to page n (1-based) |
getPageCount() | number | Total number of pages |
getPageWidth() | number | Current page width in points |
getPageHeight() | number | Current page height in points |
getContentWidth() | number | Page width minus left and right margins |
getContentHeight() | number | Page height minus top and bottom margins |
Fonts#
PDFLib supports two categories of fonts: PDF standard fonts for Latin text, and TrueType fonts for Arabic/Unicode text.
PDF Standard Fonts#
The PDF specification (ISO 32000) defines 14 standard fonts that every conforming PDF viewer is required to have built in. PDFLib references these by name only — no font files are shipped or embedded; the viewer supplies the glyphs. This makes standard-font PDFs very small.
Set the active font with setFont(fontName, size) or change only the size with setFontSize(size).
pdf.setFont(PDF_HELVETICA_BOLD, 18)
pdf.setFontSize(12)| Constant | PDF Font Name | Style |
|---|---|---|
PDF_HELVETICA | Helvetica | Sans-serif |
PDF_HELVETICA_BOLD | Helvetica-Bold | Sans-serif bold |
PDF_HELVETICA_ITALIC | Helvetica-Oblique | Sans-serif italic |
PDF_HELVETICA_BOLDITALIC | Helvetica-BoldOblique | Sans-serif bold italic |
PDF_TIMES | Times-Roman | Serif |
PDF_TIMES_BOLD | Times-Bold | Serif bold |
PDF_TIMES_ITALIC | Times-Italic | Serif italic |
PDF_TIMES_BOLDITALIC | Times-BoldItalic | Serif bold italic |
PDF_COURIER | Courier | Monospace |
PDF_COURIER_BOLD | Courier-Bold | Monospace bold |
PDF_COURIER_ITALIC | Courier-Oblique | Monospace italic |
PDF_COURIER_BOLDITALIC | Courier-BoldOblique | Monospace bold italic |
PDF_SYMBOL | Symbol | Greek and mathematical symbols |
PDF_ZAPFDINGBATS | ZapfDingbats | Decorative dingbats |
Note: Standard fonts only cover the Latin-1 (Western) character set. They do not support Arabic, Hebrew, Chinese, or other non-Latin scripts. For Arabic text, a TrueType font must be loaded separately — see the Arabic / Unicode Text section.
TrueType Fonts#
Any .ttf file can be loaded. The font is fully parsed (cmap, hmtx, glyf, head, hhea, loca, OS/2, and post tables) and embedded in the output PDF as a CIDFont/Type0 object with a ToUnicode CMap. No external tool or C library is needed.
pdf.loadTTFFont("font/arial.ttf", "Arabic") # general method
pdf.loadArabicFont("font/arial.ttf", "Arabic") # alias — identical behaviour
pdf.setFont("Arabic", 24)The second argument is a free-form alias used with setFont. The library ships font/arial.ttf in the samples folder as a ready-to-use example; any other TTF can be substituted.
Script support matrix:
| Script | Method to use | Notes |
|---|---|---|
| Arabic | drawArabicText / drawArabicParagraph | Full support: contextual shaping, RTL layout, word wrap |
| Latin extensions (é, ü, ñ …) | drawText | Works — direct codepoint-to-glyph mapping, no shaping needed |
| Greek, Cyrillic | drawText | Works — no shaping required |
| CJK (Chinese, Japanese, Korean) | drawText | Works — one codepoint maps to one glyph |
| Hebrew (basic) | drawText | Glyphs render but no RTL layout support outside the Arabic path |
| Devanagari (Hindi), Thai, Khmer, Myanmar | — | Not supported — these scripts require a shaping engine not present in the library |
In short: load any TTF and use drawText for scripts that don't need complex shaping. Use the drawArabicText family specifically for Arabic, which is the only script with a full shaping and bidirectional layout pipeline implemented.
Arabic / Unicode Text#
PDFLib includes a pure Ring Arabic text engine supporting UTF-8 decoding, contextual letter shaping (isolated/initial/medial/final letter forms), bidi reordering, and right-to-left layout. A TrueType font must be loaded first — see TrueType Fonts above.
Drawing Arabic Text#
| Method | Description |
|---|---|
drawArabicText(text, x, y) | Draw Arabic text; x is the right edge of the text (RTL) |
drawArabicTextLeft(text, x, y) | Draw Arabic text with x as the left edge |
drawArabicTextCentered(text, x, y) | Draw Arabic text centered at x |
drawArabicTextInCell(text, x, y) | Draw Arabic text in a LTR cell context (left-edge x) |
drawArabicParagraph(text, x, y, maxWidth, lineHeight) | RTL word-wrapped paragraph; x is right edge; returns final Y |
getArabicTextWidth(text) | Returns rendered width in points for the current font and size |
pdf.loadArabicFont("font/arial.ttf", "Arabic")
pdf.setFont("Arabic", 28)
pdf.setTextColor("black")
# x = 523 is the right edge of the text
pdf.drawArabicText("مرحبا بالعالم", 523, 700)
# RTL paragraph with word wrap, max width 441 pt
pdf.drawArabicParagraph("لغة البرمجة رينج هي لغة حديثة وسهلة التعلم", 523, 600, 441, 28)Mixed Arabic/Latin Documents#
When an arabicFont option is passed to drawTable, cells are automatically rendered with the correct font and direction based on their content. Arabic segments within a mixed cell are drawn right-to-left; Latin segments are drawn left-to-right.
pdf.drawTable(data, 72, 700, [200, 268], [
:headerBg = "navy",
:headerFg = "white",
:arabicFont = "Arabic"
])Colors#
pdf.setTextColor(color)
pdf.setFillColor(color)
pdf.setStrokeColor(color)Colors can be specified in three formats:
- Named string:
"red","navy","steelblue", etc. - Hex string:
"#FF5733"or"FF5733" - RGB array:
[255, 87, 51]
Named colors: black, white, red, green, blue, yellow, orange, purple, pink, gray, grey, navy, teal, maroon, silver, lime, aqua, cyan, fuchsia, olive, brown, coral, crimson, gold, indigo, salmon, steelblue, tomato, darkblue, darkgreen, darkred, lightgray, lightgrey, darkgray, darkgrey
Text Drawing#
pdf.drawText(text, x, y) # Left-aligned at (x, y)
pdf.drawTextCentered(text, x, y) # Horizontally centered at x
pdf.drawTextRight(text, x, y) # Right edge at x
pdf.drawTextAligned(text, x, y, width, align) # Within a box of given widthAlignment constants:
| Constant | Value |
|---|---|
PDF_ALIGN_LEFT | 0 |
PDF_ALIGN_CENTER | 1 |
PDF_ALIGN_RIGHT | 2 |