Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions getfields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package fillpdf

import (
"fmt"
"os/exec"
"path/filepath"
"strings"
)

// Field contains information about the fields exported from a pdf via pdftk
type Field struct {
Type string
Name string
AltName string
Flags string
}

func GetFields(formPDFFile string) ([]Field, error) {
formPDFFile, err := filepath.Abs(formPDFFile)
if err != nil {
return nil, fmt.Errorf("failed to create the absolute path: %v", err)
}

// Check if the form file exists.
e, err := exists(formPDFFile)
if err != nil {
return nil, fmt.Errorf("failed to check if form PDF file exists: %v", err)
} else if !e {
return nil, fmt.Errorf("form PDF file does not exists: '%s'", formPDFFile)
}

// Check if the pdftk utility exists.
_, err = exec.LookPath("pdftk")
if err != nil {
return nil, fmt.Errorf("pdftk utility is not installed")
}

// Create the pdftk command line arguments.
args := []string{
formPDFFile,
"dump_data_fields",
}

output, err := runCommandWithResults("pdftk", args...)
if err != nil {
return nil, fmt.Errorf("pdftk error: %v", err)
}

fieldsData := strings.Split(output, "---\n")

fields := []Field{}
for _, f := range fieldsData {
lines := strings.Split(f, "\n")
if len(lines) <= 2 {
continue
}

field := Field{}

for _, line := range lines {
props := strings.SplitN(line, ": ", 2)

if len(props) != 2 {
continue
}

switch props[0] {
case "FieldType":
field.Type = strings.ToLower(props[1])
case "FieldName":
field.Name = props[1]
case "FieldNameAlt":
field.AltName = props[1]
case "FieldFlags":
field.Flags = props[1]
}
}

fields = append(fields, field)
}

return fields, nil
}
7 changes: 7 additions & 0 deletions sample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ func main() {
if err != nil {
log.Fatal(err)
}

fields, err := fillpdf.GetFields("form.pdf")
if err != nil {
log.Fatal(err)
}

log.Printf("%+v", fields)
}
13 changes: 13 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,16 @@ func runCommandInPath(dir, name string, args ...string) error {

return nil
}

func runCommandWithResults(name string, args ...string) (string, error) {
var stderr bytes.Buffer
cmd := exec.Command(name, args...)
cmd.Stderr = &stderr

out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf(strings.TrimSpace(stderr.String()))
}

return string(out), nil
}