Documentation
¶
Overview ¶
Package pdfexport implements a paint.Painter that records draw operations and serialises them as a PDF 1.4 document. Sister of silk/svgexport — together they restore the export-surface capability lost when the opengl branch dropped Cairo (cairo_pdf_surface).
Usage:
p := pdfexport.New(595, 842) // A4 in points (72 dpi)
p.SetBrush1(paint.Color{R: 200, G: 200, B: 200, A: 255})
p.Rectangle(0, 0, 595, 842)
p.Fill()
p.SetBrush1(paint.Color{R: 0, G: 0, B: 0, A: 255})
p.DrawText1(72, 80, "Hello PDF")
p.WriteTo(file)
PDF coordinate convention is bottom-left origin with Y up; paint's is top-left, Y down. We flip Y per coordinate at emit time so the resulting PDF reads pixels with the same (x, y) the caller passed.
Single-page output. The doc structure is the minimum PDF 1.4 viewers (Acrobat, macOS Preview, browser viewers) accept: %PDF-1.4 header, Catalog/Pages/Page/Contents/Font objects, xref table, trailer.
Helvetica is the default font — one of the 14 standard PDF fonts every reader is required to ship, so no font embedding is needed. SetFont() with a paint.Font is currently advisory: the size is honoured but the family stays Helvetica until Type1/TrueType embedding lands in a follow-up.
Pixmap / Icon / Glyphs operations are recorded as no-ops (the same limitation as svgexport); raster image embedding via the /XObject dictionary is a follow-up.
Index ¶
- type PDFPainter
- func (p *PDFPainter) Arc(xc, yc, radius, angle1, angle2 float64)
- func (p *PDFPainter) ArcNegative(xc, yc, radius, angle1, angle2 float64)
- func (p *PDFPainter) Bytes() []byte
- func (p *PDFPainter) Clip()
- func (p *PDFPainter) ClipBounds() (float64, float64, float64, float64)
- func (p *PDFPainter) ClipBounds1() geom.Rect
- func (p *PDFPainter) ClipPreserve()
- func (p *PDFPainter) CompressionEnabled() bool
- func (p *PDFPainter) CurrentPoint() (float64, float64)
- func (p *PDFPainter) CurrentState() int
- func (p *PDFPainter) CurveTo(x1, y1, x2, y2, x3, y3 float64)
- func (p *PDFPainter) DrawGlyph(glyph *paint.Glyph)
- func (p *PDFPainter) DrawGlyphs(glyphs []paint.Glyph)
- func (p *PDFPainter) DrawIcon(ico paint.Icon, fSize float64, grayed bool)
- func (p *PDFPainter) DrawIcon1(ico paint.Icon, x, y, fSize float64, grayed bool)
- func (p *PDFPainter) DrawPixmap(pixmap paint.Pixmap)
- func (p *PDFPainter) DrawPixmap1(x, y float64, pixmap paint.Pixmap)
- func (p *PDFPainter) DrawPixmap2(x, y float64, pixmap paint.Pixmap, x0, y0 float64)
- func (p *PDFPainter) DrawPixmap5(x, y, w, h float64, pixmap paint.Pixmap)
- func (p *PDFPainter) DrawText(text string)
- func (p *PDFPainter) DrawText1(x, y float64, text string)
- func (p *PDFPainter) Fill()
- func (p *PDFPainter) FillPreserve()
- func (p *PDFPainter) Font() paint.Font
- func (p *PDFPainter) GetMatrix(m *geom.Mat3x2)
- func (p *PDFPainter) Line(x1, y1, x2, y2 float64)
- func (p *PDFPainter) LineTo(x, y float64)
- func (p *PDFPainter) MoveTo(x, y float64)
- func (p *PDFPainter) NewPage()
- func (p *PDFPainter) NewPage1(width, height float64)
- func (p *PDFPainter) PageCount() int
- func (p *PDFPainter) Paint()
- func (p *PDFPainter) PaintWithAlpha(alpha uint8)
- func (p *PDFPainter) Rectangle(x, y, w, h float64)
- func (p *PDFPainter) Rectangle1(rect geom.Rect)
- func (p *PDFPainter) ResetClip()
- func (p *PDFPainter) ResetMatrix()
- func (p *PDFPainter) Restore() int
- func (p *PDFPainter) RestoreTo(n int) bool
- func (p *PDFPainter) Rotate(radians float64)
- func (p *PDFPainter) Save() int
- func (p *PDFPainter) Scale(sx, sy float64)
- func (p *PDFPainter) ScaledFont() paint.ScaledFont
- func (p *PDFPainter) SetBrush(br paint.Brush)
- func (p *PDFPainter) SetBrush1(cr paint.Color)
- func (p *PDFPainter) SetCompression(on bool)
- func (p *PDFPainter) SetFont(f paint.Font)
- func (p *PDFPainter) SetMatrix(m *geom.Mat3x2)
- func (p *PDFPainter) SetOperator(op paint.Operator)
- func (p *PDFPainter) SetPen(pen paint.Pen)
- func (p *PDFPainter) SetPen1(cr paint.Color, w float64)
- func (p *PDFPainter) String() string
- func (p *PDFPainter) Stroke()
- func (p *PDFPainter) StrokePreserve()
- func (p *PDFPainter) Target() paint.Surface
- func (p *PDFPainter) Transform(m *geom.Mat3x2)
- func (p *PDFPainter) Translate(tx, ty float64)
- func (p *PDFPainter) WriteTo(w io.Writer) (int64, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PDFPainter ¶
type PDFPainter struct {
// contains filtered or unexported fields
}
PDFPainter implements paint.Painter and emits PDF 1.4.
Concurrency: one painter per export. Not safe for concurrent use.
func New ¶
func New(width, height float64) *PDFPainter
New constructs a fresh painter with the given page size in points (1/72 inch). A4 ≈ (595, 842); US Letter ≈ (612, 792); designer canvases typically pass their own sizes.
func (*PDFPainter) Arc ¶
func (p *PDFPainter) Arc(xc, yc, radius, angle1, angle2 float64)
Arc decomposes into ≤90° cubic Bezier slices. PDF has no native elliptical-arc operator; the standard practice is the same as SVG's "A" expansion — split into quarter-or-less spans and approximate each with one cubic.
func (*PDFPainter) ArcNegative ¶
func (p *PDFPainter) ArcNegative(xc, yc, radius, angle1, angle2 float64)
func (*PDFPainter) Bytes ¶
func (p *PDFPainter) Bytes() []byte
Bytes returns the complete PDF document as a byte slice.
func (*PDFPainter) Clip ¶
func (p *PDFPainter) Clip()
func (*PDFPainter) ClipBounds ¶
func (p *PDFPainter) ClipBounds() (float64, float64, float64, float64)
func (*PDFPainter) ClipBounds1 ¶
func (p *PDFPainter) ClipBounds1() geom.Rect
func (*PDFPainter) ClipPreserve ¶
func (p *PDFPainter) ClipPreserve()
func (*PDFPainter) CompressionEnabled ¶
func (p *PDFPainter) CompressionEnabled() bool
CompressionEnabled returns the current setting. Useful for production code that wants to log whether a saved PDF is in raw or compressed mode.
func (*PDFPainter) CurrentPoint ¶
func (p *PDFPainter) CurrentPoint() (float64, float64)
func (*PDFPainter) CurrentState ¶
func (p *PDFPainter) CurrentState() int
func (*PDFPainter) CurveTo ¶
func (p *PDFPainter) CurveTo(x1, y1, x2, y2, x3, y3 float64)
func (*PDFPainter) DrawGlyph ¶
func (p *PDFPainter) DrawGlyph(glyph *paint.Glyph)
func (*PDFPainter) DrawGlyphs ¶
func (p *PDFPainter) DrawGlyphs(glyphs []paint.Glyph)
func (*PDFPainter) DrawIcon ¶
func (p *PDFPainter) DrawIcon(ico paint.Icon, fSize float64, grayed bool)
func (*PDFPainter) DrawIcon1 ¶
func (p *PDFPainter) DrawIcon1(ico paint.Icon, x, y, fSize float64, grayed bool)
func (*PDFPainter) DrawPixmap ¶
func (p *PDFPainter) DrawPixmap(pixmap paint.Pixmap)
func (*PDFPainter) DrawPixmap1 ¶
func (p *PDFPainter) DrawPixmap1(x, y float64, pixmap paint.Pixmap)
func (*PDFPainter) DrawPixmap2 ¶
func (p *PDFPainter) DrawPixmap2(x, y float64, pixmap paint.Pixmap, x0, y0 float64)
func (*PDFPainter) DrawPixmap5 ¶
func (p *PDFPainter) DrawPixmap5(x, y, w, h float64, pixmap paint.Pixmap)
func (*PDFPainter) DrawText ¶
func (p *PDFPainter) DrawText(text string)
func (*PDFPainter) DrawText1 ¶
func (p *PDFPainter) DrawText1(x, y float64, text string)
DrawText1 emits a PDF text object (BT…ET) at (x, y). The text-matrix trick: PDF text is normally drawn glyph-up, but our caller's y is top-down; we use Tm = [1 0 0 -1 x H-y] which places origin at PDF (x, H-y) and flips glyph orientation back to upright for top-down users.
func (*PDFPainter) Fill ¶
func (p *PDFPainter) Fill()
func (*PDFPainter) FillPreserve ¶
func (p *PDFPainter) FillPreserve()
func (*PDFPainter) Font ¶
func (p *PDFPainter) Font() paint.Font
func (*PDFPainter) GetMatrix ¶
func (p *PDFPainter) GetMatrix(m *geom.Mat3x2)
func (*PDFPainter) Line ¶
func (p *PDFPainter) Line(x1, y1, x2, y2 float64)
func (*PDFPainter) LineTo ¶
func (p *PDFPainter) LineTo(x, y float64)
func (*PDFPainter) MoveTo ¶
func (p *PDFPainter) MoveTo(x, y float64)
func (*PDFPainter) NewPage ¶
func (p *PDFPainter) NewPage()
NewPage finalises the current page and starts a fresh page with the same dimensions. State (CTM / brush / pen / font / curX-Y) is reset to the painter's construction defaults — each page is independent, matching cairo_show_page's contract.
func (*PDFPainter) NewPage1 ¶
func (p *PDFPainter) NewPage1(width, height float64)
NewPage1 is the explicit-size variant — useful when a document alternates between portrait and landscape, or carries one title page at a different size from the rest. Width / height are in PDF user units (1/72 inch).
func (*PDFPainter) PageCount ¶
func (p *PDFPainter) PageCount() int
PageCount returns the total page count after the next WriteTo — finished pages plus the current open page (always at least 1).
func (*PDFPainter) Paint ¶
func (p *PDFPainter) Paint()
Paint fills the entire page with the active brush.
func (*PDFPainter) PaintWithAlpha ¶
func (p *PDFPainter) PaintWithAlpha(alpha uint8)
func (*PDFPainter) Rectangle ¶
func (p *PDFPainter) Rectangle(x, y, w, h float64)
func (*PDFPainter) Rectangle1 ¶
func (p *PDFPainter) Rectangle1(rect geom.Rect)
func (*PDFPainter) ResetClip ¶
func (p *PDFPainter) ResetClip()
func (*PDFPainter) ResetMatrix ¶
func (p *PDFPainter) ResetMatrix()
func (*PDFPainter) Restore ¶
func (p *PDFPainter) Restore() int
func (*PDFPainter) RestoreTo ¶
func (p *PDFPainter) RestoreTo(n int) bool
func (*PDFPainter) Rotate ¶
func (p *PDFPainter) Rotate(radians float64)
func (*PDFPainter) Save ¶
func (p *PDFPainter) Save() int
func (*PDFPainter) Scale ¶
func (p *PDFPainter) Scale(sx, sy float64)
func (*PDFPainter) ScaledFont ¶
func (p *PDFPainter) ScaledFont() paint.ScaledFont
func (*PDFPainter) SetBrush ¶
func (p *PDFPainter) SetBrush(br paint.Brush)
func (*PDFPainter) SetBrush1 ¶
func (p *PDFPainter) SetBrush1(cr paint.Color)
func (*PDFPainter) SetCompression ¶
func (p *PDFPainter) SetCompression(on bool)
SetCompression toggles FlateDecode compression on the per-page content streams. Default off. With compression on, document size drops by 60-80% on text-heavy or graphics-heavy pages because repeated PDF operator names ("re", "Tj", "rg", etc.) and identical numeric strings collapse under zlib's LZ77 window. Image XObject streams already FlateDecode independently — this flag only affects the per-page operator streams.
Toggling mid-document is allowed; the flag is read at WriteTo time, not at content-emission time. Existing tests inspect raw operators in the output so they stay compression-off; production callers can SetCompression(true) before WriteTo for smaller files.
func (*PDFPainter) SetFont ¶
func (p *PDFPainter) SetFont(f paint.Font)
func (*PDFPainter) SetMatrix ¶
func (p *PDFPainter) SetMatrix(m *geom.Mat3x2)
func (*PDFPainter) SetOperator ¶
func (p *PDFPainter) SetOperator(op paint.Operator)
func (*PDFPainter) SetPen ¶
func (p *PDFPainter) SetPen(pen paint.Pen)
func (*PDFPainter) String ¶
func (p *PDFPainter) String() string
String returns the document as a string. Convenient for tests; PDF content is mostly ASCII so this is safe for inspection (the binary marker after the header is the only non-ASCII byte sequence).
func (*PDFPainter) Stroke ¶
func (p *PDFPainter) Stroke()
func (*PDFPainter) StrokePreserve ¶
func (p *PDFPainter) StrokePreserve()
func (*PDFPainter) Target ¶
func (p *PDFPainter) Target() paint.Surface
func (*PDFPainter) Transform ¶
func (p *PDFPainter) Transform(m *geom.Mat3x2)
func (*PDFPainter) Translate ¶
func (p *PDFPainter) Translate(tx, ty float64)