Documentation
¶
Overview ¶
Package xfa opens XFA forms (Adobe LiveCycle and AEM Designer forms) in PDFs, lays them out the way Acrobat does, and fills them, in pure Go.
Dynamic XFA forms have no ordinary PDF pages: only Acrobat and Reader can show them, and browsers and most other readers show a "Please wait" page instead. This package reads the form's design (the XFA template) and data (the datasets packet), merges them, and lays the pages out itself, so the form can be shown and filled anywhere:
f, err := xfa.Open(pdfBytes)
doc, err := f.Layout() // pages of positioned boxes, text and fields
html := xfa.ViewerHTML(doc, opts) // a live form to type into
flat, err := xfa.Flatten(doc) // an ordinary PDF any viewer shows
h := f.Handler(xfa.HandlerOptions{...}) // the live form, served
err = f.Set("form1[0].Page1[0].Name[0]", "DOE, JANE A")
saved, err := f.Save() // the original PDF with the new values appended
Save writes an incremental update with a new datasets packet, as Acrobat does, so Acrobat and Reader open the filled form with its own design, scripts and signature fields intact.
What it covers: data binding (once, global, dataRef, none; repeating subforms), positioned and flowed layout (tb, lr-tb, rl-tb, tables and rows), growable fields and draws, captions, borders, fills, lines, rectangles, arcs, images, rich text, comb fields, check boxes, radio groups, choice lists, date display pictures, page areas with their master-page content, content areas, page and content-area breaks, and splitting flowed content across pages. Scripts (JavaScript and FormCalc) don't run; page-number fields are filled the way their scripts would.
Index ¶
- Variables
- func CompleteChain(chain []*x509.Certificate, client *http.Client) ([]*x509.Certificate, error)
- func FetchOCSP(chain []*x509.Certificate, client *http.Client) ([][]byte, error)
- func Flatten(doc *Doc) ([]byte, error)
- func TimestampAuthority(url string, client *http.Client) func(signature []byte) ([]byte, error)
- func ViewerCSS() string
- func ViewerHTML(doc *Doc, o ViewerOptions) []byte
- func ViewerJS() string
- type Doc
- type Edge
- type Field
- type FieldBox
- type Font
- type Form
- func (f *Form) DataXML() []byte
- func (f *Form) Dynamic() bool
- func (f *Form) Fields() []Field
- func (f *Form) Handler(o HandlerOptions) http.Handler
- func (f *Form) Layout() (doc *Doc, err error)
- func (f *Form) Save() ([]byte, error)
- func (f *Form) Set(som, value string) error
- func (f *Form) SetValues(vals map[string]string) error
- func (f *Form) Sign(o SignOptions) ([]byte, error)
- func (f *Form) SignatureFields() []SignatureField
- func (f *Form) Signed() bool
- func (f *Form) Value(som string) (string, bool)
- func (f *Form) Values() map[string]string
- type HandlerOptions
- type Inst
- type Item
- type Node
- func (n *Node) Append(k *Node) *Node
- func (n *Node) Attr(local string) string
- func (n *Node) Bytes() []byte
- func (n *Node) Child(local string) *Node
- func (n *Node) Children(local string) []*Node
- func (n *Node) DelAttr(local string)
- func (n *Node) Elems() []*Node
- func (n *Node) FindID(id string) *Node
- func (n *Node) FirstElem() *Node
- func (n *Node) HasAttr(local string) bool
- func (n *Node) Index() int
- func (n *Node) Path(names ...string) *Node
- func (n *Node) QName() string
- func (n *Node) Remove(k *Node)
- func (n *Node) SetAttr(local, v string)
- func (n *Node) SetText(s string)
- func (n *Node) String() string
- func (n *Node) TextContent() string
- type NodeKind
- type Page
- type SignOptions
- type SignatureField
- type TextLine
- type TextRun
- type ViewerOptions
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSigned means the signature field already holds a signature. ErrSigned = errors.New("xfa: the field is already signed") // ErrLocked means a signature locks the field. ErrLocked = errors.New("xfa: the field is locked") )
var ErrNoField = errors.New("xfa: no field has that SOM path")
ErrNoField means no field has that SOM path.
var ErrNoXFA = errors.New("xfa: the PDF has no XFA form")
ErrNoXFA means the PDF has no XFA form.
Functions ¶
func CompleteChain ¶
func CompleteChain(chain []*x509.Certificate, client *http.Client) ([]*x509.Certificate, error)
CompleteChain adds the issuers a chain is missing by following each last certificate's issuer URL (its AIA caIssuers entry) until a self-signed root, a certificate with no URL, or five fetches. Readers that trust the root can then validate offline. client may be nil.
func FetchOCSP ¶
FetchOCSP asks each certificate's OCSP responder in chain (signing certificate first) whether it's revoked, and returns the answers for SignOptions.OCSP. A revoked certificate is an error. Certificates without a responder, and the root, are skipped. client may be nil for http.DefaultClient.
func Flatten ¶
Flatten writes a laid-out form as an ordinary PDF: every page drawn with the PDF base-14 fonts the layout measured with, field values printed in place, and nothing left to fill. Items marked not for print (buttons and the like) are left out. Any PDF viewer shows the result.
func TimestampAuthority ¶
TimestampAuthority returns a SignOptions.Timestamp function that asks the RFC 3161 timestamp authority at url to timestamp the signature. client may be nil for http.DefaultClient.
func ViewerHTML ¶
func ViewerHTML(doc *Doc, o ViewerOptions) []byte
ViewerHTML writes a self-contained page that draws the laid-out form with inputs on its fields.
Types ¶
type Doc ¶
type Doc struct {
Pages []Page `json:"pages"`
Fonts []Font `json:"fonts"`
Dynamic bool `json:"dynamic"`
}
Doc is a laid-out form: pages of items in points, origin at each page's top left, y down.
type Edge ¶
type Edge struct {
W float64 `json:"w"`
Color string `json:"color,omitempty"`
Dash string `json:"dash,omitempty"` // dashed, dotted, dashDot, dashDotDot
}
Edge is a stroke.
type Field ¶
type Field struct {
SOM string `json:"som"`
Name string `json:"name"`
Kind string `json:"kind"` // text, multiline, check, radio, choice, date, number, password, signature, image
Value string `json:"value"`
Display string `json:"display,omitempty"`
Options []string `json:"options,omitempty"` // choices: what's shown
Saves []string `json:"saves,omitempty"` // choices: what's stored, when it differs
On string `json:"on,omitempty"` // checkbox: its "checked" value
Label string `json:"label,omitempty"`
Tip string `json:"tip,omitempty"`
Required bool `json:"required,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
MaxChars int `json:"maxChars,omitempty"`
Comb int `json:"comb,omitempty"`
Hidden bool `json:"hidden,omitempty"`
Bound bool `json:"bound"` // the value lives in the form's data
}
Field describes one field a person can fill.
type FieldBox ¶
type FieldBox struct {
SOM string `json:"som"`
Kind string `json:"kind"`
Value string `json:"value"`
Display string `json:"display,omitempty"`
Options []string `json:"options,omitempty"`
Saves []string `json:"saves,omitempty"`
On string `json:"on,omitempty"`
Group string `json:"group,omitempty"` // a radio button's exclusion group
Required bool `json:"required,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
MaxChars int `json:"maxChars,omitempty"`
Comb int `json:"comb,omitempty"`
Font int `json:"font"`
HAlign string `json:"hAlign,omitempty"`
VAlign string `json:"vAlign,omitempty"`
Mark string `json:"mark,omitempty"` // check boxes: check, circle, cross, diamond, square, star
Shape string `json:"shape,omitempty"` // check boxes: square or round
Tip string `json:"tip,omitempty"`
Page string `json:"page,omitempty"` // "page" or "pages": filled with the page number or count
ISO string `json:"iso,omitempty"` // dates: the value as YYYY-MM-DD, when it reads as a date
Signed bool `json:"signed,omitempty"` // signature fields: signed; Display says by whom
}
FieldBox is a fillable field on a page.
type Font ¶
type Font struct {
Typeface string `json:"typeface"`
Size float64 `json:"size"`
Bold bool `json:"bold,omitempty"`
Italic bool `json:"italic,omitempty"`
Underline bool `json:"underline,omitempty"`
Strike bool `json:"strike,omitempty"`
Color string `json:"color,omitempty"` // "#rrggbb"; "" is black
// HScale is the font's horizontal scale (fontHorizontalScale), 1 when 0.
HScale float64 `json:"hScale,omitempty"`
}
Font is a resolved font: a typeface, size and style.
type Form ¶
type Form struct {
// contains filtered or unexported fields
}
Form is an XFA form read from a PDF: its packets, the form built from the template and the data, and what's changed since it was opened.
func Open ¶
Open reads an XFA form from a PDF. Encrypted forms open with the empty user password, as Reader opens them.
func (*Form) Dynamic ¶
Dynamic reports whether the form is a dynamic XFA form: one whose pages exist only as its design, which browsers and most PDF readers can't show. Static XFA forms carry ordinary PDF pages as well.
func (*Form) Handler ¶
func (f *Form) Handler(o HandlerOptions) http.Handler
Handler serves the form for filling in a browser:
GET / the page, laid out with the current values
POST /set {"SOM": path, "Value": value}: sets a value and answers
with the form laid out again (Doc as JSON)
POST /save saves, and answers with the result of opts.Save
The page calls ./set and ./save, so the handler can be mounted anywhere with http.StripPrefix, as long as the page's address ends in a slash. It does no authentication: the program that mounts it decides who may reach it. Calls are serialized, so one handler can serve one form to several tabs.
func (*Form) Save ¶
Save writes the form's values back into the PDF: a new revision of the datasets packet appended to the original file, as Acrobat saves. The original bytes, earlier signatures and Reader usage rights stay as they were. Nothing changed gives the original bytes.
func (*Form) Set ¶
Set stores a field's value: the data value, as the field would store it. For a checkbox, the value is its on or off value (or "1"/"0", "true" and "false", which are mapped); for an exclusion group, a member's on value or its label; for a choice list, a stored value or a shown item. Dates take YYYY-MM-DD and are stored through the field's bind picture.
func (*Form) Sign ¶
func (f *Form) Sign(o SignOptions) ([]byte, error)
Sign signs the form's signature field and returns the signed PDF: the original bytes, then one incremental update with any values set since the form was opened, the signature field and the signature. The form then stands for the signed PDF, so later values go after the signature.
The signature covers the whole file. A dynamic form's signature field is the AcroForm field named by its SOM path, with an invisible widget; Acrobat shows the signature in the XFA field's place.
func (*Form) SignatureFields ¶
func (f *Form) SignatureFields() []SignatureField
SignatureFields lists the form's signature fields, signed or not.
type HandlerOptions ¶
type HandlerOptions struct {
// Title heads the page.
Title string
// Save receives the saved PDF (the original with the new values
// appended) when Save is pressed. Without it the page has no Save
// button.
Save func(pdf []byte) error
// SignatureText labels signature fields.
SignatureText string
// Sign, when set, makes unsigned signature fields sign when clicked.
// It gives the options for one signature (the signer, its chain and
// anything else); Field is filled in. The signed PDF goes to Save,
// which Sign needs. Asking for a PIN is the signer's business.
Sign func(field string) (SignOptions, error)
}
HandlerOptions shape the handler Form.Handler returns.
type Inst ¶
type Inst struct {
T *Node // template element
Kind string // subform, subformSet, field, draw, exclGroup, area, pageArea
Name string
SOM string
Parent *Inst
Kids []*Inst
Presence string
// contains filtered or unexported fields
}
Inst is one instance of a template container in the merged form.
type Item ¶
type Item struct {
T string `json:"t"`
X float64 `json:"x"`
Y float64 `json:"y"`
W float64 `json:"w,omitempty"`
H float64 `json:"h,omitempty"`
X2 float64 `json:"x2,omitempty"`
Y2 float64 `json:"y2,omitempty"`
Fill string `json:"fill,omitempty"`
Edges []Edge `json:"edges,omitempty"`
Radius float64 `json:"r,omitempty"`
Lines []TextLine `json:"lines,omitempty"`
Field *FieldBox `json:"field,omitempty"`
Src string `json:"src,omitempty"`
Rot float64 `json:"rot,omitempty"`
Items []Item `json:"items,omitempty"`
NoPrint bool `json:"noPrint,omitempty"`
Clip bool `json:"clip,omitempty"`
}
Item is something drawn on a page.
rect a box: Fill, Edges (one for all sides, or top, right, bottom, left), Radius line from X,Y to X2,Y2 with Edges[0] ellipse in X,Y,W,H with Edges[0] and Fill text Lines, relative to X,Y image Src (a data: URL) in X,Y,W,H field Field: an input in X,Y,W,H, with Lines for its printed value group Items rotated by Rot degrees (counterclockwise) about X,Y
type Node ¶
type Node struct {
Kind NodeKind
Space string // the prefix as written ("xfa" in xfa:data), not the namespace URI
Local string // element name, or the target of a processing instruction
Attrs []xml.Attr
Text string // character data, comment, processing instruction body or directive
Kids []*Node
Parent *Node
}
Node is one node of an XML packet.
func ParseXML ¶
ParseXML reads a packet into a tree. It refuses documents nested deeper than 512 elements, which no real form needs.
func (*Node) Index ¶
Index is n's position among its parent's element children of the same name (the [n] in a SOM path).
func (*Node) SetAttr ¶
SetAttr sets an attribute by local name, adding it without a prefix when it's missing.
func (*Node) TextContent ¶
TextContent joins the character data under n.
type SignOptions ¶
type SignOptions struct {
// Field is the signature field's SOM path, as Fields lists it.
Field string
// Signer holds the private key: a smart card's key through
// winsign, or any crypto.Signer. RSA and ECDSA keys work.
Signer crypto.Signer
// Chain is the signing certificate first, then the certificates that
// issued it. Include them up to the root, so readers that trust the
// root can build the chain offline.
Chain []*x509.Certificate
// Name, Reason, Location and ContactInfo go in the signature as
// written. Readers take the signer's name from the certificate; Name
// is what the signature's appearance shows.
Name, Reason, Location, ContactInfo string
// Time is the signing time; the zero time is now. Without a
// timestamp it's only the computer's clock, and readers say so.
Time time.Time
// CAdES makes a PAdES-style signature (/ETSI.CAdES.detached) instead
// of the default /adbe.pkcs7.detached.
CAdES bool
// OCSP responses and CRLs (DER), embedded so the signature validates
// offline and after the certificate expires. FetchOCSP gets them.
OCSP, CRLs [][]byte
// Timestamp, when set, gets an RFC 3161 timestamp token for the
// signature value. TimestampAuthority makes one for a URL.
Timestamp func(signature []byte) ([]byte, error)
}
SignOptions describe a signature.
type SignatureField ¶
type SignatureField struct {
SOM string `json:"som"`
Label string `json:"label,omitempty"`
Signed bool `json:"signed"`
// Signer is the signing certificate's common name, as the signature
// carries it (it is not verified here). On a CAC it ends in the
// holder's ID number; show Name instead.
Signer string `json:"signer,omitempty"`
// Name is the signer's name to show: the signature's /Name, or one
// made from the certificate (LAST, FIRST M, without an ID number).
Name string `json:"name,omitempty"`
Time time.Time `json:"time,omitzero"`
// Locks are the fields this signature locks once it's signed: the
// field's manifest in the template.
Locks []string `json:"locks,omitempty"`
}
SignatureField is a signature field and what's in it.
type TextLine ¶
type TextLine struct {
X float64 `json:"x"` // left edge, relative to the item
Y float64 `json:"y"` // baseline, relative to the item's top
W float64 `json:"w"` // width as measured
Runs []TextRun `json:"runs"`
}
TextLine is one laid-out line: its baseline position and its runs.
type TextRun ¶
type TextRun struct {
X float64 `json:"x"` // relative to the line's X
Text string `json:"text"`
Font int `json:"font"` // index into Doc.Fonts
W float64 `json:"w"`
}
TextRun is text in one font on a line.
type ViewerOptions ¶
type ViewerOptions struct {
// Title heads the page.
Title string
// Endpoint, when set, is where the page sends changes: it posts
// {"SOM": path, "Value": value} to Endpoint+"/set" and expects the
// laid-out Doc back as JSON, and posts to Endpoint+"/save" when Save is
// pressed. Without one, the page is a standalone copy of the form that
// keeps what is typed only until it is closed.
Endpoint string
// SignatureText labels signature fields. The default is "Sign in Acrobat".
SignatureText string
// NoSave leaves out the Save button.
NoSave bool
// CanSign makes unsigned signature fields buttons that post
// {"SOM": path} to Endpoint+"/sign" and expect the new layout back.
CanSign bool
}
ViewerOptions shape the page ViewerHTML writes.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
xfa
command
Command xfa works with XFA forms from the command line:
|
Command xfa works with XFA forms from the command line: |
|
internal
|
|
|
cms
Package cms builds CMS (PKCS #7) SignedData for PDF signatures: detached signatures over a PDF's byte ranges, as /adbe.pkcs7.detached and /ETSI.CAdES.detached expect, and encapsulated ones for tests.
|
Package cms builds CMS (PKCS #7) SignedData for PDF signatures: detached signatures over a PDF's byte ranges, as /adbe.pkcs7.detached and /ETSI.CAdES.detached expect, and encapsulated ones for tests. |
|
Package winsign signs with the keys in the Windows certificate store, smart cards (CAC, PIV) included, through CNG.
|
Package winsign signs with the keys in the Windows certificate store, smart cards (CAC, PIV) included, through CNG. |