-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfillpdf.go
140 lines (116 loc) · 3.41 KB
/
fillpdf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
)
// Form represents the PDF form.
// This is a key value map.
type Form map[string]interface{}
// Fill a PDF form with the specified form values and create a final filled PDF file.
// One variadic boolean specifies, whenever to overwrite the destination file if it exists.
func Fill(form Form, formPDFFile, destPDFFile string, overwrite ...bool) (err error) {
// Get the absolute paths.
formPDFFile, err = filepath.Abs(formPDFFile)
if err != nil {
return fmt.Errorf("failed to create the absolute path: %v", err)
}
destPDFFile, err = filepath.Abs(destPDFFile)
if err != nil {
return fmt.Errorf("failed to create the absolute path: %v", err)
}
// Check if the form file exists.
e, err := exists(formPDFFile)
if err != nil {
return fmt.Errorf("failed to check if form PDF file exists: %v", err)
} else if !e {
return fmt.Errorf("form PDF file does not exists: '%s'", formPDFFile)
}
// Check if the pdftk utility exists.
_, err = exec.LookPath("pdftk")
if err != nil {
return fmt.Errorf("pdftk utility is not installed!")
}
// Create a temporary directory.
tmpDir, err := ioutil.TempDir("", "fillpdf-")
if err != nil {
return fmt.Errorf("failed to create temporary directory: %v", err)
}
// Remove the temporary directory on defer again.
defer func() {
errD := os.RemoveAll(tmpDir)
// Log the error only.
if errD != nil {
log.Printf("fillpdf: failed to remove temporary directory '%s' again: %v", tmpDir, errD)
}
}()
// Create the temporary output file path.
outputFile := filepath.Clean(tmpDir + "/output.pdf")
// Create the fdf data file.
fdfFile := filepath.Clean(tmpDir + "/data.xfdf")
err = createXFdfFile(form, fdfFile)
if err != nil {
return fmt.Errorf("failed to create fdf form data file: %v", err)
}
// Create the pdftk command line arguments.
args := []string{
formPDFFile,
"fill_form", fdfFile,
"output", outputFile,
"flatten",
}
// Run the pdftk utility.
err = runCommandInPath(tmpDir, "pdftk", args...)
if err != nil {
return fmt.Errorf("pdftk error: %v", err)
}
// Check if the destination file exists.
e, err = exists(destPDFFile)
if err != nil {
return fmt.Errorf("failed to check if destination PDF file exists: %v", err)
} else if e {
if len(overwrite) == 0 || !overwrite[0] {
return fmt.Errorf("destination PDF file already exists: '%s'", destPDFFile)
}
err = os.Remove(destPDFFile)
if err != nil {
return fmt.Errorf("failed to remove destination PDF file: %v", err)
}
}
// On success, copy the output file to the final destination.
err = copyFile(outputFile, destPDFFile)
if err != nil {
return fmt.Errorf("failed to copy created output PDF to final destination: %v", err)
}
return nil
}
func createXFdfFile(form Form, path string) error {
// Create the file.
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
// Create a new writer.
w := bufio.NewWriter(file)
// Write the fdf header.
fmt.Fprintln(w, fdfHeader)
// Write the form data.
for key, value := range form {
fmt.Fprintf(w, `<field name="%s"><value>%v</value></field>\n`, key, value)
}
// Write the fdf footer.
fmt.Fprintln(w, fdfFooter)
// Flush everything.
return w.Flush()
}
const fdfHeader = `<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields>`
const fdfFooter = `</fields>
</xfdf>
%%EOF`