pdf

package
v0.0.0-...-ab3d048 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2019 License: BSD-2-Clause Imports: 12 Imported by: 5

Documentation

Overview

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

An example of basic usage:

package main

import (
	"bitbucket.org/zombiezen/gopdf/pdf"
	"fmt"
	"os"
)

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})
	canvas.Stroke(path)

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

	canvas.Close()

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

Index

Constants

View Source
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

Variables

This section is empty.

Functions

This section is empty.

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

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

func (canvas *Canvas) CropBox() Rectangle

CropBox returns the page's crop box.

func (*Canvas) Document

func (canvas *Canvas) Document() *Document

Document returns the document the canvas is attached to.

func (*Canvas) DrawImage

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

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

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

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

DrawText paints a text object onto the canvas.

func (*Canvas) Fill

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

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

func (*Canvas) FillStroke

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

func (canvas *Canvas) Pop()

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

func (*Canvas) Push

func (canvas *Canvas) Push()

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

func (*Canvas) Rotate

func (canvas *Canvas) Rotate(theta float32)

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

func (*Canvas) Scale

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

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

func (*Canvas) SetColor

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

func (canvas *Canvas) SetCropBox(crop Rectangle)

SetCropBox changes the page's crop box.

func (*Canvas) SetLineDash

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

func (canvas *Canvas) SetLineWidth(w Unit)

SetLineWidth changes the stroke width to the given value.

func (*Canvas) SetSize

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

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

func (*Canvas) SetStrokeColor

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

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

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

func (*Canvas) Stroke

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

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

func (*Canvas) Transform

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

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

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

type Document

type Document struct {
	// contains filtered or unexported fields
}

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

func New

func New() *Document

New creates a new document with no pages.

func (*Document) AddImage

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

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

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

func (*Document) NewPage

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

NewPage creates a new canvas with the given dimensions.

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.

func (*Path) Curve

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

Curve appends a cubic Bezier curve to the path.

func (*Path) Line

func (path *Path) Line(pt Point)

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

func (*Path) Move

func (path *Path) Move(pt Point)

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

func (*Path) Rectangle

func (path *Path) Rectangle(rect Rectangle)

Rectangle appends a complete rectangle to the path.

type Point

type Point struct {
	X, Y Unit
}

Point is a 2D point.

type Rectangle

type Rectangle struct {
	Min, Max Point
}

A Rectangle defines a rectangle with two points.

func (Rectangle) Dx

func (r Rectangle) Dx() Unit

Dx returns the rectangle's width.

func (Rectangle) Dy

func (r Rectangle) Dy() Unit

Dy returns the rectangle's height.

type Reference

type Reference struct {
	Number     uint
	Generation uint
}

Reference holds a PDF indirect reference.

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) NextLine

func (text *Text) NextLine()

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

func (*Text) NextLineOffset

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

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

func (text *Text) SetLeading(leading Unit)

SetLeading changes the amount of space between lines.

func (*Text) Text

func (text *Text) Text(s string)

Text adds a string to the text object.

func (*Text) X

func (text *Text) X() Unit

X returns the current x position of the text cursor.

func (*Text) Y

func (text *Text) Y() Unit

Y returns the current y position of the text cursor.

type Unit

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

func (unit Unit) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL