PDF Primitives

Overview

The pdf package (Layer 1) provides low-level PDF primitives. Most users only need the color functions from this package — the rest is used internally by the layout engine.

import "github.com/gpdf-dev/gpdf/pdf"

Colors

Colors are the most commonly used types from the pdf package.

Color Constructors

// RGB (float values 0.0 – 1.0)
pdf.RGB(1.0, 0.5, 0.0)     // Orange

// Hex (uint32)
pdf.RGBHex(0xFF6B6B)       // Coral
pdf.RGBHex(0x1A237E)       // Dark blue

// Grayscale (0.0 = black, 1.0 = white)
pdf.Gray(0.0)              // Black
pdf.Gray(0.5)              // Medium gray
pdf.Gray(1.0)              // White

// CMYK (for print)
pdf.CMYK(0, 1, 1, 0)      // Red in CMYK

Predefined Colors

pdf.Black    // Gray(0)
pdf.White    // Gray(1)
pdf.Red      // RGB(1, 0, 0)
pdf.Green    // RGB(0, 1, 0)
pdf.Blue     // RGB(0, 0, 1)
pdf.Yellow   // RGB(1, 1, 0)
pdf.Cyan     // RGB(0, 1, 1)
pdf.Magenta  // RGB(1, 0, 1)

Color Usage

Colors are used with TextColor() and BgColor() text options:

c.Text("Red on yellow",
    template.TextColor(pdf.Red),
    template.BgColor(pdf.Yellow),
)

And with table/line options:

// Table header
template.TableHeaderStyle(
    template.TextColor(pdf.White),
    template.BgColor(pdf.RGBHex(0x1A237E)),
)

// Line color
template.LineColor(pdf.Blue)

Color Spaces

type ColorSpace int

const (
    ColorSpaceRGB  ColorSpace = iota
    ColorSpaceGray
    ColorSpaceCMYK
)

Color Type

type Color struct {
    R, G, B float64     // RGB components (0.0 – 1.0)
    A       float64     // Alpha or K (for CMYK)
    Space   ColorSpace
}

Writer (Advanced)

The PDF Writer handles low-level PDF object creation. Most users won't need this directly.

type Writer struct {}

func NewWriter(w io.Writer) *Writer
func (pw *Writer) AllocObject() ObjectRef
func (pw *Writer) WriteObject(ref ObjectRef, obj Object) error
func (pw *Writer) RegisterFont(name string, data []byte) error
func (pw *Writer) AddPage(page *PageContent) error
func (pw *Writer) Close() error
func (pw *Writer) SetDocumentInfo(info DocumentInfo)

PDF Objects (Advanced)

Low-level PDF object types:

// Scalar types
type Name string
type LiteralString string
type HexString string
type Integer int64
type Real float64
type Boolean bool
type Null struct{}

// Composite types
type Dict map[string]Object
type Array []Object

// Stream (content, images, fonts)
type Stream struct {
    Data       []byte
    Dictionary Dict
}

// Object reference
type ObjectRef struct {
    Number     int
    Generation int
}

Font Package (Advanced)

The pdf/font sub-package handles TrueType font parsing and subsetting:

import "github.com/gpdf-dev/gpdf/pdf/font"

// Parse a TrueType font
ttf, err := font.ParseTrueType(data)

// Get font name and metrics
name := ttf.Name()
metrics := ttf.Metrics()  // Ascender, Descender, CapHeight, XHeight

// Create a subset with only used glyphs
subset, err := ttf.Subset(runes)

// Measure text width
width := font.MeasureString(ttf, "Hello", 12.0)

// Break text into lines
lines := font.LineBreak(ttf, text, 12.0, maxWidth)

Architecture

Layer 3: template ─── Builder API, JSON Schema, Components
    │
    ▼
Layer 2: document ─── Nodes, Box Model, Layout Engine
    │
    ▼
Layer 1: pdf ──────── Writer, Streams, Fonts, Images
    │
    ▼
          io.Writer (file, HTTP response, buffer...)

Each layer depends only on the layer below it — never upward.