pdf

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 9 Imported by: 0

README

gg-pdf

PDF export backend for gg's recording system.

Part of the GoGPU ecosystem.

Installation

go get github.com/gogpu/gg-pdf

Usage

Import with a blank identifier to register the PDF backend:

import (
    "github.com/gogpu/gg/recording"
    _ "github.com/gogpu/gg-pdf" // Register PDF backend
)

func main() {
    // Create a recorder
    rec := recording.NewRecorder(800, 600)

    // Draw something
    rec.SetFillRGBA(1, 0, 0, 1) // Red
    rec.DrawRectangle(100, 100, 200, 150)
    rec.Fill()

    // Finish recording
    r := rec.FinishRecording()

    // Create PDF backend
    backend, err := recording.NewBackend("pdf")
    if err != nil {
        log.Fatal(err)
    }

    // Playback to PDF
    r.Playback(backend)

    // Save to file
    if fb, ok := backend.(recording.FileBackend); ok {
        fb.SaveToFile("output.pdf")
    }
}

Multi-Page Documents

For multi-page documents, use the Document type directly:

import "github.com/gogpu/gg-pdf"

func main() {
    doc := pdf.NewDocument()
    doc.SetTitle("My Document")
    doc.SetAuthor("Author Name")

    // Page 1
    p1 := doc.NewPage(800, 600)
    // ... draw on p1 ...

    // Page 2
    p2 := doc.NewPage(600, 800)
    // ... draw on p2 ...

    // Save
    doc.SaveToFile("document.pdf")
}

Features

  • Solid color fills and strokes
  • Linear and radial gradients
  • Path operations (fill, stroke, clip)
  • Transformations
  • Stroke styles (width, cap, join, dash patterns)
  • State management (Save/Restore)
  • Multi-page documents
  • Document metadata (title, author, subject, keywords)

Limitations

  • Sweep gradients fallback to first stop color (PDF limitation)
  • Text uses Helvetica font only (custom font support planned)
  • Clipping cannot be cleared (use Save/Restore instead)

License

MIT License - see LICENSE for details.

Documentation

Overview

Package pdf provides a PDF export backend for gg's recording system.

This package registers a "pdf" backend that can be used to export recorded drawing operations to PDF format using the gxpdf library.

Usage

Import this package with a blank identifier to register the PDF backend:

import _ "github.com/gogpu/gg-pdf"

Then use recording.NewBackend("pdf") to create a PDF backend:

backend, err := recording.NewBackend("pdf")
if err != nil {
    log.Fatal(err)
}

// Record drawing operations
rec := recording.NewRecorder(800, 600)
// ... draw ...
r := rec.Finish()

// Playback to PDF backend
r.Playback(backend)

// Save to file
if fb, ok := backend.(recording.FileBackend); ok {
    fb.SaveToFile("output.pdf")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend struct {
	// contains filtered or unexported fields
}

Backend implements recording.Backend for PDF output. It uses gxpdf to generate PDF documents from recorded drawing commands.

PDF coordinates use a bottom-left origin, while gg uses a top-left origin. This backend handles the coordinate transformation automatically.

func NewBackend

func NewBackend() *Backend

NewBackend creates a new PDF backend. The backend starts in an uninitialized state. Call Begin() to initialize with specific dimensions before drawing.

func (*Backend) Begin

func (b *Backend) Begin(width, height int) error

Begin initializes the backend for rendering at the given dimensions. This creates a new PDF document with a single page of the specified size.

func (*Backend) ClearClip

func (b *Backend) ClearClip()

ClearClip removes any clipping region. Note: PDF doesn't support clearing clips, so this is a no-op warning. Proper clip management should use Save/Restore.

func (*Backend) DrawImage

func (b *Backend) DrawImage(img image.Image, src, dst recording.Rect, opts recording.ImageOptions)

DrawImage draws an image from the source rectangle to the destination rectangle.

func (*Backend) DrawText

func (b *Backend) DrawText(s string, x, y float64, face text.Face, brush recording.Brush)

DrawText draws text at the given position with the specified font face and brush.

func (*Backend) End

func (b *Backend) End() error

End finalizes the rendering and prepares the output. After End is called, WriteTo and SaveToFile can be used to get the PDF.

func (*Backend) FillPath

func (b *Backend) FillPath(path *gg.Path, brush recording.Brush, rule recording.FillRule)

FillPath fills the given path with the brush color/pattern.

func (*Backend) FillRect

func (b *Backend) FillRect(rect recording.Rect, brush recording.Brush)

FillRect fills an axis-aligned rectangle with the brush.

func (*Backend) Restore

func (b *Backend) Restore()

Restore restores the graphics state from the stack.

func (*Backend) Save

func (b *Backend) Save()

Save saves the current graphics state onto a stack.

func (*Backend) SaveToFile

func (b *Backend) SaveToFile(path string) error

SaveToFile saves the PDF to a file at the given path. This implements recording.FileBackend.

func (*Backend) SetClip

func (b *Backend) SetClip(path *gg.Path, rule recording.FillRule)

SetClip sets the clipping region to the given path.

func (*Backend) SetTransform

func (b *Backend) SetTransform(m recording.Matrix)

SetTransform sets the current transformation matrix. The transform is in gg coordinates (top-left origin).

func (*Backend) StrokePath

func (b *Backend) StrokePath(path *gg.Path, brush recording.Brush, stroke recording.Stroke)

StrokePath strokes the given path with the brush and stroke style.

func (*Backend) WriteTo

func (b *Backend) WriteTo(w io.Writer) (int64, error)

WriteTo writes the PDF to the given writer. This implements recording.WriterBackend.

type Document

type Document struct {
	// contains filtered or unexported fields
}

Document provides multi-page PDF document support. Unlike Backend which creates a single-page PDF, Document allows creating multiple pages with different sizes.

Example:

doc := pdf.NewDocument()

// First page
backend := doc.NewPage(800, 600)
// ... draw on first page ...

// Second page
backend = doc.NewPage(600, 800)
// ... draw on second page ...

doc.SaveToFile("output.pdf")

func NewDocument

func NewDocument() *Document

NewDocument creates a new multi-page PDF document.

func (*Document) Finish

func (d *Document) Finish() error

Finish finalizes all pages in the document. This must be called before WriteTo or SaveToFile.

func (*Document) NewPage

func (d *Document) NewPage(width, height int) recording.Backend

NewPage creates a new page with the given dimensions and returns a backend for it. Each call to NewPage adds a new page to the document. The returned backend can be used with recording.Playback() to draw on the page.

func (*Document) PageCount

func (d *Document) PageCount() int

PageCount returns the number of pages in the document.

func (*Document) Playback

func (d *Document) Playback(rec *recording.Recording) error

Playback replays a recording to a new page with the recording's dimensions. This is a convenience method that creates a page and plays the recording to it.

func (*Document) SaveToFile

func (d *Document) SaveToFile(path string) error

SaveToFile saves the PDF to a file at the given path. Finish() is called automatically if not already called.

func (*Document) SetAuthor

func (d *Document) SetAuthor(author string)

SetAuthor sets the document author metadata.

func (*Document) SetKeywords

func (d *Document) SetKeywords(keywords string)

SetKeywords sets the document keywords metadata.

func (*Document) SetSubject

func (d *Document) SetSubject(subject string)

SetSubject sets the document subject metadata.

func (*Document) SetTitle

func (d *Document) SetTitle(title string)

SetTitle sets the document title metadata.

func (*Document) WriteTo

func (d *Document) WriteTo(w io.Writer) (int64, error)

WriteTo writes the PDF to the given writer. Finish() is called automatically if not already called.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL