pdfrender

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	LogoBaseRadius = 16.5
	LogoTplExtent  = 3.4 * LogoBaseRadius
	LogoTplCenter  = LogoTplExtent / 2
)
View Source
const (
	TextAlignLeft   = pdfdom.TextAlignLeft
	TextAlignCenter = pdfdom.TextAlignCenter
	TextAlignRight  = pdfdom.TextAlignRight
	TextFitClip     = pdfdom.TextFitClip
	TextFitWrap     = pdfdom.TextFitWrap
)

Variables

This section is empty.

Functions

func CreateRingLogoTemplate

func CreateRingLogoTemplate(pdf *gofpdf.Fpdf, r, cx, cy float64) gofpdf.Template

func CreateRingLogoTemplateWithLayers

func CreateRingLogoTemplateWithLayers(pdf *gofpdf.Fpdf, r, cx, cy float64, renderBg, renderFg bool) gofpdf.Template

func ExtractRunningFooterElement

func ExtractRunningFooterElement(elements []pdfdom.PDFElementNode, name string) (matchedName string, elem pdfdom.PDFElementNode, remaining []pdfdom.PDFElementNode)

ExtractRunningFooterElement finds the first element with position="running(name)" and returns the matched name, the element, and a filtered slice without it. When name is empty, the first element with any position="running(...)" value is matched. Returns ("", nil, elements) when no match is found.

func RenderDocTemplateFlow

func RenderDocTemplateFlow(l *LayoutPDF, elements []pdfdom.PDFElementNode) error

RenderDocTemplateFlow renders a document flow from pre-built PDFDOM elements. Elements with CSS position:running(name) are extracted, registered as running footer templates, and stamped on every page via BeginPage.

func RenderDocTemplateOverlay

func RenderDocTemplateOverlay(l *LayoutPDF, elements []pdfdom.PDFElementNode) error

RenderDocTemplateOverlay renders page-local content without advancing the persistent normal-flow cursor.

func RenderLogoPDF

func RenderLogoPDF(outputPath string, pageWidth, pageHeight, x, y, r float64, renderBg, renderFg bool) error

func ShouldBreakPageAfter

func ShouldBreakPageAfter(node pdfdom.PDFElementNode) bool

ShouldBreakPageAfter reports whether node carries a break-after:page attribute.

func ShouldBreakPageBefore

func ShouldBreakPageBefore(node pdfdom.PDFElementNode) bool

ShouldBreakPageBefore reports whether node carries a break-before:page attribute.

Types

type CellDef

type CellDef struct {
	Text          string
	SubText       string
	FontFace      string
	FontStyle     string
	FontSize      float64
	FontColor     string
	Background    string
	LineHeight    float64
	Value         interface{}
	Type          string
	Align         string
	PaddingTop    float64
	PaddingRight  float64
	PaddingBottom float64
	PaddingLeft   float64
	Bold          bool
	Small         bool
	SubFontFace   string
	SubFontColor  string
	SubFontSize   float64
	NoWrap        bool
	Colspan       int
}

CellDef defines a cell.

type CellFormatter

type CellFormatter interface {
	FormatCurrency(value float64) string
	FormatFloat(value float64, decimals int) string
	FormatDate(dateStr string, long bool) string
	FormatWorkWeek(dateStr string) string
	FormatDuration(hours float64) string
}

CellFormatter formats typed cell values into locale-aware strings. Implemented by *invoice.Formatter; defined here to avoid an import cycle.

type ColumnDef

type ColumnDef struct {
	Width    float64
	MinWidth float64
	MaxWidth float64
	Align    string
}

ColumnDef defines a column.

type ElemBr

type ElemBr = pdfdom.ElemBr

type ElemImg

type ElemImg = pdfdom.ElemImg

type FontGlyphRegistry

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

FontGlyphRegistry tracks used glyphs per font for later subsetting.

func NewFontGlyphRegistry

func NewFontGlyphRegistry() *FontGlyphRegistry

func (*FontGlyphRegistry) Record

func (g *FontGlyphRegistry) Record(fontFace, text string)

func (*FontGlyphRegistry) Snapshot

func (g *FontGlyphRegistry) Snapshot() map[string][]rune

func (*FontGlyphRegistry) UsedGlyphs

func (g *FontGlyphRegistry) UsedGlyphs(fontFace string) []rune

type FontRegistration

type FontRegistration struct {
	Family  string
	Style   string
	Sources []string
	Data    []byte
	Name    string
}

FontRegistration declares one font family/style with ordered candidate file paths. The first existing file path will be loaded.

type ImageLoader

type ImageLoader func(name string) (ImageResource, error)

type ImageResource

type ImageResource struct {
	Name string
	Type string
	Data []byte
}

type LayoutOptions

type LayoutOptions struct {
	FontRegistrations  []FontRegistration
	ImageSearchDirs    []string
	ImageLoader        ImageLoader
	StrictRenderErrors bool
	Context            context.Context
	MaxPages           int
	TemplateFactories  map[string]TemplateFactory
	MetadataTime       time.Time
}

LayoutOptions controls optional renderer initialization behavior.

type LayoutPDF

type LayoutPDF struct {
	PDF       *gofpdf.Fpdf
	Formatter *format.Formatter
	I18n      *i18n.I18n
	// contains filtered or unexported fields
}

LayoutPDF holds the shared PDF document and pre-built templates used by all standalone rendering subcommands. Build one with NewLayoutPDF; use the exported methods to compose pages.

func NewLayoutPDF

func NewLayoutPDF(defaultPage, firstPage templateload.PageSettings, i18nInst *i18n.I18n, formatter *format.Formatter) (*LayoutPDF, error)

FontRegistration declares one font family/style with ordered candidate file paths. The first existing file path will be loaded. NewLayoutPDF creates a LayoutPDF from page settings and locale/format helpers.

func NewLayoutPDFWithOptions

func NewLayoutPDFWithOptions(defaultPage, firstPage templateload.PageSettings, i18nInst *i18n.I18n, formatter *format.Formatter, options LayoutOptions) (*LayoutPDF, error)

NewLayoutPDFWithOptions creates a LayoutPDF and applies optional profile configuration such as custom font registrations.

func (*LayoutPDF) BeginPage

func (l *LayoutPDF) BeginPage(page int) error

BeginPage handles pagination (AddPage on page > 1) and stamps the running footer template on every page.

func (*LayoutPDF) CellDefFromTableCell

func (l *LayoutPDF) CellDefFromTableCell(cellElem pdfdom.PDFElementNode, isHeader bool) CellDef

CellDefFromTableCell builds a CellDef from an HTML table cell element.

func (*LayoutPDF) CurrentFlowBottom

func (l *LayoutPDF) CurrentFlowBottom() float64

CurrentFlowBottom returns the y coordinate of the bottom of the content area.

func (*LayoutPDF) CurrentFlowBox

func (l *LayoutPDF) CurrentFlowBox() (x, y, width float64)

CurrentFlowBox returns the current page's content box origin and width.

func (*LayoutPDF) EnsureTotalPagesAtLeast

func (l *LayoutPDF) EnsureTotalPagesAtLeast(n int)

EnsureTotalPagesAtLeast sets the total page count to n if n is larger than the current tracked value.

func (*LayoutPDF) NextFlowPage

func (l *LayoutPDF) NextFlowPage() error

NextFlowPage advances to the next page in the flow.

func (*LayoutPDF) PageTemplateData

func (l *LayoutPDF) PageTemplateData(pageNumber, pageTotal int) map[string]any

PageTemplateData returns page/runtime metadata intended for template payloads. Values are resolved from the current layout state and optional explicit pageNumber/pageTotal overrides.

func (*LayoutPDF) RenderCreateTemplateElement

func (l *LayoutPDF) RenderCreateTemplateElement(elem *pdfdom.ElemCreateTemplate) error

RenderCreateTemplateElement captures an element's children in a named template.

func (*LayoutPDF) RenderFinalFlowPageNums

func (l *LayoutPDF) RenderFinalFlowPageNums()

RenderFinalFlowPageNums re-renders page numbers on all non-first pages after the total page count is known.

func (*LayoutPDF) RenderPageNum

func (l *LayoutPDF) RenderPageNum(page, pageCount int)

RenderPageNum draws the current/total page number in the top-right corner. It is a no-op on page 1.

func (*LayoutPDF) RenderUseTemplateElement

func (l *LayoutPDF) RenderUseTemplateElement(elem *pdfdom.ElemUseTemplate, fallbackX, fallbackY float64) (renderedHeight float64, absolute bool, err error)

RenderUseTemplateElement renders a <use-template> element using the named template. fallbackX and fallbackY are the current document-flow coordinates; they are used when the element does not carry explicit x or y attributes. Returns the rendered height, whether an explicit position was given (absolute=true means the caller should NOT advance the flow cursor), and any error.

func (*LayoutPDF) ResolveFlowPlacement

func (l *LayoutPDF) ResolveFlowPlacement(node pdfdom.PDFElementNode, flowX, flowY, flowW float64) (x, y, width float64, absolute bool)

ResolveFlowPlacement determines the final position and width of a flow element, applying absolute positioning when declared.

func (*LayoutPDF) ResolvePDFTextNode

func (l *LayoutPDF) ResolvePDFTextNode(node *pdfdom.PDFTextNode) string

func (*LayoutPDF) SetDeferFlowPageNum

func (l *LayoutPDF) SetDeferFlowPageNum(v bool)

SetDeferFlowPageNum controls whether page numbers are deferred until RenderFinalFlowPageNums.

func (*LayoutPDF) SetPageNumRenderer

func (l *LayoutPDF) SetPageNumRenderer(fn func(layout *LayoutPDF, page, pageCount int))

SetPageNumRenderer sets the callback used to render page numbers.

func (*LayoutPDF) SetRunningFooterTemplateFromElement

func (l *LayoutPDF) SetRunningFooterTemplateFromElement(name string, footerElem pdfdom.PDFElementNode) error

func (*LayoutPDF) SetWarningFunc

func (l *LayoutPDF) SetWarningFunc(fn func(format string, args ...any))

SetWarningFunc sets the warning sink used by non-fatal renderer warnings. Passing nil disables warning output.

func (*LayoutPDF) StartFlow

func (l *LayoutPDF) StartFlow()

StartFlow initialises page tracking and persistent normal-flow state.

func (*LayoutPDF) TableDefFromElement

func (l *LayoutPDF) TableDefFromElement(table *pdfdom.ElemTable, availableWidth float64) (*TableDef, error)

func (*LayoutPDF) TemplateByName

func (l *LayoutPDF) TemplateByName(name string) (gofpdf.Template, error)

TemplateByName returns the pre-built gofpdf.Template for a named slot.

func (*LayoutPDF) TotalPages

func (l *LayoutPDF) TotalPages() int

TotalPages returns the current tracked total page count.

type PDFDocumentNode

type PDFDocumentNode = pdfdom.PDFDocumentNode

type PDFElementNode

type PDFElementNode = pdfdom.PDFElementNode

type PDFNode

type PDFNode = pdfdom.PDFNode

type PDFTextBox

type PDFTextBox = pdfdom.PDFTextBox

type PDFTextEngine

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

PDFTextEngine hides low-level PDF calls and renders declarative PDFNode trees.

func NewPDFTextEngine

func NewPDFTextEngine(pdf *gofpdf.Fpdf, i18n Translator) *PDFTextEngine

func (*PDFTextEngine) GlyphRegistry

func (e *PDFTextEngine) GlyphRegistry() *FontGlyphRegistry

func (*PDFTextEngine) Measure

func (e *PDFTextEngine) Measure(node PDFNode) (PDFTextMetrics, error)

func (*PDFTextEngine) MeasureInBox

func (e *PDFTextEngine) MeasureInBox(node PDFNode, box *PDFTextBox) (PDFTextMetrics, error)

func (*PDFTextEngine) Render

func (e *PDFTextEngine) Render(node PDFNode) (PDFTextMetrics, error)

func (*PDFTextEngine) RenderInBox

func (e *PDFTextEngine) RenderInBox(node PDFNode, box *PDFTextBox) (PDFTextMetrics, error)

func (*PDFTextEngine) SetDefaultStyle

func (e *PDFTextEngine) SetDefaultStyle(style PDFTextStyle)

func (*PDFTextEngine) SetImageLoader

func (e *PDFTextEngine) SetImageLoader(loader ImageLoader)

func (*PDFTextEngine) SetImageSearchDirs

func (e *PDFTextEngine) SetImageSearchDirs(paths []string)

func (*PDFTextEngine) SetValueFormatter

func (e *PDFTextEngine) SetValueFormatter(formatter pdfdom.ValueFormatter)

type PDFTextMetrics

type PDFTextMetrics struct {
	Width      float64
	Height     float64
	LineCount  int
	WasClipped bool
}

PDFTextMetrics contains calculated dimensions and rendering metadata.

type PDFTextNode

type PDFTextNode = pdfdom.PDFTextNode

type PDFTextStyle

type PDFTextStyle = pdfdom.PDFTextStyle

type PageLimitError

type PageLimitError struct {
	Limit     int
	Requested int
}

func (*PageLimitError) Error

func (e *PageLimitError) Error() string

func (*PageLimitError) Is

func (e *PageLimitError) Is(target error) bool

type RowDef

type RowDef struct {
	Cells      []CellDef
	Height     float64
	Header     bool
	Background string
	Fill       string
	Border     bool
	Stroke     *bool
}

RowDef defines a row.

type TableDef

type TableDef struct {
	Title           string
	Width           float64
	TableLayout     string
	Padding         float64
	RowHeightMin    float64
	MarginTop       float64
	MarginBottom    float64
	MarginTopSet    bool
	MarginBottomSet bool
	Background      string
	BorderColor     string
	BorderStyle     string
	BorderWidth     float64
	Columns         []ColumnDef
	Rows            []RowDef
}

TableDef defines a table structure.

type TableRenderer

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

TableRenderer handles rendering tables in PDF documents.

func NewTableRenderer

func NewTableRenderer(pdf *gofpdf.Fpdf, formatter CellFormatter) *TableRenderer

NewTableRenderer creates a new table renderer.

func (*TableRenderer) MeasureTableHeight

func (tr *TableRenderer) MeasureTableHeight(table *TableDef) (float64, error)

MeasureTableHeight returns the height that RenderTable would consume (excluding the trailing 5pt cursor gap).

func (*TableRenderer) RenderTable

func (tr *TableRenderer) RenderTable(table *TableDef) error

RenderTable renders a table at the current PDF cursor position.

type TemplateFactory

type TemplateFactory func(pdf *gofpdf.Fpdf) gofpdf.Template

type TextAlign

type TextAlign = pdfdom.TextAlign

type Translator

type Translator = pdfdom.Translator

Jump to

Keyboard shortcuts

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