The glu.pdf module

The glu.pdf module provides low-level access to PDF generation using the baseline-pdf library. It allows direct control over PDF objects, streams, fonts, and images.

For most use cases, the glu.frontend module is recommended. The glu.pdf module is useful when you need fine-grained control over the PDF output.

local pdf = require("glu.pdf")

Module functions

Name Parameters Returns Description
pdf.new() filename (string) PDFWriter Create a new PDF writer

PDFWriter

local pdf = require("glu.pdf")
local pw = pdf.new("output.pdf")

Attributes

Name R/W Type Description
default_page_width R/W number Default width for new pages (points)
default_page_height R/W number Default height for new pages (points)
default_offset_x R/W number Default horizontal offset
default_offset_y R/W number Default vertical offset
size R number Size of the PDF document in bytes

Methods

Name Parameters Returns Description
new_object() - Object Create a new PDF object
add_page() stream Page Add a page with the given stream
load_face() filename, [index] Face Load a font face
load_image() filename, [page], [box] Image Load an image file
finish() - - Finalize and write the PDF

Example:

local pw = pdf.new("output.pdf")
pw.default_page_width = 595   -- A4 width in points
pw.default_page_height = 842  -- A4 height in points

-- Create content stream
local stream = pw:new_object()
stream.force_stream = true
stream:write("BT /F1 24 Tf 100 700 Td (Hello PDF) Tj ET")

-- Add page
local page = pw:add_page(stream)

pw:finish()

Object

PDF objects represent the building blocks of a PDF file.

local obj = pw:new_object()

Attributes

Name R/W Type Description
object_number R number The PDF object number
force_stream R/W boolean Always write as stream object
dict R/W table Dictionary entries
array R/W table Array data

Methods

Name Parameters Returns Description
write() string - Write data to the object stream
save() - - Save the object to the PDF
set_compression() level - Set compression level (0-9)

Example:

local stream = pw:new_object()
stream.force_stream = true
stream:set_compression(9)
stream:write("BT /F1 12 Tf 72 720 Td (Hello) Tj ET")
stream:save()

Page

local page = pw:add_page(stream)

Attributes

Name R/W Type Description
width R/W number Page width in points
height R/W number Page height in points
offset_x R/W number Horizontal offset
offset_y R/W number Vertical offset
object_number R/W number PDF object number
faces R/W table List of Face objects used
images R/W table List of Image objects used
dict R/W table Additional dictionary entries

Face

Font face objects for PDF text rendering.

local face = pw:load_face("fonts/MyFont.ttf", 0)

Attributes

Name R/W Type Description
internal_name R string PDF internal font name
postscript_name R string PostScript name of the font
units_per_em R number Font units per em
filename R string Path to the font file
face_id R number Internal face ID

Methods

Name Parameters Returns Description
codepoint() rune number Get codepoint for a character
codepoints() runes (table) table Get codepoints for characters
register_codepoint() codepoint - Register codepoint for subsetting
register_codepoints() codepoints (table) - Register multiple codepoints

Example:

local face = pw:load_face("fonts/CrimsonPro-Regular.ttf")
local cps = face:codepoints({"H", "e", "l", "o"})
face:register_codepoints(cps)

Image

local img = pw:load_image("image.png")
local pdf_page = pw:load_image("document.pdf", 1, "/MediaBox")

Attributes

Name R/W Type Description
internal_name R string PDF internal image name
page_number R number Page number (for PDF images)

Methods

Name Parameters Returns Description
close() - - Close and free memory
get_pdf_box_dimensions() page, box table Get box dimensions