gopdf: Index | Files

package pdf

import ""

Package pdf implements a Portable Document Format writer, as defined in ISO 32000-1.

An example of basic usage:

package main

import (

func main() {
	doc := pdf.New()
	canvas := doc.NewPage(pdf.USLetterWidth, pdf.USLetterHeight)
	canvas.Translate(100, 100)

	path := new(pdf.Path)
	path.Move(pdf.Point{0, 0})
	path.Line(pdf.Point{100, 0})

	text := new(pdf.Text)
	text.SetFont(pdf.Helvetica, 14)
	text.Text("Hello, World!")


	err := doc.Encode(os.Stdout)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)


Package Files

canvas.go doc.go encode.go image.go marshal.go metrics.go objects.go pdf.go stream.go text.go


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

type Canvas Uses

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 Uses

func (canvas *Canvas) Close() error

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) CropBox Uses

func (canvas *Canvas) CropBox() Rectangle

CropBox returns the page's crop box.

func (*Canvas) Document Uses

func (canvas *Canvas) Document() *Document

Document returns the document the canvas is attached to.

func (*Canvas) DrawImage Uses

func (canvas *Canvas) DrawImage(img image.Image, rect Rectangle)

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 Uses

func (canvas *Canvas) DrawImageReference(ref Reference, rect Rectangle)

DrawImageReference paints the raster image referenced in the document at the given location and scaled to the given dimensions.

func (*Canvas) DrawLine Uses

func (canvas *Canvas) DrawLine(pt1, pt2 Point)

DrawLine paints a straight line from pt1 to pt2 using the current stroke color and line width.

func (*Canvas) DrawText Uses

func (canvas *Canvas) DrawText(text *Text)

DrawText paints a text object onto the canvas.

func (*Canvas) Fill Uses

func (canvas *Canvas) Fill(p *Path)

Fill paints the area enclosed by the given path using the current fill color.

func (*Canvas) FillStroke Uses

func (canvas *Canvas) FillStroke(p *Path)

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) Pop Uses

func (canvas *Canvas) Pop()

Pop restores the most recently saved graphics state by popping it from the stack.

func (*Canvas) Push Uses

func (canvas *Canvas) Push()

Push saves a copy of the current graphics state. The state can later be restored using Pop.

func (*Canvas) Rotate Uses

func (canvas *Canvas) Rotate(theta float32)

Rotate rotates the canvas's coordinate system by a given angle (in radians).

func (*Canvas) Scale Uses

func (canvas *Canvas) Scale(x, y float32)

Scale multiplies the canvas's coordinate system by the given scalars.

func (*Canvas) SetColor Uses

func (canvas *Canvas) SetColor(r, g, b float32)

SetColor changes the current fill color to the given RGB triple (in device RGB space).

func (*Canvas) SetCropBox Uses

func (canvas *Canvas) SetCropBox(crop Rectangle)

SetCropBox changes the page's crop box.

func (*Canvas) SetLineDash Uses

func (canvas *Canvas) SetLineDash(phase Unit, dash []Unit)

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 Uses

func (canvas *Canvas) SetLineWidth(w Unit)

SetLineWidth changes the stroke width to the given value.

func (*Canvas) SetSize Uses

func (canvas *Canvas) SetSize(width, height Unit)

SetSize changes the page's media box (the size of the physical medium).

func (*Canvas) SetStrokeColor Uses

func (canvas *Canvas) SetStrokeColor(r, g, b float32)

SetStrokeColor changes the current stroke color to the given RGB triple (in device RGB space).

func (*Canvas) Size Uses

func (canvas *Canvas) Size() (width, height Unit)

Size returns the page's media box (the size of the physical medium).

func (*Canvas) Stroke Uses

func (canvas *Canvas) Stroke(p *Path)

Stroke paints a line along the given path using the current stroke color.

func (*Canvas) Transform Uses

func (canvas *Canvas) Transform(a, b, c, d, e, f float32)

Transform concatenates a 3x3 matrix with the current transformation matrix. The arguments map to values in the 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 (*Canvas) Translate Uses

func (canvas *Canvas) Translate(x, y Unit)

Translate moves the canvas's coordinates system by the given offset.

type Document Uses

type Document struct {
    // contains filtered or unexported fields

Document provides a high-level drawing interface for the PDF format.

func New Uses

func New() *Document

New creates a new document with no pages.

func (*Document) AddImage Uses

func (doc *Document) AddImage(img image.Image) Reference

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.

func (*Document) Encode Uses

func (doc *Document) Encode(w io.Writer) error

Encode writes the document to a writer in the PDF format.

func (*Document) NewPage Uses

func (doc *Document) NewPage(width, height Unit) *Canvas

NewPage creates a new canvas with the given dimensions.

type Path Uses

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 Uses

func (path *Path) Close()

Close appends a line segment from the current point to the starting point of the subpath.

func (*Path) Curve Uses

func (path *Path) Curve(pt1, pt2, pt3 Point)

Curve appends a cubic Bezier curve to the path.

func (*Path) Line Uses

func (path *Path) Line(pt Point)

Line appends a line segment from the current point to the given location.

func (*Path) Move Uses

func (path *Path) Move(pt Point)

Move begins a new subpath by moving the current point to the given location.

func (*Path) Rectangle Uses

func (path *Path) Rectangle(rect Rectangle)

Rectangle appends a complete rectangle to the path.

type Point Uses

type Point struct {
    X, Y Unit

Point is a 2D point.

type Rectangle Uses

type Rectangle struct {
    Min, Max Point

A Rectangle defines a rectangle with two points.

func (Rectangle) Dx Uses

func (r Rectangle) Dx() Unit

Dx returns the rectangle's width.

func (Rectangle) Dy Uses

func (r Rectangle) Dy() Unit

Dy returns the rectangle's height.

type Reference Uses

type Reference struct {
    Number     uint
    Generation uint

Reference holds a PDF indirect reference.

type Text Uses

type Text struct {
    // contains filtered or unexported fields

Text is a PDF text object. The zero value is an empty text object.

func (*Text) NextLine Uses

func (text *Text) NextLine()

NextLine advances the current text position to the next line, based on the current leading.

func (*Text) NextLineOffset Uses

func (text *Text) NextLineOffset(tx, ty Unit)

NextLineOffset moves the current text position to an offset relative to the beginning of the line.

func (*Text) SetFont Uses

func (text *Text) SetFont(fontName string, size Unit)

SetFont changes the current font to a standard font. This also changes the leading to 1.2 times the font size.

func (*Text) SetLeading Uses

func (text *Text) SetLeading(leading Unit)

SetLeading changes the amount of space between lines.

func (*Text) Text Uses

func (text *Text) Text(s string)

Text adds a string to the text object.

func (*Text) X Uses

func (text *Text) X() Unit

X returns the current x position of the text cursor.

func (*Text) Y Uses

func (text *Text) Y() Unit

Y returns the current y position of the text cursor.

type Unit Uses

type Unit float32

Unit is a device-independent dimensional type. On a new canvas, this represents 1/72 of an inch.

const (
    Pt   Unit = 1
    Inch Unit = 72
    Cm   Unit = 28.35

Common unit scales

const (
    USLetterWidth  Unit = 8.5 * Inch
    USLetterHeight Unit = 11.0 * Inch

    A4Width  Unit = 21.0 * Cm
    A4Height Unit = 29.7 * Cm

Common page sizes

func (Unit) String Uses

func (unit Unit) String() string

Package pdf imports 12 packages (graph) and is imported by 18 packages. Updated 2019-04-23. Refresh now. Tools for package owners.