Basics
Hello World
The simplest possible PDF — a single page with bold text.
package main
import (
"github.com/gpdf-dev/gpdf/document"
"github.com/gpdf-dev/gpdf/template"
)
func main() {
doc := template.New(
template.WithPageSize(document.A4),
template.WithMargins(document.UniformEdges(document.Mm(20))),
)
page := doc.AddPage()
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Hello, World!", template.FontSize(24), template.Bold())
})
})
doc.RenderToFile("hello_world.pdf")
}
Text Styling
Demonstrates font sizes, bold/italic, text colors, background colors, and text alignment.
doc := template.New(
template.WithPageSize(document.A4),
template.WithMargins(document.UniformEdges(document.Mm(20))),
)
page := doc.AddPage()
// Title
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Text Styling Examples", template.FontSize(20), template.Bold())
c.Spacer(document.Mm(5))
})
})
// Font sizes
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Font Size 8pt", template.FontSize(8))
c.Text("Font Size 12pt (default)", template.FontSize(12))
c.Text("Font Size 18pt", template.FontSize(18))
c.Text("Font Size 24pt", template.FontSize(24))
c.Text("Font Size 36pt", template.FontSize(36))
c.Spacer(document.Mm(5))
})
})
// Font weight and style
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Normal text")
c.Text("Bold text", template.Bold())
c.Text("Italic text", template.Italic())
c.Text("Bold + Italic text", template.Bold(), template.Italic())
c.Spacer(document.Mm(5))
})
})
// Text colors
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Red text", template.TextColor(pdf.Red))
c.Text("Green text", template.TextColor(pdf.Green))
c.Text("Blue text", template.TextColor(pdf.Blue))
c.Text("Custom color (orange)", template.TextColor(pdf.RGB(1.0, 0.5, 0.0)))
c.Text("Hex color (#336699)", template.TextColor(pdf.RGBHex(0x336699)))
c.Spacer(document.Mm(5))
})
})
// Background colors
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Yellow background", template.BgColor(pdf.Yellow))
c.Text("Cyan background", template.BgColor(pdf.Cyan))
c.Text("White text on dark background",
template.TextColor(pdf.White),
template.BgColor(pdf.RGBHex(0x333333)),
)
c.Spacer(document.Mm(5))
})
})
// Text alignment
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Left aligned (default)", template.AlignLeft())
c.Text("Center aligned", template.AlignCenter())
c.Text("Right aligned", template.AlignRight())
})
})
Colors
Comprehensive color system with predefined colors, RGB, hex, grayscale, and background swatches.
doc := template.New(
template.WithPageSize(document.A4),
template.WithMargins(document.UniformEdges(document.Mm(20))),
)
page := doc.AddPage()
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Color System Examples", template.FontSize(18), template.Bold())
c.Spacer(document.Mm(5))
})
})
// Predefined colors
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Predefined Colors:", template.Bold())
c.Text("Red", template.TextColor(pdf.Red))
c.Text("Green", template.TextColor(pdf.Green))
c.Text("Blue", template.TextColor(pdf.Blue))
c.Text("Yellow", template.TextColor(pdf.Yellow))
c.Text("Cyan", template.TextColor(pdf.Cyan))
c.Text("Magenta", template.TextColor(pdf.Magenta))
c.Spacer(document.Mm(5))
})
})
// RGB colors (0.0-1.0)
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("RGB Colors (float):", template.Bold())
c.Text("RGB(1.0, 0.5, 0.0) - Orange", template.TextColor(pdf.RGB(1.0, 0.5, 0.0)))
c.Text("RGB(0.5, 0.0, 0.5) - Purple", template.TextColor(pdf.RGB(0.5, 0.0, 0.5)))
c.Text("RGB(0.0, 0.5, 0.5) - Teal", template.TextColor(pdf.RGB(0.0, 0.5, 0.5)))
c.Spacer(document.Mm(5))
})
})
// Hex colors
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Hex Colors:", template.Bold())
c.Text("#FF6B6B - Coral", template.TextColor(pdf.RGBHex(0xFF6B6B)))
c.Text("#4ECDC4 - Turquoise", template.TextColor(pdf.RGBHex(0x4ECDC4)))
c.Text("#45B7D1 - Sky Blue", template.TextColor(pdf.RGBHex(0x45B7D1)))
c.Text("#96CEB4 - Sage", template.TextColor(pdf.RGBHex(0x96CEB4)))
c.Spacer(document.Mm(5))
})
})
// Grayscale
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Grayscale:", template.Bold())
c.Text("Gray(0.0) - Black", template.TextColor(pdf.Gray(0.0)))
c.Text("Gray(0.3) - Dark gray", template.TextColor(pdf.Gray(0.3)))
c.Text("Gray(0.5) - Medium gray", template.TextColor(pdf.Gray(0.5)))
c.Text("Gray(0.7) - Light gray", template.TextColor(pdf.Gray(0.7)))
c.Spacer(document.Mm(5))
})
})
// Background color swatches
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Background Color Swatches:", template.Bold())
})
})
page.AutoRow(func(r *template.RowBuilder) {
r.Col(3, func(c *template.ColBuilder) {
c.Text(" Red ", template.TextColor(pdf.White), template.BgColor(pdf.Red))
})
r.Col(3, func(c *template.ColBuilder) {
c.Text(" Green ", template.TextColor(pdf.White), template.BgColor(pdf.Green))
})
r.Col(3, func(c *template.ColBuilder) {
c.Text(" Blue ", template.TextColor(pdf.White), template.BgColor(pdf.Blue))
})
r.Col(3, func(c *template.ColBuilder) {
c.Text(" Yellow ", template.BgColor(pdf.Yellow))
})
})