forked from sitepoint-editors/pdf-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfForm.php
177 lines (144 loc) · 3.29 KB
/
pdfForm.php
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
<?php
class PdfForm
{
/*
* Path to raw PDF form
* @var string
*/
private $pdfurl;
/*
* Form data
* @var array
*/
private $data;
/*
* Path to filled PDF form
* @var string
*/
private $output;
/*
* Flag for flattening the file
* @var string
*/
private $flatten;
/**
* Class Conctructor
*
* @param string $pdfurl
* @param string $data
*/
public function __construct($pdfurl, $data)
{
$this->pdfurl = $pdfurl;
$this->data = $data;
}
/**
* Generate a filled PDF file
*
*/
private function generate()
{
$fdf = $this->makeFdf($this->data);
$this->output = $this->tmpfile();
exec("pdftk {$this->pdfurl} fill_form {$fdf} output {$this->output}{$this->flatten}");
unlink($fdf);
}
/**
* Extract fields information
*
* @param boolean $pretty
* @return string
*/
public function fields($pretty = false)
{
$tmp = $this->tmpfile();
exec("pdftk {$this->pdfurl} dump_data_fields > {$tmp}");
$con = file_get_contents($tmp);
unlink($tmp);
return $pretty == true ? nl2br($con) : $con;
}
/**
* Generate FDF file
* @param array $data
* @return string
*/
public function makeFdf($data)
{
$fdf = '%FDF-1.2
1 0 obj<</FDF<< /Fields[';
foreach ($data as $key => $value) {
$fdf .= '<</T(' . $key . ')/V(' . $value . ')>>';
}
$fdf .= "] >> >>
endobj
trailer
<</Root 1 0 R>>
%%EOF";
$fdf_file = $this->tmpfile();
file_put_contents($fdf_file, $fdf);
return $fdf_file;
}
/**
* Set the flatten flag
*
* @return pdfWriter
*/
public function flatten()
{
$this->flatten = ' flatten';
return $this;
}
/**
* Save the file
*
* @param string $path
*/
public function save($path = null)
{
if (is_null($path)) {
return $this;
}
if (!$this->output) {
$this->generate();
}
$dest = pathinfo($path, PATHINFO_DIRNAME);
if (!file_exists($dest)) {
mkdir($dest, 0775, true);
}
copy($this->output, $path);
unlink($this->output);
$this->output = $path;
return $this;
}
/**
* Force download the filled PDF file
*
*/
public function download()
{
if (!$this->output) {
$this->generate();
}
$filepath = $this->output;
if (file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=' . uniqid(gethostname()) . '.pdf');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($filepath);
exit;
}
}
/**
* Create a temporary file and return the name
*
* @return string
*/
private function tmpfile()
{
return tempnam(sys_get_temp_dir(), gethostname());
}
}