Documentation
¶
Overview ¶
Package extract reads a PDF page back: the text on it, with where each piece of it sits, and the images it places.
A page does not hold text. It holds instructions for drawing glyphs, and the text is what those glyphs were meant to say — which the document says only if it was asked to. Where it does not, this says so rather than guessing: a run whose characters could not be worked out comes back marked, so that a caller can tell "this page says nothing" from "this page could not be read".
Index ¶
- Variables
- func Assemble(runs []Run) string
- func Join(line []Run) string
- func Lines(runs []Run) [][]Run
- func Text(d *reader.Document, page int) (string, error)
- func TextContext(ctx context.Context, d *reader.Document, page int) (string, error)
- type Image
- type Run
- type Span
- type Table
- type TableOptions
Constants ¶
This section is empty.
Variables ¶
var ErrTooLarge = errors.New("extract: the page asks for more operations, text runs or bytes than the limit allows")
ErrTooLarge is returned, by Runs, Text, Images and their Context forms alike, for a page whose walk asks for more operations, more text runs or more bytes than the limits allow. No partial result comes with it.
Functions ¶
func Assemble ¶
Assemble puts runs back together as lines of text: everything on one baseline in the order it sits across the page, with a space wherever the gap between two pieces is wide enough to be one.
func Join ¶ added in v0.7.0
Join puts one line's pieces back together, with a space wherever the gap between two of them is wide enough to be one and nothing where it is only kerning. It is Assemble for a single line, and exported for the same reason Lines is.
func Lines ¶ added in v0.7.0
Lines gathers runs into the lines they were drawn on, down the page and then left to right within each line. A line is allowed to wobble by a fraction of its own height, which is what a superscript does.
It is exported because the rule has to be the SAME rule: Assemble uses it to put a page's text back together, and anything that reads structure out of a page — where a paragraph ends, which line is a heading — has to agree with it line for line or the two disagree about what the page says.
func TextContext ¶ added in v0.11.1
TextContext is Text bounded by ctx: when ctx is cancelled or its deadline passes the walk of the page's content stops, within a few operations, and ctx's error comes back (context.Canceled or context.DeadlineExceeded, for errors.Is) with no text. A page read from a file one did not write can be made to take as long as its writer likes, and Text has no way to say enough; this is it. A nil ctx is context.Background.
Types ¶
type Image ¶
type Image struct {
// Name is what the page's resources call it, or empty for one written
// into the content stream.
Name string
// Width and Height are the picture's own, in samples.
Width, Height int
// X and Y are where its bottom left corner lands on the page, and
// DrawnWidth and DrawnHeight how large it is drawn, in points. A picture
// turned or skewed by the page still reports the box it covers.
X, Y float64
DrawnWidth, DrawnHeight float64
// Matrix is the transform in force where the picture was drawn, as
// a b c d e f: it maps the unit square the picture occupies onto the page,
// so (0,0) is its bottom left corner and (1,1) its top right.
//
// ⛔ THE BOX ABOVE CANNOT TELL A TURNED PICTURE FROM A STRAIGHT ONE, and
// some callers must. A redaction has to destroy the pixels under a region
// (ISO 32000-1, 12.5.6.23) and therefore has to map that region into the
// picture's own samples; with only the box it covers, a picture at an angle
// would have more of it destroyed than was asked for, which is its own
// betrayal. b and c are zero for an upright picture, so this is also how a
// caller says "I can only handle the straight ones" and refuses the rest by
// name rather than guessing.
Matrix [6]float64
// Filter is the last filter still on the data: DCTDecode for a JPEG that
// comes out as one, JPXDecode for a JPEG 2000, and empty for data this
// has unfiltered into plain samples.
Filter reader.Name
// Data is the picture's bytes, with every filter this understands undone.
Data []byte
// Dict is the image's own dictionary, which says how to read the data:
// its colour space, how many bits a sample takes, and whether it is a
// mask.
Dict reader.Dict
// Inline reports a picture written into the content stream rather than
// held as a resource of its own.
Inline bool
}
An Image is a picture the page places, and where it puts it.
What comes back is the picture as the file holds it — the bytes and the dictionary describing them — rather than pixels. A JPEG comes out as a JPEG, which is what anyone pulling pictures out of a document wants: re-encoding it would lose something for nothing. Turning the other kinds into pixels means reading their colour space, which is the renderer's work rather than this package's.
type Run ¶
type Run struct {
// Text is what the run says.
Text string
// X and Y are where it starts, in the page's own coordinates — up from
// the bottom left corner, in points.
X, Y float64
// Width is how far the pen moved drawing it, and Size how tall the text
// is, both in points.
Width, Size float64
// Font is the name the page's resources give the font.
Font string
// Space is how wide a space would be here, which is what says whether a
// gap between two runs is a word break or only kerning.
Space float64
// Invisible reports text drawn in the mode that puts no ink on the page.
// It is how a scanner puts what it read underneath the picture it read it
// from, so it is text worth having — and worth knowing about.
Invisible bool
// Unreadable reports a run some of whose codes said nothing about which
// characters they were: the font carried no map and named its glyphs
// after numbers. What Text holds is then only the part that could be read.
Unreadable bool
// Op is where the operation that drew this run sits in the content stream
// it was scanned from, so a caller can EDIT the page and not only read it:
// a redaction has to cut the glyphs inside a region out of the stream, and
// deciding which glyphs those are is what this package already does.
//
// ⛔ SEVERAL RUNS CAN SHARE ONE SPAN. A TJ array shows several strings with
// a single operator, so a caller that wants to be rid of one of them must
// REWRITE the span, not delete it — two runs with equal Op are two pieces
// of one operation, and deleting the span would take the other away too.
//
// ⛔ AND THE SPAN IS INTO THE STREAM THAT DREW THE RUN, which is the page's
// own content only when InForm is false. See InForm.
Op Span
// InForm reports a run drawn from inside a Form XObject rather than from
// the page's own content stream.
//
// ⛔ ITS Op THEREFORE DOES NOT INDEX THE PAGE. Cutting the page's bytes at
// those offsets would cut somewhere else entirely — and an XObject is
// SHARED, so editing it to change one page changes every page that draws
// it. A caller that edits a page must refuse such a run rather than guess,
// which is the shape ops.Doc.RecolourText already has: it counts the text
// it could not reach and says so.
InForm bool
}
A Run is a piece of text drawn in one go: one font, one size, one place.
type Span ¶ added in v0.10.0
type Span struct {
From, To int
}
A Span is a byte range in a content stream: From is the first byte of an operation's first operand — or of the operator itself when it has none — and To is just past the operator, as reader.Operation reports them.
The zero Span means "nowhere", which is what a run carries when nothing was scanned for it.
type Table ¶ added in v0.6.0
type Table struct {
// Page is the page the table was found on, counting from one.
Page int
// Rows holds the cells, top row first and left to right within a row.
// Every row has the same number of cells, and a cell the page left empty
// is an empty string.
Rows [][]string
// X and Y are the bottom left corner of the TEXT the table is made of,
// and Width and Height its extent, in the page's own coordinates. A table
// drawn inside a frame has a frame a little larger than this; a table
// drawn without one has no frame at all, which is the usual case and the
// reason the box is measured from the words.
//
// It is every word of the block, including a bullet left out of
// [Table.Rows]: the box says where to look on the page, and a reader
// looking there wants to see the bullets too.
X, Y, Width, Height float64
}
A Table is a grid of text read off a page.
A PDF page does not contain a table. It contains text put at places, and a table is what a reader sees when those places line up. So a Table here is an inference, and Table.Rows is what the alignment said rather than what the author declared — there is nothing in the file to declare it with.
func Tables ¶ added in v0.6.0
Tables is every table on the page, top to bottom, with the default options.
func TablesInRuns ¶ added in v0.9.0
func TablesInRuns(runs []Run, page int, o TableOptions) []Table
TablesInRuns is TablesWithOptions over runs a caller chose, and page is only the number it will put in each Table.Page.
⛔ It exists so that a caller who knows something this does not can take lines OUT before asking. A numbered heading above a table is the case that made it necessary: "2. Vue d'ensemble" leaves a gap after its number, the table below it leaves that same gap empty on every row, so the heading and the table became ONE two-column table — and the real four-column table under it was lost, because the heading's own text reached across every one of its boundaries.
Nothing in the alignment says that line is a heading. Its SIZE says so, and what counts as a heading size is a property of the whole document, which is go-pdfkit/document's business and not this package's.
func TablesWithOptions ¶ added in v0.6.0
TablesWithOptions is Tables with the thresholds said out loud.
⛔ A page laid out in two columns of prose is a grid too: the gutter is a stretch every line leaves empty, so a short enough stretch of such a page comes back as a two-column table whose cells are lines of text. There is nothing in the page that distinguishes the two. What saves the ordinary case is that prose does not stay out of the gutter for long — one full-width line closes the block — but a caller who knows the document is prose should not be asking. This reports alignment; it does not read minds.
type TableOptions ¶ added in v0.6.0
type TableOptions struct {
// ColumnGapSpaces is how wide a gap must be, counted in spaces, before it
// separates two columns rather than two words. The default is 1.5: one
// space is a word break, and a column boundary is wider than a word break
// or there would be no way to tell a table from a sentence.
ColumnGapSpaces float64
// MinRows and MinColumns are the smallest grid worth reporting, both 2 by
// default. One row is a line of text and one column is a paragraph.
MinRows, MinColumns int
}
TableOptions tunes what counts as a table. A zero field means the default, so TableOptions{} is the default everywhere and a caller changes one thing by naming one field.