Documentation
¶
Overview ¶
Package ops is the verb layer of go-pdfkit: what people actually do to a PDF they already have. Merge two files, pull out pages three to seven, turn a page the right way up, drop the metadata, split a report into chapters.
A document here is an ordered list of pages, each borrowed from a source file, plus the document-level pieces. Every operation rearranges that list or annotates its entries, and nothing is applied until Doc.Bytes is called. An operation therefore costs nothing until it has to, pages from several files mix freely, and the list stays a plain value.
Index ¶
- func ParseRange(spec string, pageCount int) ([]int, error)
- type Bookmark
- type Doc
- func (d *Doc) Append(other *Doc)
- func (d *Doc) Bates(spec, prefix string, start, digits int) error
- func (d *Doc) Blank(width, height float64)
- func (d *Doc) Booklet() error
- func (d *Doc) Bytes() ([]byte, error)
- func (d *Doc) ClearInfo()
- func (d *Doc) Compress()
- func (d *Doc) Crop(spec string, box [4]float64) error
- func (d *Doc) Decrypt()
- func (d *Doc) Delete(spec string) error
- func (d *Doc) DropOutlines()
- func (d *Doc) Encrypt(e reader.Encryption)
- func (d *Doc) Flatten()
- func (d *Doc) Info() reader.Dict
- func (d *Doc) InsertBlank(i int) error
- func (d *Doc) Move(from, to int) error
- func (d *Doc) NUp(n int) error
- func (d *Doc) Overlay(other *Doc) error
- func (d *Doc) PageCount() int
- func (d *Doc) PageNumbers(spec, format string) error
- func (d *Doc) Protection() (reader.Protection, bool)
- func (d *Doc) RemoveAnnotations()
- func (d *Doc) Resize(spec string, box [4]float64) error
- func (d *Doc) Reverse()
- func (d *Doc) Rotate(spec string, degrees int) error
- func (d *Doc) Rotation(i int) (int, error)
- func (d *Doc) Sanitize()
- func (d *Doc) Select(spec string) error
- func (d *Doc) SetInfo(key reader.Name, value string)
- func (d *Doc) SetOutline(marks []Bookmark)
- func (d *Doc) SetRotation(spec string, degrees int) error
- func (d *Doc) SetVersion(v string)
- func (d *Doc) Split(n int) ([]*Doc, error)
- func (d *Doc) SplitAt(at ...int) ([]*Doc, error)
- func (d *Doc) Stamp(spec string, s Stamp) error
- func (d *Doc) Underlay(other *Doc) error
- func (d *Doc) Version() string
- func (d *Doc) Watermark(spec, text string) error
- type Filling
- type Font
- type Page
- type Position
- type Stamp
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseRange ¶
ParseRange turns the page selection people write — "1-3,7,10-", "even", "last", "all" — into page numbers counting from one, in the order given and with duplicates kept, because "1,1,2" really does mean three pages.
A range with no start begins at the first page and one with no end runs to the last. A descending range like "5-2" counts down, which is how a reversed extract is written.
Types ¶
type Bookmark ¶ added in v0.4.0
A Bookmark is an entry of an outline written from scratch: what it says, the page of this document it points at counting from one, and whatever sits under it.
It is what a document assembled rather than merged carries — a shared edit, a report built out of pieces — where there is no source outline to carry over because the outline is the caller's own.
type Doc ¶
type Doc struct {
// contains filtered or unexported fields
}
A Doc is a document being assembled: pages in the order they will be written, and the trailer's information dictionary.
func FromDocument ¶
FromDocument wraps an already-parsed document. Every page the document reports can be read — the page tree walk has already established that — so there is nothing here that can fail.
func OpenWithPassword ¶
OpenWithPassword reads a PDF file that may be encrypted.
func (*Doc) Append ¶
Append adds the pages of another document to the end of this one. The other document is not changed, and the two may come from different files.
func (*Doc) Bates ¶
Bates numbers the pages a range names with a running serial, the way a legal exhibit is marked: a fixed prefix and a zero-padded counter.
func (*Doc) Booklet ¶
Booklet reorders the pages for saddle-stitch printing — folded in half and stapled through the spine — and lays them two to a sheet. Blank pages are added so the count is a multiple of four, which is what folding needs.
func (*Doc) Bytes ¶
Bytes writes the document out as a PDF file. Every borrowed page is copied out of the file it came from with the attributes it inherited written onto it, so pages from different documents keep their own geometry and resources.
func (*Doc) ClearInfo ¶
func (d *Doc) ClearInfo()
ClearInfo drops the whole information dictionary, which is what "remove the metadata" means.
func (*Doc) Compress ¶ added in v0.2.0
func (d *Doc) Compress()
Compress asks for the file to be written with its objects packed into compressed streams and a cross-reference stream, which is what every writer since PDF 1.5 does and what makes a file a good deal smaller. It costs nothing but a version of 1.5, which every reader in use has understood for twenty years.
func (*Doc) Crop ¶
Crop sets the visible area of the pages the range names, in points, as [left bottom right top]. It sets the crop box; the media box, which is the paper the page is on, is left alone.
func (*Doc) Decrypt ¶ added in v0.2.0
func (d *Doc) Decrypt()
Decrypt writes the file without protection. A document opened with the right password is already decrypted, so this only undoes an earlier call to Doc.Encrypt; a file read with OpenWithPassword and written out is unprotected either way.
func (*Doc) DropOutlines ¶
func (d *Doc) DropOutlines()
DropOutlines leaves the bookmarks behind. They are kept by default: a merge that loses every bookmark is a poor merge.
func (*Doc) Encrypt ¶ added in v0.2.0
func (d *Doc) Encrypt(e reader.Encryption)
Encrypt protects the file that will be written. Two people can open it: whoever knows the user password, subject to the permissions, and whoever knows the owner password, subject to nothing.
An encrypted file is not byte-for-byte reproducible — encryption needs randomness, by design — so a document written twice with the same call comes out different both times, and neither can be compared with the other.
func (*Doc) Flatten ¶
func (d *Doc) Flatten()
Flatten draws each annotation's own appearance into the page and then drops the annotation, so what one reader sees is what every reader sees — which is what filling in a form and then flattening it means.
func (*Doc) InsertBlank ¶
InsertBlank puts an empty page before the page at i, counting from one; an index one past the end appends. The size is taken from the page it precedes, or from the one before it at the end of the document.
func (*Doc) Move ¶
Move takes the page at from and puts it at to, both counting from one, the other pages closing up behind it.
func (*Doc) NUp ¶
NUp lays n pages on each sheet, in reading order. The sheet keeps the size of the first page, and the grid is whichever arrangement of at least n cells gives cells closest in shape to the pages going into them.
func (*Doc) Overlay ¶
Overlay draws each page of another document on top of the pages here, in order. When the other document has fewer pages its last page is not repeated: pages past its end are left alone.
func (*Doc) PageNumbers ¶
PageNumbers writes a number at the foot of the pages a range names. The format is a stamp's text, so "{page} of {pages}" and "— {page} —" both work.
func (*Doc) Protection ¶ added in v0.2.0
func (d *Doc) Protection() (reader.Protection, bool)
Protection reports how the file this document was read from was protected, and false when it was not protected at all — or when the document was not read from a file. It says nothing about how the document will be written: that is what was passed to Doc.Encrypt.
func (*Doc) RemoveAnnotations ¶
func (d *Doc) RemoveAnnotations()
RemoveAnnotations drops every annotation: links, comments, form fields and all. What was drawn on the page stays; what sat on top of it goes.
func (*Doc) Rotate ¶
Rotate turns the pages the range names by the given number of degrees, which must be a multiple of ninety. The rotation is relative to what the page already had, so rotating twice by ninety turns a page upside down.
func (*Doc) Sanitize ¶
func (d *Doc) Sanitize()
Sanitize strips the parts of a file that do something rather than show something: page and annotation actions, JavaScript, launching, form submission, files travelling with a page, and the annotation types that exist to embed or play something.
A document written by this package always carries a catalogue of this package's own making, so document-level scripts, automatic actions on opening and embedded file trees are gone whatever this setting says; sanitising deals with what travels attached to a page.
func (*Doc) Select ¶
Select keeps only the pages the range names, in the order it names them, so it extracts, reorders and duplicates in one verb.
func (*Doc) SetInfo ¶
SetInfo replaces one entry of the information dictionary. An empty value removes it.
func (*Doc) SetOutline ¶ added in v0.4.0
SetOutline writes these bookmarks rather than carrying over the ones the sources had. An entry pointing at a page this document has not got is left out, and so is everything under it: a heading whose section has gone is not a heading any more.
Passing nothing puts the sources' own bookmarks back.
func (*Doc) SetRotation ¶
SetRotation turns the pages the range names to an absolute angle.
func (*Doc) SetVersion ¶
SetVersion sets the version written in the header.
func (*Doc) SplitAt ¶
SplitAt cuts the document before each of the given page numbers, which is how a report is broken into chapters.
type Filling ¶ added in v0.5.0
type Filling struct {
// contains filtered or unexported fields
}
A Filling is a document's form, opened so that it can be filled in and written back.
func OpenForm ¶ added in v0.5.0
OpenForm reads a document's form. It reports false, with no error, for a document that simply has none — including one carrying an AcroForm dictionary a producer left behind with an empty field list, which 561 of the figure corpus's 118 833 files do.
func OpenFormWithPassword ¶ added in v0.5.0
OpenFormWithPassword is the same for a document that is protected.
func (*Filling) Bytes ¶ added in v0.5.0
Bytes writes the original file with the changes appended to it.
type Font ¶
type Font string
A Font names one of the four faces this package can draw with.
type Page ¶
type Page struct {
// contains filtered or unexported fields
}
A Page is one page of a document, borrowed from the file it came from. The rotation is what will be written, not what the source said.
type Stamp ¶
type Stamp struct {
Text string
Font Font
Size float64 // in points; zero means twelve
Colour [3]float64 // red, green and blue, each from zero to one
Opacity float64 // zero means fully opaque
Rotate float64 // degrees, anticlockwise, about the text's own centre
Position Position
Margin float64 // distance from the edge, in points; zero means 24
Start int // the first value of {n}; zero means one
Digits int // {n} padded to this many digits
}
A Stamp is a line of text drawn on a page.
The text may name what it is being drawn on: {page} is the page's number, {pages} the number of pages, and {n} a counter that starts wherever the stamp says and advances once per page stamped, which is what a numbering scheme like Bates needs.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
pdfops
command
Command pdfops does to a PDF the things people actually want done to one: merge, extract, delete, reorder, rotate, crop, split, and read or strip the metadata.
|
Command pdfops does to a PDF the things people actually want done to one: merge, extract, delete, reorder, rotate, crop, split, and read or strip the metadata. |