Images

Image Embedding

Embed PNG and JPEG images inline or side-by-side in grid columns.

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("Image Examples", template.FontSize(18), template.Bold())
        c.Spacer(document.Mm(5))
    })
})

// Create test images
bluePNG := generatePNG(200, 100)  // []byte of PNG data
redJPEG := generateJPEG(200, 100) // []byte of JPEG data

// PNG image
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("PNG image (blue):")
        c.Spacer(document.Mm(2))
        c.Image(bluePNG)
        c.Spacer(document.Mm(5))
    })
})

// JPEG image
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("JPEG image (red):")
        c.Spacer(document.Mm(2))
        c.Image(redJPEG)
        c.Spacer(document.Mm(5))
    })
})

// Images in columns
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("Images side by side in grid columns:")
        c.Spacer(document.Mm(2))
    })
})

greenPNG := generatePNG(150, 80)  // []byte of PNG data
yellowPNG := generatePNG(150, 80) // []byte of PNG data

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(6, func(c *template.ColBuilder) {
        c.Text("Green PNG")
        c.Image(greenPNG)
    })
    r.Col(6, func(c *template.ColBuilder) {
        c.Text("Yellow PNG")
        c.Image(yellowPNG)
    })
})

Image Fit Modes

Control how images are sized within their container.

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("Image Fit Options", template.FontSize(18), template.Bold())
        c.Spacer(document.Mm(5))
    })
})

bluePNG := generatePNG(300, 200) // []byte of PNG data

// FitWidth
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("FitWidth(80mm):")
        c.Spacer(document.Mm(2))
        c.Image(bluePNG, template.FitWidth(document.Mm(80)))
        c.Spacer(document.Mm(5))
    })
})

// FitHeight
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("FitHeight(30mm):")
        c.Spacer(document.Mm(2))
        c.Image(bluePNG, template.FitHeight(document.Mm(30)))
        c.Spacer(document.Mm(5))
    })
})

// Default (no fit options)
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("Default (no fit options):")
        c.Spacer(document.Mm(2))
        c.Image(bluePNG)
    })
})

Advanced Image Features

FitMode comparison, image alignment, PNG transparency, and file path loading.

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("Advanced Image Features", template.FontSize(18), template.Bold())
        c.Spacer(document.Mm(5))
    })
})

bluePNG := generatePNG(300, 200) // []byte of PNG data

// --- FitMode examples ---
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("FitMode Comparison", template.FontSize(14), template.Bold())
        c.Spacer(document.Mm(3))
    })
})

// FitContain (default) and FitStretch
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(6, func(c *template.ColBuilder) {
        c.Text("FitContain (default):", template.FontSize(9))
        c.Spacer(document.Mm(1))
        c.Image(bluePNG,
            template.FitWidth(document.Mm(60)),
            template.FitHeight(document.Mm(30)),
            template.WithFitMode(document.FitContain),
        )
    })
    r.Col(6, func(c *template.ColBuilder) {
        c.Text("FitStretch:", template.FontSize(9))
        c.Spacer(document.Mm(1))
        c.Image(bluePNG,
            template.FitWidth(document.Mm(60)),
            template.FitHeight(document.Mm(30)),
            template.WithFitMode(document.FitStretch),
        )
    })
})

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Spacer(document.Mm(3))
    })
})

// FitOriginal and FitCover
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(6, func(c *template.ColBuilder) {
        c.Text("FitOriginal:", template.FontSize(9))
        c.Spacer(document.Mm(1))
        smallPNG := generatePNG(80, 50) // []byte of smaller PNG
        c.Image(smallPNG, template.WithFitMode(document.FitOriginal))
    })
    r.Col(6, func(c *template.ColBuilder) {
        c.Text("FitCover:", template.FontSize(9))
        c.Spacer(document.Mm(1))
        c.Image(bluePNG,
            template.FitWidth(document.Mm(60)),
            template.FitHeight(document.Mm(30)),
            template.WithFitMode(document.FitCover),
        )
    })
})

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Spacer(document.Mm(5))
    })
})

// --- Align examples ---
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("Image Alignment", template.FontSize(14), template.Bold())
        c.Spacer(document.Mm(3))
    })
})

redPNG := generatePNG(100, 60) // []byte of PNG data

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(4, func(c *template.ColBuilder) {
        c.Text("AlignLeft", template.FontSize(9))
        c.Image(redPNG,
            template.FitWidth(document.Mm(30)),
            template.WithAlign(document.AlignLeft),
        )
    })
    r.Col(4, func(c *template.ColBuilder) {
        c.Text("AlignCenter", template.FontSize(9))
        c.Image(redPNG,
            template.FitWidth(document.Mm(30)),
            template.WithAlign(document.AlignCenter),
        )
    })
    r.Col(4, func(c *template.ColBuilder) {
        c.Text("AlignRight", template.FontSize(9))
        c.Image(redPNG,
            template.FitWidth(document.Mm(30)),
            template.WithAlign(document.AlignRight),
        )
    })
})

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Spacer(document.Mm(5))
    })
})

// --- PNG transparency ---
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("PNG Transparency (Alpha Channel)", template.FontSize(14), template.Bold())
        c.Spacer(document.Mm(3))
    })
})

alphaPNG := generatePNGAlpha(200, 100)    // []byte of PNG with alpha
gradientPNG := generatePNGGradient(200, 100) // []byte of PNG with gradient alpha

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(6, func(c *template.ColBuilder) {
        c.Text("Checkerboard alpha:", template.FontSize(9))
        c.Spacer(document.Mm(1))
        c.Image(alphaPNG, template.FitWidth(document.Mm(60)))
    })
    r.Col(6, func(c *template.ColBuilder) {
        c.Text("Gradient alpha:", template.FontSize(9))
        c.Spacer(document.Mm(1))
        c.Image(gradientPNG, template.FitWidth(document.Mm(60)))
    })
})

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Spacer(document.Mm(5))
    })
})

// --- File path loading ---
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("File Path Loading", template.FontSize(14), template.Bold())
        c.Spacer(document.Mm(3))
    })
})

yellowPNG := generatePNG(150, 100) // []byte of PNG data

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("Image loaded from file path:", template.FontSize(9))
        c.Spacer(document.Mm(1))
        c.Image(yellowPNG, template.FitWidth(document.Mm(50)))
    })
})