Command line interface
The command line interface
The command line interface glu makes it possible to create documents without a Go installation.
It uses Lua as scripting language, providing a simple yet powerful way to create PDF documents.
Installation
Download the latest binaries at https://github.com/speedata/glu/releases/latest.
If you have a Go installation, you can also build from source:
git clone https://github.com/speedata/glu
cd glu
rake buildSample document
Create a file hello.lua:
local frontend = require("glu.frontend")
-- Load a font
local doc = frontend.new("hello.pdf")
local ff = doc:new_font_family("text")
local fs = frontend.fontsource({ location = "fonts/CrimsonPro-Regular.ttf" })
ff:add_member(fs, "regular", "normal")
-- Create text content
local txt = frontend.text({
font_family = ff,
font_size = "12pt",
color = "black"
})
local str = [[The quick brown fox jumps over the lazy dog with a very
long line that should be wrapped at some point. This is a test to see
how the text is formatted when it is too long to fit on one line.]]
-- Remove newlines
str = str:gsub("\n", " ")
txt:append(str)
-- Format paragraph and create page
local vlist = doc:format_paragraph(txt, "225pt", { leading = "14pt" })
local page = doc:new_page()
page.width = "210mm"
page.height = "297mm"
page:output_at("1cm", "28cm", vlist)
page:shipout()
doc:finish()
print("Created hello.pdf")Run with:
glu hello.luaThe documentation is split into parts: general information with code snippets, and the module references.
- Using glu
- glu.frontend module – high-level typesetting API
- glu.pdf module – low-level PDF writing
- xml.cxpath module – XPath XML querying
- Backend modules – glu, glu.node, glu.font
Dimensions
All dimension parameters accept:
- Numbers – interpreted as points:
12,595.28 - Strings with units –
"12pt","1cm","10mm","1in" - ScaledPoint – type-safe dimension values (see ScaledPoint)
page.width = "21cm"
page:output_at("2cm", "27cm", vlist)
txt:set("font_size", "14pt")
doc:format_paragraph(txt, 400) -- 400 points
-- Using ScaledPoint for calculations
local frontend = require("glu.frontend")
local y = frontend.sp_string("28cm")
local h = vlist.height -- returns ScaledPoint
page:output_at("2cm", y - h - "1cm", vlist)Document properties
local doc = frontend.new("output.pdf")
doc.title = "My Document"
doc.author = "Author Name"
doc.subject = "Document Subject"
doc.creator = "glu"
doc.keywords = "pdf, lua, typesetting"
doc.language = doc:get_language("en")Attachments
doc:attach_file({
filename = "data.xml",
name = "data.xml", -- visible name (optional)
description = "XML data", -- description (optional)
mimetype = "text/xml" -- MIME type (optional)
})Image inclusion
-- Load an image file
local imgfile = doc:load_imagefile("image.pdf", 1, "/MediaBox")
-- Arguments: filename, page (optional), box (optional)
-- Create an image node for placement
local imgnode = doc:create_image_node(imgfile, 1, "/MediaBox")
imgnode.width = "5cm"
imgnode.height = "3cm"
-- Pack into vlist and place on page
local node = require("glu.node")
local vl = node.vpack(imgnode)
page:output_at("2cm", "25cm", vl)Set the PDF format
doc.format = "PDF/A-3b" -- or "PDF", "PDF/X-3", "PDF/X-4", "PDF/UA"For PDF/A-3b with ZUGFeRD support:
doc.format = "PDF/A-3b"
doc.additional_xml_metadata = [[<rdf:Description ...>...</rdf:Description>]]
-- Load ICC color profile
local colorprofile = doc:load_colorprofile("AdobeRGB1998.icc")
colorprofile.identifier = "AdobeRGB1998"
colorprofile.registry = "Adobe"
colorprofile.info = "Adobe RGB (1998)"
colorprofile.condition = "RGB"
colorprofile.colors = 3Page dimensions
local page = doc:new_page()
page.width = "210mm" -- A4 width
page.height = "297mm" -- A4 height