Documentation
¶
Overview ¶
Package pdf provides composable PDF rendering primitives structured like the go-mx html package, but targeting the codeberg.org/go-pdf/fpdf renderer instead of an HTML markup writer.
The correspondence to the html package is deliberate:
- Component is the html Component: anything that can draw implements Render(context.Context, *Renderer) error.
- Renderer replaces the markup writer. It embeds *fpdf.Fpdf, so every fpdf method is available directly and any primitive can be expressed in raw fpdf when needed.
- Components, If, Iff, ForEach and ForEachIter mirror their mx counterparts for composing and conditionally rendering content.
- Document is html.Document: it carries metadata, page setup and a body, and renders to a Renderer, an io.Writer, a file or an http.ResponseWriter.
Unlike HTML, PDF has no element tree. fpdf is an imperative, stateful drawing API: the current page, cursor, font and colors persist across calls. The primitives here are therefore thin wrappers around that state machine rather than a retained tree. They divide into:
- text: Text, Textf, Cell, CellFormat, MultiCell, Paragraph, TextAt, Ln, NewLine
- vector: Line, Rect, RoundedRect, Circle, Ellipse, Polygon
- images: Image (file), ImageReader / ImageBytes (in memory)
- state: Font, FontSize, TextColor, FillColor, DrawColor, LineWidth, LineCap, LineJoin, X, Y, XY, MoveDown, MoveRight, and Save to scope state changes to a group of children
- layout: Page, PageFormat, Document
Closed-set values are typed enums with generated Valid, Validate, Enums and EnumStrings methods (Orientation, Unit, PageSize, FontStyle, HAlign, VAlign, Border, DrawOp, LnPos, LineCapStyle, LineJoinStyle, ImageType); FontStyle enumerates all sixteen bold/italic/underline/ strike-out combinations and Border all sixteen left/top/right/bottom edge combinations plus the "1" full-border shorthand, while horizontal and vertical cell alignment are two independent single-choice axes (HAlign, VAlign). The only genuinely open value is the font family name (Helvetica, …, or any registered font), which stays a plain string constant. There is also an RGB Color type with named colors and a Hex parser.
A minimal document:
doc := pdf.NewDocument("Hello",
pdf.Font(pdf.Helvetica, pdf.StyleBold, 24),
pdf.Paragraph("Hello, PDF!"),
)
err := doc.OutputFile(ctx, "hello.pdf")
See README.md for the limitations of the fpdf renderer relative to the PDF specification.
Index ¶
- Constants
- Variables
- type Border
- type Color
- type Component
- func Cell(w, h float64, text string) Component
- func CellFormat(w, h float64, text string, border Border, ln LnPos, hAlign HAlign, ...) Component
- func Circle(x, y, rad float64, op DrawOp) Component
- func DefaultAsComponent(c any) Component
- func DrawColor(c Color) Component
- func Ellipse(x, y, rx, ry, degRotate float64, op DrawOp) Component
- func FillColor(c Color) Component
- func Font(family string, style FontStyle, size float64) Component
- func FontSize(size float64) Component
- func Image(file string, x, y, w, h float64) Component
- func ImageBytes(name string, imageType ImageType, data []byte, x, y, w, h float64) Component
- func ImageReader(name string, imageType ImageType, src io.Reader, x, y, w, h float64) Component
- func Line(x1, y1, x2, y2 float64) Component
- func LineCap(style LineCapStyle) Component
- func LineJoin(style LineJoinStyle) Component
- func LineWidth(w float64) Component
- func Ln(h float64) Component
- func MoveDown(dy float64) Component
- func MoveRight(dx float64) Component
- func MultiCell(w, h float64, text string, border Border, hAlign HAlign, fill bool) Component
- func NewLine() Component
- func Page(children ...any) Component
- func PageFormat(orientation Orientation, size PageSize, children ...any) Component
- func Paragraph(text string) Component
- func Polygon(op DrawOp, points ...Point) Component
- func Rect(x, y, w, h float64, op DrawOp) Component
- func RoundedRect(x, y, w, h, radius float64, op DrawOp) Component
- func Save(children ...any) Component
- func TextAt(x, y float64, text string) Component
- func TextColor(c Color) Component
- func X(x float64) Component
- func XY(x, y float64) Component
- func Y(y float64) Component
- type ComponentFunc
- type Components
- type Document
- func (d *Document) Bytes(ctx context.Context) ([]byte, error)
- func (d *Document) NewRenderer() *Renderer
- func (d *Document) Output(ctx context.Context, w io.Writer) error
- func (d *Document) OutputFile(ctx context.Context, filename string) error
- func (d *Document) Render(ctx context.Context, r *Renderer) error
- func (d *Document) ServeHTTP(w http.ResponseWriter, req *http.Request)
- type DrawOp
- type FontStyle
- type HAlign
- type IfElse
- type ImageType
- type LineCapStyle
- type LineJoinStyle
- type LnPos
- type Margins
- type Orientation
- type PageSize
- type Point
- type Renderer
- func (r *Renderer) LineHeight() float64
- func (r *Renderer) LoadUTF8FontBytes(family string, style FontStyle, ttf []byte)
- func (r *Renderer) LoadUTF8FontReader(family string, style FontStyle, src io.Reader)
- func (r *Renderer) SetLineHeight(h float64)
- func (r *Renderer) SetTranslator(translate func(string) string)
- func (r *Renderer) Str(s string) string
- type Text
- type Unit
- type VAlign
Constants ¶
const ( DefaultFontFamily = Helvetica DefaultFontSize = 12.0 )
Defaults applied by the Renderer constructors and Document so text can be drawn without explicit setup.
const ( Helvetica = "Helvetica" // sans-serif (alias of Arial) Arial = "Arial" // sans-serif (alias of Helvetica) Times = "Times" // serif Courier = "Courier" // fixed-width Symbol = "Symbol" // symbolic ZapfDingbats = "ZapfDingbats" // symbolic )
Standard (core) font families, which need no font files. These are untyped string constants because any family registered with the raw fpdf AddFont / AddUTF8Font API (or Renderer.LoadUTF8FontBytes) is an equally valid family name and can be passed as a plain string.
const ContentType = "application/pdf"
ContentType is the MIME type of PDF output.
Variables ¶
var ( Black = Color{0, 0, 0} White = Color{255, 255, 255} Red = Color{255, 0, 0} Green = Color{0, 128, 0} Blue = Color{0, 0, 255} Yellow = Color{255, 255, 0} Cyan = Color{0, 255, 255} Magenta = Color{255, 0, 255} Gray50 = Color{128, 128, 128} Silver = Color{192, 192, 192} Orange = Color{255, 165, 0} )
Common named colors, matching the basic CSS keyword palette.
var ( // AsComponent converts a value passed as a child into a Component. It // defaults to DefaultAsComponent — see its docs for the recognized types and // the github.com/domonda/go-pretty fallback used to draw an unexpected value // as text. Document, page, state and component constructors call AsComponent // indirectly (via AsComponents), so assigning a different func changes child // conversion everywhere. // // For example, to turn the silent "unexpected value becomes text" fallback // into a hard failure during development (widen the accepted set to taste): // // base := pdf.AsComponent // pdf.AsComponent = func(c any) pdf.Component { // switch c.(type) { // case nil, pdf.Component, string: // return base(c) // default: // panic(fmt.Sprintf("pdf: unexpected child of type %T", c)) // } // } AsComponent = DefaultAsComponent )
The variables in this file are the package-level configuration of the pdf package, mirroring the configuration vars of the mx package. Each has a working default and is consulted while a document tree is built or rendered, so assigning a different value changes that behavior for the whole program. They are plain package variables with no locking: set them once during initialization (before any concurrent rendering), not while rendering.
Functions ¶
This section is empty.
Types ¶
type Border ¶
type Border string //#enum
Border selects which cell edges are stroked. The four edges are independent fpdf flags (L, T, R, B); every combination is enumerated below in canonical L-T-R-B order, so the values serve both as a closed enum and as the result of concatenating the single-edge constants — BorderLeft + BorderRight equals BorderLeftRight. Concatenate in L-T-R-B order to stay within the enumerated set. BorderNone strokes nothing; BorderFull is fpdf's "1" shorthand that strokes all four edges as a single rectangle — a more compact content stream than the otherwise equivalent BorderLeftTopRightBottom, which strokes four separate lines.
const ( // BorderNone strokes no cell edge. BorderNone Border = "" // no border // BorderFull strokes all four edges as a single rectangle (fpdf's "1" shorthand). BorderFull Border = "1" // all four edges as a single rectangle stroke // BorderLeft strokes the left edge. BorderLeft Border = "L" // left edge // BorderTop strokes the top edge. BorderTop Border = "T" // top edge // BorderRight strokes the right edge. BorderRight Border = "R" // right edge // BorderBottom strokes the bottom edge. BorderBottom Border = "B" // bottom edge // BorderLeftTop strokes the left and top edges. BorderLeftTop Border = "LT" // left + top // BorderLeftRight strokes the left and right edges. BorderLeftRight Border = "LR" // left + right // BorderLeftBottom strokes the left and bottom edges. BorderLeftBottom Border = "LB" // left + bottom // BorderTopRight strokes the top and right edges. BorderTopRight Border = "TR" // top + right // BorderTopBottom strokes the top and bottom edges. BorderTopBottom Border = "TB" // top + bottom // BorderRightBottom strokes the right and bottom edges. BorderRightBottom Border = "RB" // right + bottom // BorderLeftTopRight strokes the left, top and right edges. BorderLeftTopRight Border = "LTR" // left + top + right // BorderLeftTopBottom strokes the left, top and bottom edges. BorderLeftTopBottom Border = "LTB" // left + top + bottom // BorderLeftRightBottom strokes the left, right and bottom edges. BorderLeftRightBottom Border = "LRB" // left + right + bottom // BorderTopRightBottom strokes the top, right and bottom edges. BorderTopRightBottom Border = "TRB" // top + right + bottom // BorderLeftTopRightBottom strokes all four edges as separate lines. BorderLeftTopRightBottom Border = "LTRB" // all four edges as separate lines )
func (Border) EnumStrings ¶
EnumStrings returns all valid values for Border as strings
type Color ¶
type Color struct {
R, G, B int
}
Color is an 8-bit-per-channel RGB color, the form fpdf uses for the draw, fill and text colors. fpdf has no notion of alpha in colors; use the renderer's SetAlpha for transparency.
func Hex ¶
Hex parses a CSS-style hex color: "#rgb", "#rrggbb" or the same without the leading '#'. An invalid string returns black and a non-nil error, so callers that want strictness can check it; the State helpers ignore the error and fall back to black.
type Component ¶
Component is the fundamental interface of the pdf package, mirroring the Component interface of the html package: everything that can draw onto a page implements Render. The Renderer replaces the markup writer used for HTML, but the shape — Render(context.Context, target) error — is identical, so the same composition patterns (Components, If, ForEach, …) apply.
func Cell ¶
Cell prints text in a single-line box of the given width and height and moves the cursor to its right. A width of 0 extends the cell to the right margin. For borders, alignment, fill or a different cursor move use CellFormat.
func CellFormat ¶
func CellFormat(w, h float64, text string, border Border, ln LnPos, hAlign HAlign, vAlign VAlign, fill bool) Component
CellFormat prints text in a single-line box with full control over the border, post-cell cursor move, horizontal and vertical alignment and background fill, mirroring fpdf.CellFormat with typed parameters. A width of 0 extends to the right margin. fill paints the box with the current fill color before the text.
func DefaultAsComponent ¶
DefaultAsComponent converts an arbitrary value into a Component, the PDF counterpart of mx.DefaultAsComponent. Children are accepted as ...any and converted at render-build time:
- nil -> nil (renders nothing)
- Component -> returned unchanged
- string -> Text (flowing text)
- the func signatures in the switch -> wrapped as ComponentFunc
- error -> Text of error.Error()
- fmt.Stringer -> Text of String()
Any other value falls back to Text(pretty.Sprint(c)) using github.com/domonda/go-pretty, mirroring the mx package: primitives get their plain textual form and any other value a compact, single-line representation where structs and pointers are tagged with their type name (for example "Item{Name:`x`;Count:3}" rather than fmt's anonymous "{x 3}") while slices and maps keep their literal form, with pointers dereferenced and the length bounded, which makes an unexpected value easy to spot. Unlike the markup packages there is no escaping step — a PDF is not markup, so a stringified value cannot inject anything; it is simply drawn as text. The flip side is the same as in mx: a value passed by mistake is silently drawn as text rather than causing a compile error, so convert non-obvious children to a Component explicitly.
DefaultAsComponent is the default implementation of the package-level AsComponent variable, which may be reassigned to customize this.
func Ellipse ¶
Ellipse paints an ellipse centered at (x, y) with horizontal radius rx and vertical radius ry, rotated degRotate degrees counter-clockwise.
func Font ¶
Font selects the font family, style and size for subsequent text. It is the stateful PDF analog of setting CSS font properties: the selection persists until changed. Wrap it in Save to scope it to a group of children.
func Image ¶
Image draws the image file scaled into the box at (x, y) of width w and height h. A zero w or h preserves the image's aspect ratio from the other dimension; both zero uses the image's natural size at 72 dpi. The format is inferred from the file extension. To draw an image held in memory, use ImageReader or ImageBytes.
func ImageBytes ¶
ImageBytes is ImageReader for an in-memory byte slice.
func ImageReader ¶
ImageReader draws an image read from src into the box at (x, y) of width w and height h, without touching the filesystem. Sizing follows Image.
Because the source has no filename, name is used as fpdf's cache key: draw the same image again by passing the same name (the bytes are decoded only once), and give distinct images distinct names. imageType gives the encoding.
func Line ¶
Line strokes a straight line from (x1, y1) to (x2, y2) using the current draw color and line width.
func LineCap ¶
func LineCap(style LineCapStyle) Component
LineCap sets the shape drawn at the ends of open stroked paths.
func LineJoin ¶
func LineJoin(style LineJoinStyle) Component
LineJoin sets the shape drawn where stroked path segments meet.
func Ln ¶
Ln breaks to a new line, advancing the cursor down by h document units and back to the left margin. A value <= 0 uses the height of the last printed cell (fpdf's Ln(-1) behavior).
func MultiCell ¶
MultiCell prints word-wrapped text in a box of the given width, breaking into as many lines of height h as needed and advancing the cursor below the block, mirroring fpdf.MultiCell. A width of 0 wraps at the right margin. Only the horizontal alignment applies; vertical alignment is meaningless for flowing, multi-line text.
func NewLine ¶
func NewLine() Component
NewLine breaks to a new line using the renderer's default line height.
func Page ¶
Page starts a new page and renders children onto it. It is the structural unit of a document, loosely analogous to an html section: content is grouped per page and a new Page begins a fresh one. The very first Page (or the first drawing primitive) opens page one, so wrapping a one-page document in Page is optional.
func PageFormat ¶
func PageFormat(orientation Orientation, size PageSize, children ...any) Component
PageFormat is Page with a per-page orientation and size override, for documents that mix, say, portrait and landscape pages.
func Paragraph ¶
Paragraph is the common-case shortcut for a block of wrapped, left-aligned body text: full content width, automatic line height, no border, no fill. It is the PDF analog of an html.P.
func Rect ¶
Rect paints a rectangle at (x, y) of width w and height h with the given paint operation (Stroke, FillShape or FillStroke).
func RoundedRect ¶
RoundedRect paints a rectangle with all four corners rounded to radius. Use the raw fpdf RoundedRectExt for per-corner radii.
func Save ¶
Save renders children with the current graphics state restored afterwards, the PDF analog of wrapping content in an element so style changes inside do not leak out. It captures and restores the font (family, style and size), the text, fill and draw colors, the line width, the line cap and join styles, and the cursor position — using fpdf's getters, so it works regardless of whether the state was set through this package or the raw embedded renderer.
The dash pattern and the alpha/blend mode are not restored: fpdf exposes no getter for the dash pattern, and its zero alpha value is indistinguishable from a deliberate fully-transparent setting. Reset those explicitly if you change them inside a Save.
The cursor restore assumes children stay on the same page: if they trigger an automatic page break or add a page, the restored x, y lands on the new page, where later content can overprint. Save is meant for scoping style, not page flow. State setters and drawing primitives may be freely mixed as children, e.g. Save(TextColor(Red), Text("warning")).
func TextAt ¶
TextAt prints text once at the absolute coordinate (x, y) without wrapping or advancing the cursor, mirroring fpdf.Text. Useful for labels and annotations placed by coordinate rather than by flow.
type ComponentFunc ¶
ComponentFunc adapts a function to the Component interface.
type Components ¶
type Components []Component
Components is an ordered list of components rendered one after another, the PDF counterpart of mx.Components. A nil element renders nothing.
func AsComponents ¶
func AsComponents(cs ...any) Components
AsComponents converts a list of arbitrary values into Components using AsComponent, dropping nil results.
func ForEach ¶
func ForEach[V any, C Component](values []V, componentForValue func(V) C) Components
ForEach builds a component for every value in a slice, mirroring mx.ForEach.
func ForEachIter ¶
func ForEachIter[V any, C Component](values iter.Seq[V], componentForValue func(V) C) Components
ForEachIter is ForEach over an iterator sequence.
type Document ¶
type Document struct {
// Metadata written to the PDF info dictionary.
Title string
Author string
Subject string
Keywords string
Creator string
// Page setup. Zero values default to A4 portrait in millimeters.
Orientation Orientation
Unit Unit
PageSize PageSize
// Margins overrides the page margins when non-nil; nil keeps fpdf defaults.
Margins *Margins
// Default font applied before the body. Zero values use Helvetica 12pt.
FontFamily string
FontStyle FontStyle
FontSize float64
// Header and Footer, if set, render at the top and bottom of every page.
// They run inside fpdf's page lifecycle with the context passed to Render.
Header Component
// Body holds the page content.
Body Component
}
Document is the top-level PDF builder, the analog of html.Document. It holds document metadata, page setup, a default font and the body components, and can render into an existing Renderer or produce a finished PDF directly.
All zero-valued setup fields fall back to sensible defaults: A4 portrait in millimeters with a Helvetica 12pt font. The body is rendered after setup; it typically contains Page components, but any drawing primitive auto-starts the first page, so a single flow of text needs no explicit Page.
func NewDocument ¶
NewDocument creates a Document with the given title and body components.
func (*Document) NewRenderer ¶
NewRenderer creates a Renderer configured from the document's page setup, applying the A4/portrait/millimeter defaults for any unset field.
func (*Document) OutputFile ¶
OutputFile renders the document and writes the PDF to the named file.
func (*Document) Render ¶
Render applies the document's metadata, margins, default font and header/footer to r, then renders the body. The Document is itself a Component, so it can be embedded in a larger render.
func (*Document) ServeHTTP ¶
func (d *Document) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP renders the document and serves it as application/pdf, using the request context for cancellation. On error it responds with a short, generic 500 message and does not leak the underlying error.
type DrawOp ¶
type DrawOp string //#enum
DrawOp is the paint operation for vector shapes: stroke the outline, fill the interior, or both — the three canonical combinations of fill and stroke. fpdf also accepts the even-odd variants ("F*", "FD*") and raw PDF path operators, reachable through the embedded renderer for the rare cases that need them.
const ( // Stroke strokes the shape outline with the draw color. Stroke DrawOp = "D" // stroke the outline with the draw color // FillShape fills the shape interior with the fill color. FillShape DrawOp = "F" // fill the interior with the fill color // FillStroke fills the interior and then strokes the outline. FillStroke DrawOp = "FD" // fill, then stroke )
func (DrawOp) EnumStrings ¶
EnumStrings returns all valid values for DrawOp as strings
type FontStyle ¶
type FontStyle string //#enum
FontStyle selects bold/italic/underline/strike-out. The four are independent fpdf flags (B, I, U, S); every combination is enumerated below in canonical B-I-U-S order, so the values serve both as a closed enum and as the result of concatenating the single-flag constants — StyleBold + StyleItalic equals StyleBoldItalic. Concatenate in B-I-U-S order to stay within the enumerated set. Bold and italic do not apply to the Symbol and ZapfDingbats families.
const ( // StyleRegular is the unstyled (regular) font style. StyleRegular FontStyle = "" // regular // StyleBold is the bold (B) font style. StyleBold FontStyle = "B" // bold // StyleItalic is the italic (I) font style. StyleItalic FontStyle = "I" // italic // StyleUnderline is the underline (U) font style. StyleUnderline FontStyle = "U" // underline // StyleStrikeOut is the strike-out (S) font style. StyleStrikeOut FontStyle = "S" // strike-out // StyleBoldItalic combines the bold and italic styles (BI). StyleBoldItalic FontStyle = "BI" // bold + italic // StyleBoldUnderline combines the bold and underline styles (BU). StyleBoldUnderline FontStyle = "BU" // bold + underline // StyleBoldStrikeOut combines the bold and strike-out styles (BS). StyleBoldStrikeOut FontStyle = "BS" // bold + strike-out // StyleItalicUnderline combines the italic and underline styles (IU). StyleItalicUnderline FontStyle = "IU" // italic + underline // StyleItalicStrikeOut combines the italic and strike-out styles (IS). StyleItalicStrikeOut FontStyle = "IS" // italic + strike-out // StyleUnderlineStrikeOut combines the underline and strike-out styles (US). StyleUnderlineStrikeOut FontStyle = "US" // underline + strike-out // StyleBoldItalicUnderline combines the bold, italic and underline styles (BIU). StyleBoldItalicUnderline FontStyle = "BIU" // bold + italic + underline // StyleBoldItalicStrikeOut combines the bold, italic and strike-out styles (BIS). StyleBoldItalicStrikeOut FontStyle = "BIS" // bold + italic + strike-out // StyleBoldUnderlineStrikeOut combines the bold, underline and strike-out styles (BUS). StyleBoldUnderlineStrikeOut FontStyle = "BUS" // bold + underline + strike-out // StyleItalicUnderlineStrikeOut combines the italic, underline and strike-out styles (IUS). StyleItalicUnderlineStrikeOut FontStyle = "IUS" // italic + underline + strike-out // StyleBoldItalicUnderlineStrikeOut combines all four styles (BIUS). StyleBoldItalicUnderlineStrikeOut FontStyle = "BIUS" // all four )
func (FontStyle) EnumStrings ¶
EnumStrings returns all valid values for FontStyle as strings
type HAlign ¶
type HAlign string //#enum
HAlign is the horizontal text alignment inside a cell. Horizontal and vertical alignment are two independent single-choice axes rather than a flag set, so they are modelled as two enums (HAlign and VAlign) instead of one large combined enumeration. fpdf treats left as the default, so AlignLeft and the empty value render identically.
const ( // AlignLeft aligns text to the left edge of the cell (fpdf default). AlignLeft HAlign = "L" // left (default) // AlignCenter centers text horizontally within the cell. AlignCenter HAlign = "C" // center // AlignRight aligns text to the right edge of the cell. AlignRight HAlign = "R" // right // AlignJustify justifies text to both edges of the cell (MultiCell only). AlignJustify HAlign = "J" // justified (MultiCell only) )
func (HAlign) EnumStrings ¶
EnumStrings returns all valid values for HAlign as strings
type IfElse ¶
type IfElse struct {
// contains filtered or unexported fields
}
IfElse is a conditional component returned by If and Iff.
func If ¶
If renders comps only when cond is true, mirroring mx.If. Use the returned IfElse's Else / ElseIf methods for the false branch.
func Iff ¶
Iff is like If but takes the condition as a function, so the condition can be computed lazily at call time.
func (IfElse) Else ¶
func (i IfElse) Else(comps ...Component) Components
Else returns the components to render when the condition was false.
type ImageType ¶
type ImageType string //#enum
ImageType identifies the encoding of an in-memory image passed to ImageReader / ImageBytes, where there is no filename to infer it from. fpdf supports these three raster formats only.
func (ImageType) EnumStrings ¶
EnumStrings returns all valid values for ImageType as strings
type LineCapStyle ¶
type LineCapStyle string //#enum
LineCapStyle is the shape drawn at the ends of open paths.
const ( // CapButt ends open paths flush at the endpoint with no extension. CapButt LineCapStyle = "butt" // CapRound ends open paths with a semicircular cap centered on the endpoint. CapRound LineCapStyle = "round" // CapSquare ends open paths with a square cap extending half the line width past the endpoint. CapSquare LineCapStyle = "square" )
func (LineCapStyle) EnumStrings ¶
func (LineCapStyle) EnumStrings() []string
EnumStrings returns all valid values for LineCapStyle as strings
func (LineCapStyle) Enums ¶
func (LineCapStyle) Enums() []LineCapStyle
Enums returns all valid values for LineCapStyle
func (LineCapStyle) String ¶
func (l LineCapStyle) String() string
String implements the fmt.Stringer interface for LineCapStyle
func (LineCapStyle) Valid ¶
func (l LineCapStyle) Valid() bool
Valid indicates if l is any of the valid values for LineCapStyle
func (LineCapStyle) Validate ¶
func (l LineCapStyle) Validate() error
Validate returns an error if l is none of the valid values for LineCapStyle
type LineJoinStyle ¶
type LineJoinStyle string //#enum
LineJoinStyle is the shape drawn where path segments meet.
const ( // JoinMiter joins segments with a sharp, extended corner. JoinMiter LineJoinStyle = "miter" // JoinRound joins segments with a rounded corner. JoinRound LineJoinStyle = "round" // JoinBevel joins segments with a flattened corner. JoinBevel LineJoinStyle = "bevel" )
func (LineJoinStyle) EnumStrings ¶
func (LineJoinStyle) EnumStrings() []string
EnumStrings returns all valid values for LineJoinStyle as strings
func (LineJoinStyle) Enums ¶
func (LineJoinStyle) Enums() []LineJoinStyle
Enums returns all valid values for LineJoinStyle
func (LineJoinStyle) String ¶
func (l LineJoinStyle) String() string
String implements the fmt.Stringer interface for LineJoinStyle
func (LineJoinStyle) Valid ¶
func (l LineJoinStyle) Valid() bool
Valid indicates if l is any of the valid values for LineJoinStyle
func (LineJoinStyle) Validate ¶
func (l LineJoinStyle) Validate() error
Validate returns an error if l is none of the valid values for LineJoinStyle
type LnPos ¶
type LnPos int //#enum
LnPos selects where the cursor moves after a Cell, matching fpdf's ln argument.
const ( // LnRight moves the cursor to the right of the cell (fpdf default). LnRight LnPos = 0 // to the right of the cell (default) // LnNewline moves the cursor to the start of the next line. LnNewline LnPos = 1 // to the start of the next line // LnBelow moves the cursor directly below the cell. LnBelow LnPos = 2 // directly below the cell )
func (LnPos) EnumStrings ¶
EnumStrings returns all valid values for LnPos as strings
type Margins ¶
type Margins struct {
Left, Top, Right float64
}
Margins are the left, top and right page margins in document units. fpdf derives the bottom margin (the auto page-break trigger) from the top margin.
type Orientation ¶
type Orientation string //#enum
Orientation is a page orientation passed to the Renderer constructors and to the Page component. fpdf matches on the first letter, case-insensitively, so Portrait and Landscape are the only two distinct orientations.
const ( // Portrait is the upright "portrait" page orientation (taller than wide). Portrait Orientation = fpdf.OrientationPortrait // "portrait" // Landscape is the sideways "landscape" page orientation (wider than tall). Landscape Orientation = fpdf.OrientationLandscape // "landscape" )
func (Orientation) EnumStrings ¶
func (Orientation) EnumStrings() []string
EnumStrings returns all valid values for Orientation as strings
func (Orientation) Enums ¶
func (Orientation) Enums() []Orientation
Enums returns all valid values for Orientation
func (Orientation) String ¶
func (o Orientation) String() string
String implements the fmt.Stringer interface for Orientation
func (Orientation) Valid ¶
func (o Orientation) Valid() bool
Valid indicates if o is any of the valid values for Orientation
func (Orientation) Validate ¶
func (o Orientation) Validate() error
Validate returns an error if o is none of the valid values for Orientation
type PageSize ¶
type PageSize string //#enum
PageSize is a standard page size accepted by fpdf's string-based page setup. The constants below are all of them; fully custom dimensions go through the raw fpdf API (fpdf.NewCustom / AddPageFormat with a SizeType). fpdf lower-cases the value, so these upper-case forms and their lower-case spellings are equivalent.
const ( // A1 is the ISO 216 A1 page size (594 × 841 mm). A1 PageSize = "A1" // 594 × 841 mm // A2 is the ISO 216 A2 page size (420 × 594 mm). A2 PageSize = "A2" // 420 × 594 mm // A3 is the ISO 216 A3 page size (297 × 420 mm). A3 PageSize = fpdf.PageSizeA3 // 297 × 420 mm // A4 is the ISO 216 A4 page size (210 × 297 mm). A4 PageSize = fpdf.PageSizeA4 // 210 × 297 mm // A5 is the ISO 216 A5 page size (148 × 210 mm). A5 PageSize = fpdf.PageSizeA5 // 148 × 210 mm // A6 is the ISO 216 A6 page size (105 × 148 mm). A6 PageSize = "A6" // 105 × 148 mm // A7 is the ISO 216 A7 page size (74 × 105 mm). A7 PageSize = "A7" // 74 × 105 mm // A8 is the ISO 216 A8 page size (52 × 74 mm). A8 PageSize = "A8" // 52 × 74 mm // Letter is the US Letter page size (8.5 × 11 in). Letter PageSize = fpdf.PageSizeLetter // 8.5 × 11 in // Legal is the US Legal page size (8.5 × 14 in). Legal PageSize = fpdf.PageSizeLegal // 8.5 × 14 in // Tabloid is the US Tabloid/Ledger page size (11 × 17 in). Tabloid PageSize = "Tabloid" // 11 × 17 in )
func (PageSize) EnumStrings ¶
EnumStrings returns all valid values for PageSize as strings
type Point ¶
Point is a coordinate in document units, re-exported from fpdf so it can be passed straight to the embedded renderer's polygon and curve methods.
type Renderer ¶
Renderer is the PDF output target that components draw into.
It is the PDF counterpart of the markup writer used by the html package: where an html component writes tags into a markup writer, a pdf Component issues drawing calls against a Renderer. The embedded *fpdf.Fpdf is the actual renderer, so every fpdf method (Cell, Rect, Image, Transform, …) is available directly and components can always drop down to raw fpdf for anything the typed primitives do not cover.
fpdf is imperative and stateful: the current page, cursor position, font, and colors persist between calls. The primitives in this package are thin, composable wrappers around that state machine rather than a retained tree.
func NewRenderer ¶
func NewRenderer(orientation Orientation, unit Unit, size PageSize) *Renderer
NewRenderer creates a Renderer for the given page orientation, measurement unit and page size, with a Helvetica 12pt default font already selected so text can be drawn without further setup.
func NewRendererA4Landscape ¶
func NewRendererA4Landscape() *Renderer
NewRendererA4Landscape creates a Renderer for an A4 landscape document measured in millimeters.
func NewRendererA4Portrait ¶
func NewRendererA4Portrait() *Renderer
NewRendererA4Portrait creates a Renderer for an A4 portrait document measured in millimeters.
func NewRendererLetterPortrait ¶
func NewRendererLetterPortrait() *Renderer
NewRendererLetterPortrait creates a Renderer for a US Letter portrait document measured in inches.
func (*Renderer) LineHeight ¶
LineHeight returns the default line height for flowing text in document units, resolving the "auto" zero value to 1.15× the current font size.
func (*Renderer) LoadUTF8FontBytes ¶
LoadUTF8FontBytes registers a UTF-8 TrueType font from in-memory bytes under the given family and style — the in-memory counterpart of fpdf's file-based AddUTF8Font, so non-Latin text needs no font file on disk. It also switches the renderer's translator to identity, because UTF-8 fonts take UTF-8 strings directly and the cp1252 translation used for the core fonts would corrupt them; call SetTranslator if you later go back to a core font. Select the font afterwards with the Font component or SetFont.
func (*Renderer) LoadUTF8FontReader ¶
LoadUTF8FontReader is Renderer.LoadUTF8FontBytes reading the font from src. A read error is recorded on the renderer and surfaces from the next Error().
func (*Renderer) SetLineHeight ¶
SetLineHeight sets the default line height in document units for flowing text. A value <= 0 restores automatic height derived from the font size.
func (*Renderer) SetTranslator ¶
SetTranslator replaces the UTF-8 translator, e.g. after switching to a font with a different code page. Pass the result of fpdf.UnicodeTranslatorFromDescriptor or the identity for UTF-8 fonts.
type Text ¶
type Text string
Text is flowing text printed at the current cursor with automatic line wrapping at the right margin, the PDF counterpart of mx.Text and the type a bare string child is converted to. It uses the renderer's default line height and advances the cursor; embedded "\n" force line breaks.
type Unit ¶
type Unit string //#enum
Unit is the document measurement unit. All coordinates, widths and heights passed to components are expressed in this unit; font sizes are always in points regardless of the document unit. These four are the only distinct units fpdf supports.
const ( // UnitPoint is the typographic point unit ("pt", 1/72 inch). UnitPoint Unit = fpdf.UnitPoint // "pt" // UnitMillimeter is the millimeter unit ("mm"). UnitMillimeter Unit = fpdf.UnitMillimeter // "mm" // UnitCentimeter is the centimeter unit ("cm"). UnitCentimeter Unit = fpdf.UnitCentimeter // "cm" // UnitInch is the inch unit ("inch"). UnitInch Unit = fpdf.UnitInch // "inch" )
func (Unit) EnumStrings ¶
EnumStrings returns all valid values for Unit as strings
type VAlign ¶
type VAlign string //#enum
VAlign is the vertical text alignment inside a cell. fpdf treats middle as the default, so AlignMiddle and the empty value render identically.
const ( // AlignTop aligns text to the top edge of the cell. AlignTop VAlign = "T" // top // AlignMiddle centers text vertically within the cell (fpdf default). AlignMiddle VAlign = "M" // middle (default) // AlignBottom aligns text to the bottom edge of the cell. AlignBottom VAlign = "B" // bottom // AlignBaseline aligns text to the font baseline. AlignBaseline VAlign = "A" // baseline )
func (VAlign) EnumStrings ¶
EnumStrings returns all valid values for VAlign as strings