pdfapi

package
v0.1.0-experimental.3 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2021 License: BSD-3-Clause Imports: 12 Imported by: 0

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

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

View Source
const (
	Newline = "\n" //Newline = "\r\n"

)

Variables

This section is empty.

Functions

func Deg2Rad

func Deg2Rad(theta float32) float32

Deg2Rad returns degree angle coordinates to radians.

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

func (canvas *Canvas) MoveTo(pt Point)

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

func (canvas *Canvas) SetFillColor(c color.Color)

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

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

func (canvas *Canvas) SetStrokeColor(c color.Color)

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(t Transform)

Transform concatenates a 3x3 matrix with the current transformation matrix. See type Transform.

type Document

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

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

func NewDocument

func NewDocument() *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) Assemble

func (doc *Document) Assemble(c *Canvas)

Assemble takes a page and integrates it into the document's structure.

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. TODO: do not directly couple to document TODO de-couple page geometry from document geometry

type Font

type Font struct {
	Name string
	// contains filtered or unexported fields
}

Font is a type representing fonts in PDF documents.

func NewInternalFont

func NewInternalFont(fname string) *Font

NewInternalFont creates a font for one of the PDF standard fonts, given a font's name (e.g., "Helveltica").

func (*Font) String

func (fn *Font) String() string

String returns the Font's indentifier string.

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.

func (*Path) CurveTo

func (path *Path) CurveTo(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) LineTo

func (path *Path) LineTo(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 by a vector.

func (*Path) MoveTo

func (path *Path) MoveTo(pt Point)

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

func (*Path) Rectangle

func (path *Path) Rectangle(rect Rectangle, corner Unit)

Rectangle appends a complete rectangle to the path. If corner > 0, the corners of the rectangle will be rounded. All four corners will have the same radius.

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

func (text *Text) AddGlyphs(s string)

Text adds a string to the text object.

func (*Text) AdvanceCursor

func (text *Text) AdvanceCursor(x Unit)

AdvanceCursor moves the text cursor in x-direction.

func (*Text) Cursor

func (text *Text) Cursor() Point

Cursor returns the current cursor location. This is where new glyphs will be positioned.

func (*Text) MoveCursor

func (text *Text) MoveCursor(diff Point)

MoveCursor moves the text cursor by a vector.

func (*Text) MoveCursorTo

func (text *Text) MoveCursorTo(pt Point)

MoveCursorTo moves the text cursor to a point.

func (*Text) SetFont

func (text *Text) SetFont(font *Font, size Unit)

SetFont changes the current font to a standard font.

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

func (t Transform) Rotated(theta float32) Transform

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

func (Transform) Scaled

func (t Transform) Scaled(x, y float32) Transform

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

func (Transform) Shifted

func (t Transform) Shifted(by Point) Transform

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

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