-
Notifications
You must be signed in to change notification settings - Fork 1
/
createpdf.php
97 lines (77 loc) · 2.4 KB
/
createpdf.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
<?php
/***************************
Download and generate pdf
****************************/
/*if ( !defined( 'FPDM_PATH' ) ) {
define( 'FPDM_PATH', dirname(__FILE__).'/fpdm/' );
}
if ( !defined( 'FPDM_FILE_PATH' ) ) {
define('FPDM_FILE_PATH', dirname(__FILE__).'/uploads/fpdf/');
}
if ( !defined( 'PDF_FILE_PATH' ) ) {
define('PDF_FILE_PATH', dirname(__FILE__).'/uploads/docs/');
}*/
require(FPDM_PATH. 'fpdm.php');
// parse pdf fields
function parsePDFFields($file){
if(!$file){return false;}
$txt_file = file_get_contents($file);
$rows = explode("\n", $txt_file);
array_shift($rows);
$jsonArr = array();
foreach($rows as $row => $data)
{
//get row data
$row_data = explode(':', $data);
if(!strcmp($row_data[0], 'FieldName')){
array_push($jsonArr, trim($row_data[1]));
}
}
return (!empty($jsonArr)? $jsonArr : false);
}
// mapping with pdf fields and
function mappingFieldValues($pdf_f, $form_f){
if(empty($pdf_f) || empty($form_f)){
return false;
}
$arrFinal = array();
foreach ($pdf_f as $key1 => $fpdf) {
//$int = intval(preg_replace('/[^0-9]+/', '', $fpdf), 10);
preg_match_all('!\d+!', $fpdf, $matches);
$int = implode(' ', $matches[0]);
if(!$int){
$pdf_key = $fpdf;
}else{
$pdf_key = trim($fpdf, $int);
}
foreach ($form_f as $key2 => $form) {
if(!strcmp($pdf_key, $key2)){ // compare the fields of both pdf & form fields
$arrFinal[$fpdf] = $form;
}
}
}
return (!empty($arrFinal)? $arrFinal : false);
}
function generate_and_download_pdf($fields){
if(empty($fields)){return false;}
/*********** Fetch PDF Fields ***************/
$parseTxt = FPDM_FILE_PATH. $fields['form_slug']. '.txt';
if (!file_exists($parseTxt)) { // for pdf file fields
return false;
}
$pdfFields = parsePDFFields($parseTxt);
/*********** Mapping form fields ***************/
$fieldValues = mappingFieldValues($pdfFields, $fields);
/*********** Fill UP & Generate PDF ***************/
$templateUrl = FPDM_FILE_PATH. $fields['form_slug']. '.pdf';
$download = PDF_FILE_PATH. $fields['file_name'];
if (!file_exists($templateUrl)) { // for template file fields
return false;
}
$pdf = new FPDM($templateUrl);
$pdf->Load($fieldValues, false); // second parameter: false if field values are in ISO-8859-1, true if UTF-8
$pdf->Merge();
$pdf->Output('F', $download);
//exit;
}
?>