Skip to content

Simple PDF drawing library#

The package pdfdraw is a simple, low level, drawing library. It is mainly created because I cannot remember the PDF operators.

Use

func New() *Object

or

func NewStandalone() *Object

to get a PDF draw object:

type Object struct {
}

There are a lot of methods defined on the pdfdraw object, such as

func (pd *Object) Fill() *Object

which return the object itself. So you can call these methods by chaining:

myobj := pdfdraw.New()
myobj.ColorNonstroking(col).Moveto(x0, y0).Lineto(x1, y1).Lineto(x2, y1).Lineto(x3, y0).Close().Fill()

or by calling these methods from the object:

myobj := pdfdraw.New()
myobj.ColorNonstroking(col)
myobj.Moveto(x0, y0)
myobj.Lineto(x1, y1)
myobj.Lineto(x2, y1)
myobj.Lineto(x3, y0)
myobj.Close()
myobj.Fill()

After you have chained the methods together, you can call the .String() to get the textual representation of the drawing commands.

Standalone or not to stand alone?#

The NewStandalone() function creates a q...Q encapsulated PDF string, which is normally used to stop leaking settings for the next drawing commands.

Methods#

Method Description PDF operator
Color(color.Color) Set the color for stroking and non-stroking operations (depending on the color space)
ColorStroking(color.Color) Set the color for stroking operations (depending on the color space)
ColorNonStroking(color.Color) Set the color for stroking operations (depending on the color space)
Close() Close the sub path h
Clip() Clip the sub path using the even-odd rule W*
Curveto(c1x,c1y,c2x,c2y,x1,y1 bag.ScaledPoint) Append a bezier curve from the current point to point 1 controlled by control points 1 and 2 c
Endpath() End the current path n
Moveto(x, y bag.ScaledPoint) Move the cursor relative to the current point m
Lineto(x, y bag.ScaledPoint) Draw a straight line from the current point to the point given at x and y l
Circle(x, y, radiusX, radiusY bag.ScaledPoint) Draw a circle mainly c
Rect(x, y, wd, ht bag.ScaledPoint) Draw a rectangle re
Save() Save the graphics state q
Restore() Restore the graphics state Q
Fill() Fill the current object f
Stroke() Paint the current object without filling it S
StrokeFill() Paint the current object and fill it B
LineWidth(wd bag.ScaledPoint) Set the line width w
Literal(s string) Insert the string without interpretation -
SetDash(dasharray []uint, dashphase uint) SetDash sets the dash pattern d
String()