-
Notifications
You must be signed in to change notification settings - Fork 1
/
MergePDF.php
153 lines (131 loc) · 4.46 KB
/
MergePDF.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
<?php
/**
* Created by PhpStorm.
* User: Kumar Shubham
* Date: 16/05/17
* Time: 7:23 PM
*/
require_once (dirname(__FILE__).'/fpdf/fpdf.php');
require_once (dirname(__FILE__).'/fpdi/fpdi.php');
class MergePDF
{
// The Remote files
private $files = [];
private $attachmentFolder = '/assets/';
private $outputFile = 'merged_pdf.pdf';
/**
* MergePDF constructor.
* The configuration array properties the required input.
* @param array $config
* @throws Exception
*/
function __construct(array $config = [])
{
// Setting the configuration.
foreach ($config as $property =>$value) {
if (property_exists('MergePDF',$property)){
$this->{$property} = $value;
}
}
//Reads the input
if (isset($config['inputFile']))
$this->_readInput($config['inputFile']);
if (sizeof($this->files) == 0){
throw new Exception("No files provided.");
}
// Checks if the asset folder exists. If not, creates one.
// Note: The user must have priviledge to create folder in the directory.
if (!file_exists(__DIR__.$this->attachmentFolder)) {
mkdir($this->attachmentFolder, 0777, true);
}
}
/**
* Reads the input file which contains the URL
* @param $file
* @throws Exception
*/
public function _readInput($file){
if (!file_exists(__DIR__.'/'.$file)){
throw new Exception("Invalid Input File specified");
}
$handle = fopen(__DIR__.'/'.$file, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// echo $line."\n";
array_push($this->files, trim($line));
}
fclose($handle);
} else {
throw new Exception("Unable to read Input specified");
}
// echo sizeof($this->files);
// die();
}
// Remotely fetches all the files through curl and stores in the attachmentFolder.
/**
* @return array [The files downloaded successfully]
*/
private function _fetchFiles(){
echo "Starting PDF fetching at: ".date("Y-m-d H:i:s")."\n";
$startTime = microtime(true);
$readyFiles = [];
$i=0;
foreach ($this->files as $fileUrl){
if ($this->_downloadFile($fileUrl, $this->attachmentFolder.'_file'.$i.'.pdf')){
$readyFiles[$i] = $this->attachmentFolder.'_file'.$i.'.pdf';
$i = $i + 1;
}
}
$endTime = microtime(true);
echo "PDF Fetch completed at: ".date("Y-m-d H:i:s")."\n";
echo $endTime - $startTime ."ms to fetch ". $i ." files.\n";
return $readyFiles;
}
/**
* Makes a curl call to fetch the file.
* @param $fileUrl - The remote URL
* @param $location - The path where stored.
* @return mixed - The status of CURL request.
*/
private function _downloadFile($fileUrl, $location){
set_time_limit(0);
$fp = fopen (dirname(__FILE__) . $location, 'w+'); // Output file
$ch = curl_init($fileUrl); // Input file
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$fileStatus = curl_exec($ch);
curl_close($ch);
fclose($fp);
return $fileStatus;
}
/**
* The merge function.
*
* @param $files [The input file list which is to be merged]
*/
private function _mergeFiles($files){
echo "Starting PDF merging at: ".date("Y-m-d H:i:s")."\n";
$startTime = microtime(true);
$pdf = new FPDI();
foreach ($files as $file){
$count = $pdf->setSourceFile(dirname(__FILE__).$file);
for ($i = 0; $i < $count; $i++) {
$tpl = $pdf->importPage($i + 1, '/MediaBox'); //Imports each page as a screen.
$pdf->addPage();
$pdf->useTemplate($tpl);
}
}
// Create an output file.
$pdf->Output('F',$this->outputFile);
$endTime = microtime(true);
echo "Completed PDF merging at: ".date("Y-m-d H:i:s")."\n";
echo $endTime - $startTime ."ms to fetch and merge ". sizeof($files) ." pdfs.\n";
}
public function execute(){
$files = $this->_fetchFiles();
$this->_mergeFiles($files);
}
}
$t = new MergePDF(['inputFile' => 'inputURL']);
$t->execute();