Documentation ¶
Overview ¶
Package pdfapi will implement a low-level API for the PDF format.
The original package is by Ross Light (zombiezen). I copied it, rather than importing it, because I intend to write a low level PDF API from scratch and will use Ross's code as a loose reference.
There are other PDF packages around (most notably gofpdf (/github.com/jung-kurt/gofpdf) and gopdf (/github.com/signintech/gopdf). However, for my purpose these a often too high-level. Ross's package is the most concise of all and has a Go-like API (in contrast to, e.g., gofpdf).
My focus for the PDF API will be on concurrency, Unicode typesetting and the usage of PDF fragment templates.
Status ¶
This is just a bag of ideas. Nothing useful for anyone else yet.
License ¶
The original code has a BSD 2-clause license. Changes are published under a BSD 3-clause licencse. Please refer to the license file for details.
PDF Format ¶
The PDF format can roughly be broken into four parts:
- Header
- List of Objects (with Streams)
- XRef
- Trailer
This package will stick closely to the nomenclature of PDF where possible.
Index ¶
- Constants
- func Deg2Rad(theta float32) float32
- type Canvas
- func (canvas *Canvas) Close() error
- func (canvas *Canvas) CropBox() Rectangle
- func (canvas *Canvas) Document() *Document
- func (canvas *Canvas) DrawImage(img image.Image, rect Rectangle)
- func (canvas *Canvas) DrawImageReference(ref Reference, rect Rectangle)
- func (canvas *Canvas) DrawLine(pt1, pt2 Point)
- func (canvas *Canvas) DrawText(text *Text)
- func (canvas *Canvas) Fill(p *Path)
- func (canvas *Canvas) FillStroke(p *Path)
- func (canvas *Canvas) MoveTo(pt Point)
- func (canvas *Canvas) PopState()
- func (canvas *Canvas) PushState()
- func (canvas *Canvas) SetFillColor(c color.Color)
- func (canvas *Canvas) SetLineDash(phase Unit, dash []Unit)
- func (canvas *Canvas) SetLineWidth(w Unit)
- func (canvas *Canvas) SetStrokeColor(c color.Color)
- func (canvas *Canvas) Size() (width, height Unit)
- func (canvas *Canvas) Stroke(p *Path)
- func (canvas *Canvas) Transform(t Transform)
- type Document
- type Font
- type FontFormat
- type Path
- type Point
- type Rectangle
- type Reference
- type Text
- type Transform
- type Unit
Constants ¶
const ( Courier = "Courier" CourierBold = "Courier-Bold" CourierOblique = "Courier-Oblique" CourierBoldOblique = "Courier-BoldOblique" Helvetica = "Helvetica" HelveticaBold = "Helvetica-Bold" HelveticaOblique = "Helvetica-Oblique" HelveticaBoldOblique = "Helvetica-BoldOblique" Symbol = "Symbol" Times = "Times-Roman" TimesBold = "Times-Bold" TimesItalic = "Times-Italic" TimesBoldItalic = "Times-BoldItalic" ZapfDingbats = "ZapfDingbats" )
Standard 14 fonts
const (
Newline = "\n" //Newline = "\r\n"
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Canvas ¶
type Canvas struct {
// contains filtered or unexported fields
}
Canvas is a two-dimensional drawing region on a single page. You can obtain a canvas once you have created a document.
func (*Canvas) Close ¶
Close flushes the page's stream to the document. This must be called once drawing has completed or else the document will be inconsistent.
func (*Canvas) DrawImage ¶
DrawImage paints a raster image at the given location and scaled to the given dimensions. If you want to render the same image multiple times in the same document, use DrawImageReference.
func (*Canvas) DrawImageReference ¶
DrawImageReference paints the raster image referenced in the document at the given location and scaled to the given dimensions.
func (*Canvas) DrawLine ¶
DrawLine paints a straight line from pt1 to pt2 using the current stroke color and line width.
func (*Canvas) FillStroke ¶
FillStroke fills then strokes the given path. This operation has the same effect as performing a fill then a stroke, but does not repeat the path in the file.
func (*Canvas) PopState ¶
func (canvas *Canvas) PopState()
PopState restores the most recently saved graphics state by popping it from the stack.
func (*Canvas) PushState ¶
func (canvas *Canvas) PushState()
PushState saves a copy of the current graphics state. The state can later be restored using Pop.
func (*Canvas) SetFillColor ¶
SetFillColor changes the current fill color to the given RGB triple (in device RGB space).
func (*Canvas) SetLineDash ¶
SetLineDash changes the line dash pattern in the current graphics state. Examples:
c.SetLineDash(0, []Unit{}) // solid line c.SetLineDash(0, []Unit{3}) // 3 units on, 3 units off... c.SetLineDash(0, []Unit{2, 1}) // 2 units on, 1 unit off... c.SetLineDash(1, []Unit{2}) // 1 unit on, 2 units off, 2 units on...
func (*Canvas) SetLineWidth ¶
SetLineWidth changes the stroke width to the given value.
func (*Canvas) SetStrokeColor ¶
SetStrokeColor changes the current stroke color to the given RGB triple (in device RGB space).
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document provides a high-level drawing interface for the PDF format.
func (*Document) AddImage ¶
AddImage encodes an image into the document's stream and returns its PDF file reference. This reference can be used to draw the image multiple times without storing the image multiple times.
type Font ¶
type Font struct { Name string // contains filtered or unexported fields }
Font is a type representing fonts in PDF documents.
func NewInternalFont ¶
NewInternalFont creates a font for one of the PDF standard fonts, given a font's name (e.g., "Helveltica").
type FontFormat ¶
type FontFormat int8
const ( Standard FontFormat Type1 Type3 TrueType_Mac TrueType_Win OpenType WOFF WOFF2 )
type Path ¶
type Path struct {
// contains filtered or unexported fields
}
Path is a shape that can be painted on a canvas. The zero value is an empty path.
func (*Path) Close ¶
func (path *Path) Close()
Close appends a line segment from the current point to the starting point of the subpath.
type Rectangle ¶
type Rectangle struct {
Min, Max Point
}
A Rectangle defines a rectangle with two points.
type Text ¶
type Text struct {
// contains filtered or unexported fields
}
Text is a PDF text object. The zero value is an empty text object.
func (*Text) AdvanceCursor ¶
AdvanceCursor moves the text cursor in x-direction.
func (*Text) Cursor ¶
Cursor returns the current cursor location. This is where new glyphs will be positioned.
func (*Text) MoveCursor ¶
MoveCursor moves the text cursor by a vector.
func (*Text) MoveCursorTo ¶
MoveCursorTo moves the text cursor to a point.
type Transform ¶
type Transform [6]float32
Transform represents an affine transformation (on a PDF graphics state). The six indices map to values in a 3x3-matrix as shown below:
⎛ a b 0 ⎞ ⎜ c d 0 ⎥ ⎝ e f 1 ⎠
For more information, see Section 8.3.4 of ISO 32000-1.
func Identity ¶
func Identity() Transform
Identity is the neutral transformation. This is the starting point for chaining transform operations. To create a transformation which shifts a point by vector (a, b) and then rotates around origin by 30 degrees:
vector := Point{a, b} T := Identity().Shifted(vector).Rotated(Deg2Rad(30)) mycanvas.Transform(T)
func (Transform) Rotated ¶
Rotate rotates the canvas's coordinate system by a given angle (in radians), counterclockwise.