Documentation ¶
Overview ¶
Package pdf implements reading of PDF files.
Overview ¶
PDF is Adobe's Portable Document Format, ubiquitous on the internet. A PDF document is a complex data format built on a fairly simple structure. This package exposes the simple structure along with some wrappers to extract basic information. If more complex information is needed, it is possible to extract that information by interpreting the structure exposed by this package.
Specifically, a PDF is a data structure built from Values, each of which has one of the following Kinds:
Null, for the null object. Integer, for an integer. Real, for a floating-point number. Bool, for a boolean value. Name, for a name constant (as in /Helvetica). String, for a string constant. Dict, for a dictionary of name-value pairs. Array, for an array of values. Stream, for an opaque data stream and associated header dictionary.
The accessors on Value—Int64, Float64, Bool, Name, and so on—return a view of the data as the given type. When there is no appropriate view, the accessor returns a zero result. For example, the Name accessor returns the empty string if called on a Value v for which v.Kind() != Name. Returning zero values this way, especially from the Dict and Array accessors, which themselves return Values, makes it possible to traverse a PDF quickly without writing any error checking. On the other hand, it means that mistakes can go unreported.
The basic structure of the PDF file is exposed as the graph of Values.
Most richer data structures in a PDF file are dictionaries with specific interpretations of the name-value pairs. The Font and Page wrappers make the interpretation of a specific Value as the corresponding type easier. They are only helpers, though: they are implemented only in terms of the Value API and could be moved outside the package. Equally important, traversal of other PDF data structures can be implemented in other packages as needed.
Index ¶
- Variables
- func Interpret(strm Value, do func(stk *Stack, op string))
- type Column
- type Columns
- type Content
- type Font
- type Outline
- type Page
- func (p Page) Content() Content
- func (p Page) Font(name string) Font
- func (p Page) Fonts() []string
- func (p Page) GetPlainText(fonts map[string]*Font) (result string, err error)
- func (p Page) GetTextByColumn() (Columns, error)
- func (p Page) GetTextByRow() (result Rows, err error)
- func (p Page) Resources() Value
- type Point
- type Reader
- type Rect
- type Row
- type Rows
- type Stack
- type Text
- type TextEncoding
- type TextHorizontal
- type TextVertical
- type Value
- func (v Value) Bool() bool
- func (v Value) Float64() float64
- func (v Value) Index(i int) Value
- func (v Value) Int64() int64
- func (v Value) IsNull() bool
- func (v Value) Key(key string) Value
- func (v Value) Keys() []string
- func (v Value) Kind() ValueKind
- func (v Value) Len() int
- func (v Value) Name() string
- func (v Value) RawString() string
- func (v Value) Reader() io.ReadCloser
- func (v Value) String() string
- func (v Value) Text() string
- func (v Value) TextFromUTF16() string
- type ValueKind
- Bugs
Constants ¶
This section is empty.
Variables ¶
var DebugOn = false
DebugOn is responsible for logging messages into stdout. If problems arise during reading, set it true.
var ErrInvalidPassword = fmt.Errorf("encrypted PDF: invalid password")
Functions ¶
func Interpret ¶
Interpret interprets the content in a stream as a basic PostScript program, pushing values onto a stack and then calling the do function to execute operators. The do function may push or pop values from the stack as needed to implement op.
Interpret handles the operators "dict", "currentdict", "begin", "end", "def", and "pop" itself.
Interpret is not a full-blown PostScript interpreter. Its job is to handle the very limited PostScript found in certain supporting file formats embedded in PDF files, such as cmap files that describe the mapping from font code points to Unicode code points.
There is no support for executable blocks, among other limitations.
Types ¶
type Column ¶
type Column struct { Position int64 Content TextVertical }
Column represents the contents of a column
type Font ¶
type Font struct { V Value // contains filtered or unexported fields }
A Font represent a font in a PDF file. The methods interpret a Font dictionary stored in V.
func (Font) Encoder ¶
func (f Font) Encoder() TextEncoding
Encoder returns the encoding between font code point sequences and UTF-8.
type Outline ¶
An Outline is a tree describing the outline (also known as the table of contents) of a document.
type Page ¶
type Page struct {
V Value
}
A Page represent a single page in a PDF file. The methods interpret a Page dictionary stored in V.
func (Page) GetPlainText ¶
GetPlainText returns the page's all text without format. fonts can be passed in (to improve parsing performance) or left nil
func (Page) GetTextByColumn ¶
GetTextByColumn returns the page's all text grouped by column
func (Page) GetTextByRow ¶
GetTextByRow returns the page's all text grouped by rows
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
A Reader is a single PDF file open for reading.
func NewReaderEncrypted ¶
NewReaderEncrypted opens a file for reading, using the data in f with the given total size. If the PDF is encrypted, NewReaderEncrypted calls pw repeatedly to obtain passwords to try. If pw returns the empty string, NewReaderEncrypted stops trying to decrypt the file and returns an error.
func (*Reader) GetPlainText ¶
GetPlainText returns all the text in the PDF file
func (*Reader) Outline ¶
Outline returns the document outline. The Outline returned is the root of the outline tree and typically has no Title itself. That is, the children of the returned root are the top-level entries in the outline.
type Row ¶
type Row struct { Position int64 Content TextHorizontal }
Row represents the contents of a row
type Stack ¶
type Stack struct {
// contains filtered or unexported fields
}
A Stack represents a stack of values.
type Text ¶
type Text struct { Font string // the font used FontSize float64 // the font size, in points (1/72 of an inch) X float64 // the X coordinate, in points, increasing left to right Y float64 // the Y coordinate, in points, increasing bottom to top W float64 // the width of the text, in points S string // the actual UTF-8 text }
A Text represents a single piece of text drawn on a page.
type TextEncoding ¶
type TextEncoding interface { // Decode returns the UTF-8 text corresponding to // the sequence of code points in raw. Decode(raw string) (text string) }
A TextEncoding represents a mapping between font code points and UTF-8 text.
type TextHorizontal ¶
type TextHorizontal []Text
TextHorizontal implements sort.Interface for sorting a slice of Text values in horizontal order, left to right, and then top to bottom within a column.
func (TextHorizontal) Len ¶
func (x TextHorizontal) Len() int
func (TextHorizontal) Less ¶
func (x TextHorizontal) Less(i, j int) bool
func (TextHorizontal) Swap ¶
func (x TextHorizontal) Swap(i, j int)
type TextVertical ¶
type TextVertical []Text
TextVertical implements sort.Interface for sorting a slice of Text values in vertical order, top to bottom, and then left to right within a line.
func (TextVertical) Len ¶
func (x TextVertical) Len() int
func (TextVertical) Less ¶
func (x TextVertical) Less(i, j int) bool
func (TextVertical) Swap ¶
func (x TextVertical) Swap(i, j int)
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
A Value is a single PDF value, such as an integer, dictionary, or array. The zero Value is a PDF null (Kind() == Null, IsNull() = true).
func (Value) Float64 ¶
Float64 returns v's float64 value, converting from integer if necessary. If v.Kind() != Float64 and v.Kind() != Int64, Float64 returns 0.
func (Value) Index ¶
Index returns the i'th element in the array v. If v.Kind() != Array or if i is outside the array bounds, Index returns a null Value.
func (Value) IsNull ¶
IsNull reports whether the value is a null. It is equivalent to Kind() == Null.
func (Value) Key ¶
Key returns the value associated with the given name key in the dictionary v. Like the result of the Name method, the key should not include a leading slash. If v is a stream, Key applies to the stream's header dictionary. If v.Kind() != Dict and v.Kind() != Stream, Key returns a null Value.
func (Value) Keys ¶
Keys returns a sorted list of the keys in the dictionary v. If v is a stream, Keys applies to the stream's header dictionary. If v.Kind() != Dict and v.Kind() != Stream, Keys returns nil.
func (Value) Name ¶
Name returns v's name value. If v.Kind() != Name, Name returns the empty string. The returned name does not include the leading slash: if v corresponds to the name written using the syntax /Helvetica, Name() == "Helvetica".
func (Value) RawString ¶
RawString returns v's string value. If v.Kind() != String, RawString returns the empty string.
func (Value) Reader ¶
func (v Value) Reader() io.ReadCloser
Reader returns the data contained in the stream v. If v.Kind() != Stream, Reader returns a ReadCloser that responds to all reads with a “stream not present” error.
func (Value) String ¶
String returns a textual representation of the value v. Note that String is not the accessor for values with Kind() == String. To access such values, see RawString, Text, and TextFromUTF16.
func (Value) Text ¶
Text returns v's string value interpreted as a “text string” (defined in the PDF spec) and converted to UTF-8. If v.Kind() != String, Text returns the empty string.
func (Value) TextFromUTF16 ¶
TextFromUTF16 returns v's string value interpreted as big-endian UTF-16 and then converted to UTF-8. If v.Kind() != String or if the data is not valid UTF-16, TextFromUTF16 returns the empty string.
Notes ¶
Bugs ¶
The package is incomplete, although it has been used successfully on some large real-world PDF files.
There is no support for closing open PDF files. If you drop all references to a Reader, the underlying reader will eventually be garbage collected.
The library makes no attempt at efficiency. A value cache maintained in the Reader would probably help significantly.
The support for reading encrypted files is weak.
The Value API does not support error reporting. The intent is to allow users to set an error reporting callback in Reader, but that code has not been implemented.