-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpdf-render.php
294 lines (255 loc) · 7.79 KB
/
pdf-render.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
class FPPDFRender
{
/**
* Outputs a PDF entry from a Gravity Form
* var $form_id integer: The form id
* var $lead_id integer: The entry id
* var $output string: either view, save or download
* save will save a copy of the PDF to the server using the FP_PDF_SAVE_LOCATION constant
* var $return boolean: if set to true it will return the path of the saved PDF
* var $template string: if you want to use multiple PDF templates - name of the template file
* var $pdfname string: allows you to pass a custom PDF name to the generator e.g. 'Application Form.pdf' (ensure .pdf is appended to the filename)
* var $fpdf boolean: custom hook to allow the FPDF engine to generate PDFs instead of DOMPDF. Premium Paid Feature.
*/
public function PDF_Generator($form_id, $lead_id, $arguments = array())
{
/*
* Because we merged the create and attach functions we need a measure to only run this function once per session per lead id.
*/
static $pdf_creator = array();
/*
* Set user-variable to output HTML instead of PDF
*/
$html = (isset($_GET['html'])) ? (int) $_GET['html'] : 0;
/*
* Join the form and lead IDs together to get the real ID
*/
$id = $form_id . $lead_id;
/*
* PDF_Generator was becoming too cluttered so store all the variables in an array
*/
$filename = $arguments['pdfname'];
$template = $arguments['template'];
$output = (isset($arguments['output']) && strlen($arguments['output']) > 0) ? $arguments['output'] : 'save';
/*
* Check if the PDF exists and if this function has already run this season
*/
if(in_array($lead_id, $pdf_creator) && file_exists(FP_PDF_SAVE_LOCATION.$id.'/'. $filename))
{
/*
* Don't generate a new PDF, use the existing one
*/
return FP_PDF_SAVE_LOCATION.$id.'/'. $filename;
}
/*
* Add lead to PDF creation tracker
*/
$pdf_creator[] = $lead_id;
/*
* Add filter before we load the template file so we can stop the main process
* Used in premium plugins
* return true to cancel, otherwise run.
*/
$return = apply_filters('fppdfe_pre_load_template', $form_id, $lead_id, $template, $id, $output, $filename, $arguments);
if($return !== true)
{
/*
* Get the tempalte HTML file
*/
$entry = $this->load_entry_data($form_id, $lead_id, $template);
/*
* Output HTML version and return if user requested a HTML version
*/
if($html === 1)
{
echo $entry;
exit;
}
/*
* If successfully got the entry then run the processor
*/
if(strlen($entry) > 0)
{
return $this->PDF_processing($entry, $filename, $id, $output, $arguments);
}
return false;
}
/*
* Used in extensions to return the name of the PDF file when attaching to notifications
*/
return apply_filters('fppdfe_return_pdf_path', $form_id, $lead_id);
}
/**
* Loads the Gravity Form output script (actually the print preview)
*/
private function load_entry_data($form_id, $lead_id, $template)
{
/* set up contstants for Formidable Pro to use so we can override the security on the printed version */
if(file_exists(FP_PDF_TEMPLATE_LOCATION.$template))
{
return FPPDF_Common::get_html_template(FP_PDF_TEMPLATE_LOCATION.$template);
}
else
{
/*
* Check if template file exists in the plugin's core template folder
*/
if(file_exists(FP_PDF_PLUGIN_DIR."templates/" . $template))
{
return FPPDF_Common::get_html_template(FP_PDF_PLUGIN_DIR."templates/" . $template);
}
/*
* If template not found then we will resort to the default template.
*/
else
{
return FPPDF_Common::get_html_template(FP_PDF_PLUGIN_DIR."templates/" . FPPDFGenerator::$default['template']);
}
}
}
/**
* Creates the PDF and does a specific output (see PDF_Generator function above for $output variable types)
*/
public function PDF_processing($html, $filename, $id, $output = 'view', $arguments)
{
/*
* DOMPDF replaced with mPDF in v3.0.0
* Check which version of mpdf we are calling
* Full, Lite or Tiny
*/
if(!class_exists('mPDF'))
{
if(FP_PDF_ENABLE_MPDF_TINY === true)
{
include FP_PDF_PLUGIN_DIR .'/mPDF/mpdf-extra-lite.php';
}
elseif(FP_PDF_ENABLE_MPDF_LITE === true)
{
include FP_PDF_PLUGIN_DIR .'/mPDF/mpdf-lite.php';
}
else
{
include FP_PDF_PLUGIN_DIR .'/mPDF/mpdf.php';
}
}
/*
* Initialise class and set the paper size and orientation
*/
$paper_size = $arguments['pdf_size'];
if(!is_array($paper_size))
{
$orientation = ($arguments['orientation'] == 'landscape') ? '-L' : '';
$paper_size = $paper_size.$orientation;
}
else
{
$orientation = ($arguments['orientation'] == 'landscape') ? 'L' : 'P';
}
$mpdf = new mPDF('', $paper_size, 0, '', 15, 15, 16, 16, 9, 9, $orientation);
/*
* Display PDF is full-page mode which allows the entire PDF page to be viewed
* Normally PDF is zoomed right in.
*/
$mpdf->SetDisplayMode('fullpage');
if(FP_PDF_ENABLE_SIMPLE_TABLES === true)
{
$mpdf->simpleTables = true;
}
/*
* Automatically detect fonts and substitue as needed
*/
if(FP_PDF_DISABLE_FONT_SUBSTITUTION === true)
{
$mpdf->useSubstitutions = false;
}
else
{
$mpdf->SetAutoFont(AUTOFONT_ALL);
$mpdf->useSubstitutions = true;
}
/*
* Set Creator Meta Data
*/
$mpdf->SetCreator('Formidable Pro PDF Extended v'. FP_PDF_EXTENDED_VERSION.'. http://formidablepropdfextended.com');
/*
* Set RTL languages at user request
*/
if($arguments['rtl'] === true)
{
$mpdf->SetDirectionality('rtl');
}
/*
* Set up security if user requested
*/
if($arguments['security'] === true && $arguments['pdfa1b'] !== true && $arguments['pdfx1a'] !== true)
{
$password = (strlen($arguments['pdf_password']) > 0) ? $arguments['pdf_password'] : '';
$master_password = (strlen($arguments['pdf_master_password']) > 0) ? $arguments['pdf_master_password'] : null;
$pdf_privileges = (is_array($arguments['pdf_privileges'])) ? $arguments['pdf_privileges'] : array();
$mpdf->SetProtection($pdf_privileges, $password, $master_password, 128);
}
/* PDF/A1-b support added in v3.4.0 */
if($arguments['pdfa1b'] === true)
{
$mpdf->PDFA = true;
$mpdf->PDFAauto = true;
}
else if($arguments['pdfx1a'] === true) /* PDF/X-1a support added in v3.4.0 */
{
$mpdf->PDFX = true;
$mpdf->PDFXauto = true;
}
/*
* Check if we should auto prompt to print the document on open
*/
if(isset($_GET['print']))
{
$mpdf->SetJS('this.print();');
}
/* load HTML block */
$mpdf->WriteHTML($html);
switch($output)
{
case 'download':
$mpdf->Output($filename, 'D');
exit;
break;
case 'view':
$mpdf->Output(time(), 'I');
exit;
break;
case 'save':
/*
* PDF wasn't writing to file with the F method - http://mpdf1.com/manual/index.php?tid=125
* Return as a string and write to file manually
*/
$pdf = $mpdf->Output('', 'S');
return $this->savePDF($pdf, $filename, $id);
break;
}
}
/**
* Creates the PDF and does a specific output (see PDF_Generator function above for $output variable types)
* var $dompdf Object
*/
public function savePDF($pdf, $filename, $id)
{
/* create unique folder for PDFs */
if(!is_dir(FP_PDF_SAVE_LOCATION.$id))
{
if(!mkdir(FP_PDF_SAVE_LOCATION.$id))
{
trigger_error('Could not create PDF folder in '. FP_PDF_SAVE_LOCATION.$id, E_USER_WARNING);
return;
}
}
$pdf_save = FP_PDF_SAVE_LOCATION.$id.'/'. $filename;
if(!file_put_contents($pdf_save, $pdf))
{
trigger_error('Could not save PDF to '. $pdf_save, E_USER_WARNING);
return;
}
return $pdf_save;
}
}