pdf

package module
v0.0.0-...-d3b0bd5 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2023 License: BSD-3-Clause Imports: 24 Imported by: 6

README

Go reference documentation Fund the development Homepage

baseline logo

baseline-pdf is a low-level PDF writer for the Go language. It is used in the boxes and glue typesetting library but can be used in other projects as well.

This library has a godoc reference and a more verbose manual.

Status

Not yet used in production. Expect API changes.

License

BSD license - see License.md

Contact

Patrick Gundlach, gundlach@speedata.de

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Logger is initialized to write to io.Discard and the default log level is math.MaxInt, so it should never write anything.
	Logger *slog.Logger
)

Functions

func ArrayToString

func ArrayToString(ary []any) string

ArrayToString converts the objects in ary to a string including the opening and closing bracket

func FloatToPoint

func FloatToPoint(in float64) string

FloatToPoint returns a string suitable as a PDF size value.

func HashToString

func HashToString(h Dict, level int) string

HashToString converts a PDF dictionary to a string including the paired angle brackets (<< ... >>).

func StringToPDF

func StringToPDF(str string) string

StringToPDF returns an escaped string suitable to be used as a PDF object.

Types

type Annotation

type Annotation struct {
	Subtype    Name
	Action     string
	Dictionary Dict
	Rect       [4]float64 // x1, y1, x2, y2
}

An Annotation is a PDF element that is additional to the text, such as a hyperlink or a note.

type Array

type Array []any

Array is a list of anything

func (Array) String

func (ary Array) String() string

type Dict

type Dict map[Name]any

Dict is a string - string dictionary

func (Dict) String

func (d Dict) String() string

type Face

type Face struct {
	FaceID         int
	HarfbuzzFont   *harfbuzz.Font
	UnitsPerEM     int32
	Cmap           fonts.Cmap
	Filename       string
	PostscriptName string
	// contains filtered or unexported fields
}

Face represents a font structure with no specific size. To get the dimensions of a font, you need to create a Font object with a given size.

func LoadFace

func LoadFace(pw *PDF, filename string, idx int) (*Face, error)

LoadFace loads a font from the disc. The index specifies the sub font to be loaded.

func NewFaceFromData

func NewFaceFromData(pw *PDF, data []byte, idx int) (*Face, error)

NewFaceFromData returns a Face object which is a representation of a font file. The first parameter (id) should be the file name of the font, but can be any string. This is to prevent duplicate font loading.

func (*Face) Codepoint

func (face *Face) Codepoint(r rune) fonts.GID

Codepoint tries to find the code point for r. If none found, 0 is returned.

func (*Face) Codepoints

func (face *Face) Codepoints(runes []rune) []int

Codepoints returns the internal code points for the runes.

func (*Face) InternalName

func (face *Face) InternalName() string

InternalName returns a PDF usable name such as /F1

func (*Face) RegisterChar

func (face *Face) RegisterChar(codepoint int)

RegisterChar marks the codepoint as used on the page. For font subsetting.

func (*Face) RegisterChars

func (face *Face) RegisterChars(codepoints []int)

RegisterChars marks the codepoints as used on the page. For font subsetting.

type Imagefile

type Imagefile struct {
	Format        string
	NumberOfPages int
	PageSizes     map[int]map[string]map[string]float64
	Filename      string
	ScaleX        float64
	ScaleY        float64
	W             int
	H             int
	Box           string
	PageNumber    int
	// contains filtered or unexported fields
}

Imagefile represents a physical image file. Images to be place in the PDF must be derived from the image.

func LoadImageFile

func LoadImageFile(pw *PDF, filename string) (*Imagefile, error)

LoadImageFile loads an image from the disc. For PDF files it defaults to page 1 and the /MediaBox.

func LoadImageFileWithBox

func LoadImageFileWithBox(pw *PDF, filename string, box string, pagenumber int) (*Imagefile, error)

LoadImageFileWithBox loads an image from the disc with the given box and page number.

func (*Imagefile) GetPDFBoxDimensions

func (imgf *Imagefile) GetPDFBoxDimensions(p int, boxname string) (map[string]float64, error)

GetPDFBoxDimensions returns the dimensions for the given box. Box must be one of "/MediaBox", "/CropBox", "/BleedBox", "/TrimBox", "/ArtBox".

func (*Imagefile) InternalName

func (imgf *Imagefile) InternalName() string

InternalName returns a PDF usable name such as /F1

type Name

type Name string

Name represents a PDF name such as Adobe Green. The String() method prepends a / (slash) to the name if not present.

func (Name) String

func (n Name) String() string

type NameDest

type NameDest struct {
	PageObjectnumber Objectnumber
	Name             String
	X                float64
	Y                float64
	// contains filtered or unexported fields
}

NameDest represents a named PDF destination. The origin of X and Y are in the top left corner and expressed in DTP points.

type NumDest

type NumDest struct {
	PageObjectnumber Objectnumber
	Num              int
	X                float64
	Y                float64
	// contains filtered or unexported fields
}

NumDest represents a simple PDF destination. The origin of X and Y are in the top left corner and expressed in DTP points.

type Object

type Object struct {
	ObjectNumber Objectnumber
	Data         *bytes.Buffer
	Dictionary   Dict
	Array        []any
	Raw          bool // Data holds everything between object number and endobj
	ForceStream  bool
	// contains filtered or unexported fields
}

Object has information about a specific PDF object

func (*Object) Dict

func (obj *Object) Dict(d Dict) *Object

Dict writes the dict d to a PDF object

func (*Object) Save

func (obj *Object) Save() error

Save adds the PDF object to the main PDF file.

func (*Object) SetCompression

func (obj *Object) SetCompression(compresslevel uint)

SetCompression turns on stream compression if compresslevel > 0

type Objectnumber

type Objectnumber int

Objectnumber represents a PDF object number

func (Objectnumber) Ref

func (o Objectnumber) Ref() string

Ref returns a reference to the object number

func (Objectnumber) String

func (o Objectnumber) String() string

String returns a reference to the object number

type Outline

type Outline struct {
	Children []*Outline
	Title    string
	Open     bool
	Dest     string
	// contains filtered or unexported fields
}

Outline represents PDF bookmarks. To create outlines, you need to assign previously created Dest items to the outline. When Open is true, the PDF viewer shows the child outlines.

type PDF

type PDF struct {
	Catalog           Dict
	InfoDict          Dict
	DefaultPageWidth  float64
	DefaultPageHeight float64
	Colorspaces       []*Separation
	NumDestinations   map[int]*NumDest
	NameDestinations  []*NameDest
	Outlines          []*Outline
	Major             uint // Major version. Should be 1.
	Minor             uint // Minor version. Just for information purposes. No checks are done.
	// contains filtered or unexported fields
}

PDF is the central point of writing a PDF file.

func NewPDFWriter

func NewPDFWriter(file io.Writer) *PDF

NewPDFWriter creates a PDF file for writing to file

func (*PDF) AddPage

func (pw *PDF) AddPage(content *Object, dictnum Objectnumber) *Page

AddPage adds a page to the PDF file. The content stream must be complete.

func (*PDF) Finish

func (pw *PDF) Finish() error

Finish writes the trailer and xref section but does not close the file.

func (*PDF) NewObject

func (pw *PDF) NewObject() *Object

NewObject create a new PDF object and reserves an object number for it. The object is not written to the PDF until Save() is called.

func (*PDF) NewObjectWithNumber

func (pw *PDF) NewObjectWithNumber(objnum Objectnumber) *Object

NewObjectWithNumber create a new PDF object and reserves an object number for it. The object is not written to the PDF until Save() is called.

func (*PDF) NextObject

func (pw *PDF) NextObject() Objectnumber

NextObject returns the next free object number

func (*PDF) Print

func (pw *PDF) Print(s string) error

Print writes the string to the PDF file

func (*PDF) Printf

func (pw *PDF) Printf(format string, a ...any) error

Printf writes the formatted string to the PDF file.

func (*PDF) Println

func (pw *PDF) Println(s string) error

Println writes the string to the PDF file and adds a newline.

func (*PDF) Size

func (pw *PDF) Size() int64

Size returns the current size of the PDF file.

type Page

type Page struct {
	Dictnum     Objectnumber // The "/Page" object
	Annotations []Annotation
	Faces       []*Face
	Images      []*Imagefile
	Width       float64
	Height      float64
	Dict        Dict // Additional dictionary entries such as "/Trimbox"
	// contains filtered or unexported fields
}

Page contains information about a single page.

type Pages

type Pages struct {
	Pages []*Page
	// contains filtered or unexported fields
}

Pages is the parent page structure

type Separation

type Separation struct {
	Obj        Objectnumber
	ID         string
	Name       string
	ICCProfile Objectnumber
	C          float64
	M          float64
	Y          float64
	K          float64
}

Separation represents a spot color

type SortByFaceID

type SortByFaceID []*Face

SortByFaceID is used to sort the order of the written font faces in the PDF file to create reproducible builds.

func (SortByFaceID) Len

func (a SortByFaceID) Len() int

func (SortByFaceID) Less

func (a SortByFaceID) Less(i, j int) bool

func (SortByFaceID) Swap

func (a SortByFaceID) Swap(i, j int)

type SortByName

type SortByName []Name

SortByName implements the sorting sequence.

func (SortByName) Len

func (a SortByName) Len() int

func (SortByName) Less

func (a SortByName) Less(i, j int) bool

func (SortByName) Swap

func (a SortByName) Swap(i, j int)

type SortImagefile

type SortImagefile []*Imagefile

SortImagefile is used to sort the order of the written images in the PDF file to create reproducible builds.

func (SortImagefile) Len

func (a SortImagefile) Len() int

func (SortImagefile) Less

func (a SortImagefile) Less(i, j int) bool

func (SortImagefile) Swap

func (a SortImagefile) Swap(i, j int)

type String

type String string

String is a string that gets automatically converted to (...) or hexadecimal form when placed in the PDF.

func (String) String

func (s String) String() string

Jump to

Keyboard shortcuts

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