Documentation
¶
Overview ¶
Package pdf is a write-only PDF 1.3–1.7 serializer: the low-level object model, document/xref writer, content-stream operator builder, and standard-14 font support. It is the Go replacement for react-pdf's pdfkit fork. Higher layers (layout, render) build documents through this package.
Index ¶
- func IsStandardFont(name string) bool
- func MeasureText(baseFont string, size float64, text string) (float64, error)
- func StandardFonts() []string
- type Array
- type Boolean
- type ChoiceOption
- type Content
- func (c *Content) AddFormField(f FormField)
- func (c *Content) AddLink(x0, y0, x1, y1 float64, uri string)
- func (c *Content) AddNote(x, y float64, text string)
- func (c *Content) AlphaResource(a float64) (Name, bool)
- func (c *Content) Alphas() []float64
- func (c *Content) BeginText() *Content
- func (c *Content) Bytes() []byte
- func (c *Content) CharSpacing(v float64) *Content
- func (c *Content) Clip() *Content
- func (c *Content) ClipEvenOdd() *Content
- func (c *Content) ClosePath() *Content
- func (c *Content) CurveTo(x1, y1, x2, y2, x3, y3 float64) *Content
- func (c *Content) Dash(phase float64, pattern ...float64) *Content
- func (c *Content) DrawImage(spec *ImageSpec, x, y, w, h float64) *Content
- func (c *Content) EmbeddedFontResource(ef *EmbeddedFont) (Name, bool)
- func (c *Content) EmbeddedFonts() []*EmbeddedFont
- func (c *Content) EndPath() *Content
- func (c *Content) EndText() *Content
- func (c *Content) Fill() *Content
- func (c *Content) FillEvenOdd() *Content
- func (c *Content) FillGray(v float64) *Content
- func (c *Content) FillRGB(r, g, b float64) *Content
- func (c *Content) FillStroke() *Content
- func (c *Content) FontResource(baseFont string) (Name, bool)
- func (c *Content) Fonts() []string
- func (c *Content) FormFields() []FormField
- func (c *Content) HorizScale(pct float64) *Content
- func (c *Content) ImageResource(spec *ImageSpec) (Name, bool)
- func (c *Content) Images() []*ImageSpec
- func (c *Content) Leading(v float64) *Content
- func (c *Content) LineCap(style int) *Content
- func (c *Content) LineJoin(style int) *Content
- func (c *Content) LineTo(x, y float64) *Content
- func (c *Content) LineWidth(v float64) *Content
- func (c *Content) Links() []LinkAnnotation
- func (c *Content) MoveTo(x, y float64) *Content
- func (c *Content) NextLine() *Content
- func (c *Content) Notes() []NoteAnnotation
- func (c *Content) Rect(x, y, wid, hgt float64) *Content
- func (c *Content) Restore() *Content
- func (c *Content) Save() *Content
- func (c *Content) SetAlpha(a float64) *Content
- func (c *Content) SetEmbeddedFont(ef *EmbeddedFont, size float64) *Content
- func (c *Content) SetFont(baseFont string, size float64) *Content
- func (c *Content) Shade(sh *Shading) *Content
- func (c *Content) ShadingResource(sh *Shading) (Name, bool)
- func (c *Content) Shadings() []*Shading
- func (c *Content) ShowText(s string) *Content
- func (c *Content) ShowTextRaw(enc []byte) *Content
- func (c *Content) Stroke() *Content
- func (c *Content) StrokeGray(v float64) *Content
- func (c *Content) StrokeRGB(r, g, b float64) *Content
- func (c *Content) TextMatrix(a, b, cc, d, e, f float64) *Content
- func (c *Content) TextPosition(x, y float64) *Content
- func (c *Content) TextRise(v float64) *Content
- func (c *Content) Transform(a, b, cc, d, e, f float64) *Content
- func (c *Content) WordSpacing(v float64) *Content
- type Dict
- type Document
- type EmbeddedFont
- type FontDescriptor
- type FormField
- type FormFieldKind
- type HexString
- type ImageSpec
- type Integer
- type LinkAnnotation
- type LiteralString
- type Name
- type NoteAnnotation
- type Null
- type Object
- type Options
- type Outline
- type Real
- type Reference
- type Shading
- type ShadingStop
- type Stream
- type Writer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsStandardFont ¶
IsStandardFont reports whether name is one of the 14 standard fonts.
func MeasureText ¶
MeasureText returns the advance width in points of text set in a standard font at the given size, under WinAnsiEncoding. It errors if baseFont is not one of the 14 standard fonts.
func StandardFonts ¶
func StandardFonts() []string
StandardFonts returns the 14 standard PDF font names.
Types ¶
type ChoiceOption ¶
ChoiceOption is one entry of a choice field: an export value and the display label shown to the user (often identical).
type Content ¶
type Content struct {
// contains filtered or unexported fields
}
Content builds a page content stream from PDF drawing operators. Methods are chainable. It also records which standard fonts were used so the page can build a matching /Font resource dictionary.
func (*Content) AddFormField ¶
AddFormField records an interactive form field widget for this page.
func (*Content) AddLink ¶
AddLink records a hyperlink annotation over the given page-space rectangle.
func (*Content) AddNote ¶
AddNote records a text-note annotation anchored at the given page-space point.
func (*Content) AlphaResource ¶
AlphaResource returns the resource name assigned to an alpha value.
func (*Content) CharSpacing ¶
CharSpacing sets the character spacing (Tc).
func (*Content) ClipEvenOdd ¶
ClipEvenOdd intersects the clip path using the even-odd rule (W*).
func (*Content) DrawImage ¶
DrawImage draws an image, scaled to w×h at (x, y) in the current coordinate system (bottom-left origin). Images are drawn from a unit square, so the CTM maps them to the target rectangle.
func (*Content) EmbeddedFontResource ¶
func (c *Content) EmbeddedFontResource(ef *EmbeddedFont) (Name, bool)
EmbeddedFontResource returns the resource name assigned to an embedded font.
func (*Content) EmbeddedFonts ¶
func (c *Content) EmbeddedFonts() []*EmbeddedFont
EmbeddedFonts returns the embedded fonts used, in first-use order.
func (*Content) FillEvenOdd ¶
FillEvenOdd fills the path with the even-odd rule (f*).
func (*Content) FillRGB ¶
FillRGB sets the nonstroking color in DeviceRGB (rg). Components are 0..1.
func (*Content) FillStroke ¶
FillStroke fills then strokes the path (B).
func (*Content) FontResource ¶
FontResource returns the resource name assigned to a base font (valid after SetFont has been called for it).
func (*Content) FormFields ¶
FormFields returns the form fields recorded for this page.
func (*Content) HorizScale ¶
HorizScale sets horizontal text scaling as a percentage (Tz).
func (*Content) ImageResource ¶
ImageResource returns the resource name assigned to an image.
func (*Content) Links ¶
func (c *Content) Links() []LinkAnnotation
Links returns the hyperlink annotations recorded for this page.
func (*Content) Notes ¶
func (c *Content) Notes() []NoteAnnotation
Notes returns the text-note annotations recorded for this page.
func (*Content) SetAlpha ¶
SetAlpha sets the constant alpha for fills and strokes (ca/CA) via an ExtGState resource, recording the alpha so the page can build /ExtGState. Values are clamped to [0,1]. Requires PDF 1.4+.
func (*Content) SetEmbeddedFont ¶
func (c *Content) SetEmbeddedFont(ef *EmbeddedFont, size float64) *Content
SetEmbeddedFont selects an embedded TrueType font and size (Tf), registering it so the page can wire a matching /Font resource.
func (*Content) SetFont ¶
SetFont selects a standard font and size (Tf). The base font is registered so the page can wire the matching resource name to a font object.
func (*Content) Shade ¶
Shade registers a shading and emits `sh` to paint it, subject to the current clip path. Wrap in Save → path → Clip → EndPath → Shade → Restore to confine the gradient to a shape.
func (*Content) ShadingResource ¶
ShadingResource returns the resource name assigned to a shading.
func (*Content) ShowText ¶
ShowText shows a string, encoding it to WinAnsiEncoding (Tj). Runes not representable in WinAnsi are shown as '?'.
func (*Content) ShowTextRaw ¶
ShowTextRaw shows already-encoded bytes as a literal string (Tj).
func (*Content) StrokeGray ¶
StrokeGray sets the stroking gray level (G).
func (*Content) StrokeRGB ¶
StrokeRGB sets the stroking color in DeviceRGB (RG). Components are 0..1.
func (*Content) TextMatrix ¶
TextMatrix sets the text matrix (Tm).
func (*Content) TextPosition ¶
TextPosition moves to the next line offset by (tx, ty) (Td).
func (*Content) WordSpacing ¶
WordSpacing sets the word spacing (Tw).
type Dict ¶
Dict is a PDF dictionary. Keys are serialized in sorted order so that output is deterministic.
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document assembles a PDF: metadata, a page tree, and shared font objects.
func (*Document) AddPage ¶
AddPage appends a page of the given size (in points) with the given content. The page's /Font resources are built from the fonts the content used.
func (*Document) SetOutline ¶
SetOutline installs the document outline (bookmark tree). Passing an empty slice leaves the document without an /Outlines dictionary.
type EmbeddedFont ¶
type EmbeddedFont struct {
Name string // PDF /BaseFont name (unique per face)
Program []byte // raw TrueType (sfnt) bytes
Descriptor FontDescriptor
Widths []int
// contains filtered or unexported fields
}
EmbeddedFont is a TrueType font program embedded as a simple (WinAnsi) PDF font. Widths is indexed by WinAnsi code (0..255), in 1000-unit advances. It is compared by pointer identity, so reuse the same *EmbeddedFont per face — that also lets the compressed font program be built once and shared across every document the face appears in.
type FontDescriptor ¶
type FontDescriptor struct {
Ascent, Descent, CapHeight float64
BBox [4]float64 // xMin, yMin, xMax, yMax
ItalicAngle float64
Flags int
StemV float64
}
FontDescriptor holds embedded-font metrics in 1000-unit text space.
type FormField ¶
type FormField struct {
Kind FormFieldKind
Name string // fully-qualified field name (/T)
Value string // text value (/V) for text fields
FontSize float64 // text fields: 0 means auto-size (/DA "0 Tf")
MultiLine bool // text field flag: multi-line
Password bool // text field flag: password (value masked)
Checked bool // checkbox: initial state
OnState string // checkbox: export value of the "on" state (default "Yes")
Options []ChoiceOption // choice field entries
Combo bool // choice field: dropdown (combo) vs. list box
X0, Y0, X1, Y1 float64 // widget rectangle in page (default user) space
}
FormField is an interactive AcroForm field rendered as a widget annotation on a page. Text fields (/Tx) and checkboxes (/Btn) are supported.
type FormFieldKind ¶
type FormFieldKind int
FormFieldKind selects the widget type of an AcroForm field.
const ( FieldText FormFieldKind = iota // single- or multi-line text (/Tx) FieldCheckbox // toggle button (/Btn) FieldChoice // combo box / list box (/Ch) )
type ImageSpec ¶
type ImageSpec struct {
Width, Height int
ColorSpace string // "DeviceRGB", "DeviceGray", "DeviceCMYK"
BitsPerComponent int
Filter string // "DCTDecode" or "FlateDecode"
Data []byte // filtered stream bytes
SMask []byte // optional 8-bit gray alpha, FlateDecode-compressed
}
ImageSpec describes an image ready to embed as an XObject: its dimensions, color space, and already-filtered stream data (plus an optional alpha SMask).
type LinkAnnotation ¶
LinkAnnotation is a clickable URI link over a rectangle, in page (default user) space — bottom-left origin, x0<x1, y0<y1. It is a page annotation, not a content-stream op, so it is unaffected by the content CTM.
type LiteralString ¶
type LiteralString string
LiteralString is a PDF literal string; the bytes are written verbatim inside parentheses with the required escaping.
type NoteAnnotation ¶
NoteAnnotation is a text (sticky-note) annotation anchored at a page-space point (its icon's top-left), carrying popup text. Coordinates are in default user space (bottom-left origin), so it is unaffected by the content CTM.
type Object ¶
type Object interface {
// contains filtered or unexported methods
}
Object is any PDF object that can serialize itself as PDF syntax.
func TextString ¶
TextString returns a PDF text string: a literal ASCII string when possible, otherwise a UTF-16BE hex string with a byte-order mark (PDF text string form).
type Options ¶
type Options struct {
Title string
Author string
Subject string
Keywords string
Creator string
Producer string
PDFVersion string // "1.3".."1.7"; defaults to "1.4"
Language string // catalog /Lang
PageMode string // catalog /PageMode, e.g. "UseOutlines"
PageLayout string // catalog /PageLayout, e.g. "TwoColumnLeft"
CreationDate time.Time // omitted from /Info when zero
ModDate time.Time // omitted from /Info when zero
// UserPassword and OwnerPassword enable standard-security-handler
// encryption (RC4 128-bit, V=2/R=3) when either is non-empty. An empty
// user password with a non-empty owner password yields a file that opens
// without a password but whose permissions require the owner password to
// change.
UserPassword string
OwnerPassword string
// Permissions is the encryption dictionary's /P flags value. Zero means
// "everything allowed" and is replaced by -4 (0xFFFFFFFC): every flag
// bit set except bits 1–2, which are reserved and shall be 0 per
// PDF 32000-1 Table 22. Ignored unless encryption is enabled.
Permissions int32
// EncryptAES selects AES-128 (AESV2, /V 4 /R 4) instead of the default
// RC4-128 handler when encryption is enabled. Ignored without a password.
EncryptAES bool
// EncryptAES256 selects AES-256 (AESV3, /V 5 /R 6) — the strongest handler.
// Takes precedence over EncryptAES. Ignored without a password.
EncryptAES256 bool
}
Options configure document-level metadata and output determinism.
type Outline ¶
Outline is one document-outline (bookmark) entry: a title, the destination page (0-based index into the pages added to the document), the destination Y in PDF space (points from the page bottom), and any nested child entries.
type Shading ¶
type Shading struct {
Radial bool
Coords []float64
Stops []ShadingStop
}
Shading describes an axial (linear) or radial gradient in DeviceRGB, expressed in the current user space — it is painted with the `sh` operator, which honors the current CTM and clip (so it needs no pattern matrix). Coords are [x0 y0 x1 y1] for axial and [cx0 cy0 r0 cx1 cy1 r1] for radial. Compared by pointer identity; reuse the same *Shading per gradient instance.
type ShadingStop ¶
type ShadingStop struct {
Offset, R, G, B float64
}
ShadingStop is a gradient color stop: Offset in [0,1], RGB components in [0,1].
type Stream ¶
Stream is a PDF stream object: a dictionary plus raw bytes. Streams must be indirect objects. The /Length entry is filled in automatically at encode time.
func FlateStream ¶
FlateStream returns a stream object whose data is zlib/FlateDecode-compressed. extra dictionary entries (if any) are merged in alongside /Filter.
type Writer ¶
type Writer struct {
Root Reference // catalog; required
Info Reference // document info dict; optional
Encrypt Reference // encryption dictionary; optional
ID [2][]byte // file identifier halves; optional
Version string // e.g. "1.4"; defaults to "1.4"
// contains filtered or unexported fields
}
Writer manages the indirect object table and serializes a complete PDF file (header, body, cross-reference table, trailer). Object numbers are assigned in allocation order, which callers drive deterministically.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package afm parses Adobe Font Metrics for the 14 standard PDF fonts and exposes glyph widths and font metrics used for text measurement.
|
Package afm parses Adobe Font Metrics for the 14 standard PDF fonts and exposes glyph widths and font metrics used for text measurement. |