Documentation
¶
Overview ¶
Package render turns a page of a PDF into pixels.
It reads with github.com/go-pdfkit/reader and rasterises with github.com/go-gfx/gfx. Nothing outside those and the standard library is used, so it builds for js/wasm and for every architecture the fleet targets — which is what lets a PDF be shown in a browser tab with nothing on the far end.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrTimedOut = errors.New("render: the page was still being drawn when its time ran out")
ErrTimedOut says a page was still being drawn when its time ran out. The image returned with it holds what had been drawn by then.
Functions ¶
Types ¶
type Image ¶ added in v0.17.0
type Image struct {
// Name is the resource name the page draws it by.
Name string
// Filter is the image format the picture was stored in — DCTDecode,
// JPXDecode, JBIG2Decode, CCITTFaxDecode — and is empty when the bytes
// were samples that no image codec had to read.
Filter string
// Stencil says the picture is a one-bit mask, which carries no colours of
// its own: it paints whatever colour is in force through its own shape.
// Its Pic has that shape in the alpha channel and black everywhere else,
// because the colour is not the image's to know.
Stencil bool
// Pic is the decoded picture.
Pic *raster.Image
}
Image is one picture a page draws, decoded.
func Images ¶ added in v0.17.0
Images decodes the pictures the i'th page draws, counting from one, in the order of the names it draws them by.
It exists because a page is the wrong unit for two jobs. Getting the pictures out of a document is one. Finding out whether a codec is right is the other: a page is a composition, so one image decoded wrongly can be averaged away by everything drawn over and around it, and a per-page difference of a few percent says nothing about which image was wrong. That mattered here — a release went out drawing scanned pages dark because "ink appeared" was measured instead of "the right ink".
A picture nothing here can decode is left out rather than returned empty, which is the same answer Page gives by not drawing it. Inline images — the ones written into the content stream — are not returned: they belong to the stream that draws them rather than to the page's resources.
type Options ¶
type Options struct {
// Scale is device pixels per point. Zero means one, which is seventy-two
// to the inch; [Options.DPI] is the same thing said the other way.
Scale float64
// DPI sets the scale from dots per inch. It is ignored when Scale is set.
DPI float64
// Background is painted before anything else. The zero value is opaque
// white, which is what paper is.
Background *color.RGBA
// MaxPixels refuses a page that would come out larger than this, so a
// media box a file made up cannot be asked to fill memory. Zero means
// forty million, which is a little over A4 at 600 dots to the inch.
MaxPixels int
// NoAnnotations leaves out what the page carries beside its content: the
// notes, the highlights, the stamps, and the fields of a form. They are
// drawn by default, because a filled-in form drawn without them is a form
// with nothing in it.
NoAnnotations bool
// MaxDuration is how long the page may be drawn for. Zero means as long as
// it takes, which is what this did before there was anywhere to say
// otherwise.
//
// Some pages take a very long time. Of 59 432 corpus pages drawn at half
// as much again as their own size, 1 131 were still going after twenty
// seconds, and one figure took two hundred and seventy-three. A caller
// drawing somebody else's file — a browser tab, a queue of them, a server
// — cannot afford to wait for the worst of those and has, until now, had
// no way to say so: MaxPixels bounds what comes out, and nothing bounded
// the work of making it.
//
// When the time runs out the page stops being drawn and comes back as far
// as it got, with ErrTimedOut. That is deliberately not a blank: half a
// page is worth more than nothing to somebody scrolling, and the error
// says plainly that it is half.
MaxDuration time.Duration
// AllLayers draws every optional-content layer, including the ones the
// document's default configuration turns off.
//
// The default is to draw what the document says it shows when it is
// opened. On the corpus that is the same picture either way: 275 of the
// 1 633 real forms use optional content and 161 hide a group, but not one
// mark on any page of any of the 8 300 files measured falls inside a layer
// its own configuration hides. See optional.go for the numbers and for why
// the mechanism is read all the same.
//
// A caller that wants everything — a tool showing a layer panel, an
// archive comparing what a file holds against what it displays — sets
// this and gets the behaviour from before optional content was read.
AllLayers bool
}
Options say how a page is to be drawn.