Drawing, tables, images and output
Shapes, line style, images, tables, lists, charts, page numbers, headers, footers, watermarks, graphics state and saving.
Paragraphs#
Both functions return the Y position after the last line, which can be used to continue drawing below the paragraph.
newY = pdf.drawParagraph(text, x, y, maxWidth, lineHeight)
newY = pdf.drawParagraphAligned(text, x, y, maxWidth, lineHeight, align)lineHeight defaults to fontSize × 1.2 when NULL is passed.
Shapes#
Rectangles#
pdf.drawRect(x, y, width, height) # Stroke only
pdf.drawFilledRect(x, y, width, height) # Fill and stroke
pdf.drawFilledRectNoStroke(x, y, width, height) # Fill onlyCircles and Ellipses#
pdf.drawCircle(cx, cy, radius)
pdf.drawFilledCircle(cx, cy, radius)
pdf.drawEllipse(cx, cy, rx, ry)
pdf.drawFilledEllipse(cx, cy, rx, ry)Lines#
pdf.drawLine(x1, y1, x2, y2)
pdf.drawHorizontalRule(x, y, width) # Horizontal linePolygons#
# Points is a list of [x, y] pairs; polygon is always filled and stroked
pdf.drawPolygon([[x1, y1], [x2, y2], [x3, y3], ...])Line Style#
pdf.setLineWidth(width)
pdf.setLineCap(cap) # 0 = butt (PDF_CAP_BUTT), 1 = round, 2 = square
pdf.setLineJoin(join) # 0 = miter (PDF_JOIN_MITER), 1 = round, 2 = bevel
pdf.setDash([5, 3], 0) # Dash array and phase
pdf.resetDash() # Restore solid lineImages#
drawImage detects the file format automatically by its header bytes and embeds accordingly. No external image library is required.
pdf.drawImage(filename, x, y, width, height)| Format | Encoding | Notes |
|---|---|---|
JPEG (.jpg, .jpeg) | DCTDecode | All standard JPEG files; dimensions parsed from SOF marker |
PNG (.png) | FlateDecode | Grayscale, RGB, grayscale+alpha, RGBA; predictor 15 |
BMP (.bmp) | Raw RGB | 24-bit BMP only; rows converted from BGR bottom-up to RGB top-down |
# JPEG
pdf.drawImage("photo.jpg", 72, 500, 200, 150)
# PNG (all color types supported)
pdf.drawImage("logo.png", 72, 400, 100, 100)
# BMP (24-bit)
pdf.drawImage("scan.bmp", 72, 300, 150, 100)Width and height are the display dimensions in points (72 pt = 1 inch). The image is scaled to fit the specified rectangle regardless of its pixel dimensions.
Tables#
Explicit Column Widths#
# data is a 2D list; first row is the header
newY = pdf.drawTable(data, x, y, colWidths, options)Auto Column Widths#
# Divides totalWidth evenly across all columns
newY = pdf.drawSimpleTable(data, x, y, totalWidth, options)Both return the Y position after the last row.
Table options:
| Key | Default | Description |
|---|---|---|
:rowHeight | 20 | Row height in points |
:headerBg | [66, 133, 244] | Header background color |
:headerFg | [255, 255, 255] | Header text color |
:evenRowBg | [240, 240, 240] | Even row background color |
:borderColor | [0, 0, 0] | Cell border color |
:fontSize | 10 | Text size in points |
:showHeader | true | Show or hide the header row |
:padding | 5 | Cell padding in points |
:arabicFont | "" | Font alias for Arabic cell content (enables mixed-script cells) |
Lists#
newY = pdf.drawBulletList(items, x, y, lineHeight)
newY = pdf.drawNumberedList(items, x, y, lineHeight)items is a list of strings. lineHeight defaults to fontSize × 1.4 when NULL. Both return the Y position after the last item.
Charts#
Bar Chart#
data = [:labels = ["Q1", "Q2", "Q3"], :values = [120, 200, 170]]
pdf.drawBarChart(data, x, y, width, height, options)| Option | Description |
|---|---|
:title | Chart title drawn above the chart |
:colors | List of RGB arrays for bars |
:showValues | true to draw each bar's value above it |
Pie Chart#
data = [:labels = ["Alpha", "Beta", "Gamma"], :values = [40, 35, 25]]
pdf.drawPieChart(data, cx, cy, radius, options)| Option | Description |
|---|---|
:colors | List of RGB arrays for slices |
:showLegend | true to draw a legend to the right of the chart |
Page Numbers#
pdf.enablePageNumbers()
pdf.setPageNumberFormat("Page {n} of {total}") # {n} and {total} are placeholders
pdf.setPageNumberPosition(x, y) # Default: horizontally centered, y = 30Page numbers are added to all pages automatically at save time.
Headers and Footers#
pdf.setHeader("My Document", PDF_ALIGN_CENTER)
pdf.setFooter("Confidential", PDF_ALIGN_RIGHT)Headers and footers are drawn on every page at save time using 10 pt Helvetica in gray. The align parameter is a PDF_ALIGN_* constant and defaults to PDF_ALIGN_LEFT.
Watermarks#
pdf.drawWatermark("DRAFT", [
:fontSize = 60,
:color = [200, 200, 200],
:angle = 45
])All three options are optional. Defaults are fontSize 60, light gray, angle 45°. The watermark is centered on the current page.
Graphics State#
pdf.saveState() # Push current graphics state (color, line width, etc.)
pdf.restoreState() # Pop and restore previously saved stateOutput#
ok = pdf.save("output.pdf") # Returns true on success, false on failure