Documentation
¶
Overview ¶
Package document reads a PDF back as a document rather than as text.
A PDF page has no headings, no paragraphs and no lists. It has instructions for drawing glyphs at places, and everything a reader sees above that is an inference from where those places are and how big the glyphs were. So what comes out of here is the richdoc model — richdoc.Heading, richdoc.Paragraph, richdoc.List, richdoc.Table — and every one of those is a judgement this package made, not something the file said.
That is worth stating plainly because the usual phrasing, "convert a PDF to Markdown", suggests the structure was in there and merely needed a different spelling. It was not. A PDF is the output of a typesetter, and this is the typesetter's work read backwards.
What it infers, and from what ¶
a heading text set LARGER than the body. The body size is the one the
document uses for the most characters, measured over every
page; sizes above it become heading levels, largest first.
a paragraph consecutive body lines, joined with spaces, ending at a gap
down the page wider than the lines' own leading.
a list consecutive lines that begin with the same short mark.
a table what [extract.Tables] found, which is its own inference and
documents its own limits.
What it does not do ¶
It does not read ruled lines, columns of a multi-column layout, footnotes, or the reading order of a page whose text was drawn out of order — a PDF is free to draw its last line first, and nothing here reorders a page beyond putting its lines down the page and its pieces left to right.
Nothing here is CGO.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
Parse reads the whole document, one page after another.
⛔ There is no error. Nothing here can fail: the document has already been parsed to get this far, and a page that cannot be READ is an empty page, which is an answer rather than a failure. Returning an error that is always nil would teach every caller to write a branch that never runs — and the one thing worse than an unhandled error is a handled one that cannot happen.
Types ¶
type Limits ¶
type Limits struct {
// HeadingLevels is how many sizes above the body text get to be headings.
// The default is 6, which is as many as Markdown and HTML have; a size
// above the sixth is read AS the sixth rather than ignored, because text
// set large is a heading whatever else is on the page.
HeadingLevels int
// ParagraphBreak is how far below the line above a line has to sit,
// counted in that line's own height, before it starts a new paragraph
// instead of continuing one. The default is 1.8: ordinary leading is
// about 1.2, and the space a typesetter puts above a paragraph takes the
// first line of it past this.
ParagraphBreak float64
}
Limits bound what the inference will say. A zero field means the default, so Limits{} is the default everywhere and a caller changes one thing by naming one field.
type Page ¶
A Page is one page read back as blocks, with the number it had.
The number is here because a caller indexing a document for retrieval has to be able to say which page a passage came from, and because a page is the only unit a PDF actually has.
func PagesWithLimits ¶
PagesWithLimits is Pages with the thresholds said out loud.
⛔ Every page is read, even when a caller only wants one, because the body text size is a property of the DOCUMENT. A page holding nothing but a title would otherwise have that title read as its body text, and a caller asking for page nine would get different headings from a caller asking for the lot.
A page that cannot be read contributes no blocks rather than stopping the document: half a report is worth more to somebody searching than an error, which is the choice extract already made for the text. It is also why there is no error to return — see Parse.