forked from reillywatson/fillpdf
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfillpdf.go
191 lines (166 loc) · 4.65 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* FillPDF - Fill PDF forms
* Copyright DesertBit
* Authors: Roland Singer, Alexander Félix
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fillpdf
import (
"bufio"
"encoding/binary"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"unicode/utf16"
)
// 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.
// Checkboxes specify one string for checked (checkedString) and one string for
// unchecked (uncheckedString). The specification can be done on each individual
// checkbox, but lets assume that all checkboxes in the same document will
// use the same strings.
func Fill(form Form, formPDFFile, destPDFFile, checkedString, uncheckedString string, overwrite bool) error {
var err error
// Check if the pdftk utility exists.
if _, err := exec.LookPath("pdftk"); err != nil {
return err
}
// Get the absolute paths.
if formPDFFile, err = getAbs(formPDFFile); err != nil {
return err
}
if destPDFFile, err = filepath.Abs(destPDFFile); err != nil {
return err
}
// Create a temporary directory.
tmpDir, err := ioutil.TempDir("", "fillpdf-")
if err != nil {
return err
}
// Remove the temporary directory on defer again.
defer func() {
os.RemoveAll(tmpDir)
}()
// Create the temporary output file path.
outputFile := filepath.Clean(tmpDir + "/output.pdf")
// Create the fdf data file.
fdfFile := filepath.Clean(tmpDir + "/data.fdf")
if err := createFdfFile(form, fdfFile, checkedString, uncheckedString); err != nil {
return err
}
// Create the pdftk command line arguments.
args := []string{
formPDFFile,
"fill_form", fdfFile,
"output", outputFile,
"flatten",
}
// Run the pdftk utility.
if err := runCommandInPath(tmpDir, "pdftk", args...); err != nil {
return fmt.Errorf("pdftk error: %v", err)
}
// Check if the destination file exists.
e, err := exists(destPDFFile)
if err != nil {
return err
} else if e {
if !overwrite {
return fmt.Errorf("destination PDF file already exists: '%s'", destPDFFile)
}
if err := os.Remove(destPDFFile); err != nil {
return err
}
}
// On success, copy the output file to the final destination.
if err := copyFile(outputFile, destPDFFile); err != nil {
return err
}
return nil
}
// createFdfFile with 16 bit encoded utf to enable creation of pdf with special characters
func createFdfFile(form Form, path, checkedString, uncheckedString string) error {
// Create the file.
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
// Create a new writer.
b := bufio.NewWriter(file)
// Header
b.WriteString("%FDF-1.2\n")
b.WriteString("\xE2\xE3\xCF\xD3\n")
b.WriteString("1 0 obj \n")
b.WriteString("<<\n")
b.WriteString("/FDF \n")
b.WriteString("<<\n")
b.WriteString("/Fields [\n")
// Write the form data.
for key, value := range form {
var valStr string
switch v := value.(type) {
case bool:
if v {
valStr = checkedString
} else {
valStr = uncheckedString
}
default:
valStr = fmt.Sprintf("%v", value)
}
b.WriteString("<<\n")
b.WriteString("/T (")
b.Write(EncodeUTF16(key, true))
b.WriteString(")\n")
b.WriteString("/V (")
b.Write(EncodeUTF16(valStr, true))
b.WriteString(")\n")
b.WriteString(">>\n")
}
// Footer
b.WriteString("]\n")
b.WriteString(">>\n")
b.WriteString(">>\n")
b.WriteString("endobj \n")
b.WriteString("trailer\n")
b.WriteString("\n")
b.WriteString("<<\n")
b.WriteString("/Root 1 0 R\n")
b.WriteString(">>\n")
b.WriteString("%%EOF\n")
// Flush everything.
return b.Flush()
}
// Taken from https://gist.github.com/ik5/65de721ca495fa1bf451
// EncodeUTF16 get a utf8 string and translate it into a slice of bytes of ucs2
func EncodeUTF16(s string, addBom bool) []byte {
r := []rune(s)
iresult := utf16.Encode(r)
var bytes []byte
if addBom {
bytes = make([]byte, 2)
bytes = []byte{254, 255}
}
for _, i := range iresult {
temp := make([]byte, 2)
binary.BigEndian.PutUint16(temp, i)
bytes = append(bytes, temp...)
}
return bytes
}