1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class PDFCrowdConverter extends Object |
5
|
|
|
{ |
6
|
|
|
private static $third_party_file = "pdfcrowd/thirdparty/pdfcrowd.php"; |
7
|
|
|
|
8
|
|
|
private static $username = ""; |
9
|
|
|
|
10
|
|
|
private static $api_key = ""; |
11
|
|
|
|
12
|
|
|
private static $singleton = null; |
13
|
|
|
|
14
|
|
|
private static $save_pdfs_here = 'pdfs'; //e.g. assets/pdfs |
15
|
|
|
|
16
|
|
|
public $pdf = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Allows access to the cart from anywhere in code. |
20
|
|
|
*@return ShoppingCart Object |
21
|
|
|
*/ |
22
|
|
|
public static function singleton() |
23
|
|
|
{ |
24
|
|
|
if (!self::$singleton) { |
25
|
|
|
self::$singleton = new PDFCrowdConverter(); |
26
|
|
|
} |
27
|
|
|
return self::$singleton; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
require_once(Director::baseFolder() . '/' .self::$third_party_file); |
33
|
|
|
$this->pdf = new Pdfcrowd(self::$username, self::$api_key); |
34
|
|
|
parent::__construct(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function ConvertPage($page) |
38
|
|
|
{ |
39
|
|
|
return $this->ConvertURL(Director::AbsoluteURL($page->Link), $page->URLSegment.".pdf", true); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
//CACHING DOES NOT WORK! |
43
|
|
|
public function ConvertURL($url, $filename, $useCacheIfAvailable = false) |
44
|
|
|
{ |
45
|
|
|
$folderFilename = ''; |
|
|
|
|
46
|
|
|
if (isset($_GET["flush"])) { |
47
|
|
|
$useCacheIfAvailable = false; |
48
|
|
|
} |
49
|
|
|
$folderFilename = $this->file2FolderFilename($filename); |
50
|
|
|
if ($folderFilename && $useCacheIfAvailable) { |
51
|
|
|
if (file_exists($folderFilename)) { |
52
|
|
|
$url = Director::absoluteBaseURL().$this->file2FolderFilename($filename, true); |
53
|
|
|
header("Location: $url"); |
54
|
|
|
exit(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
try { |
58
|
|
|
$pdf = $this->pdf->convertURI($url); |
59
|
|
|
} catch (PdfcrowdException $e) { |
60
|
|
|
return "Pdfcrowd Error: " . $e->getMessage(); |
61
|
|
|
} |
62
|
|
|
if ($folderFilename = $this->file2FolderFilename($filename)) { |
63
|
|
|
if (!$pdf) { |
64
|
|
|
$pdf = "error occured"; |
65
|
|
|
} |
66
|
|
|
$this->removeCachedPDF($filename); |
67
|
|
|
$fh = fopen($folderFilename, 'w'); |
68
|
|
|
fwrite($fh, $pdf); |
69
|
|
|
fclose($fh); |
70
|
|
|
} |
71
|
|
|
return $this->outputPDF($pdf, $filename); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function removeCachedPDF($filename) |
75
|
|
|
{ |
76
|
|
|
if ($folderFilename = $this->file2FolderFilename($filename)) { |
77
|
|
|
if (file_exists($folderFilename)) { |
78
|
|
|
unlink($folderFilename); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function file2FolderFilename($filename, $withoutBase = false) |
84
|
|
|
{ |
85
|
|
|
if (self::$save_pdfs_here) { |
86
|
|
|
$folder = Director::baseFolder()."/".self::$save_pdfs_here."/"; |
87
|
|
|
$folderObject = Folder::find_or_make($folder); |
88
|
|
|
if ($withoutBase) { |
89
|
|
|
$folderFilename = $folderObject->getRelativePath() . $filename; |
90
|
|
|
} else { |
91
|
|
|
$folderFilename = $folderObject->getFullPath() . $filename; |
92
|
|
|
} |
93
|
|
|
return $folderFilename; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
public static function outputPDF($pdfAsString, $filename) |
99
|
|
|
{ |
100
|
|
|
// set HTTP response headers |
101
|
|
|
header("Content-Type: application/pdf"); |
102
|
|
|
header("Cache-Control: no-cache"); |
103
|
|
|
header("Accept-Ranges: none"); |
104
|
|
|
header("Content-Disposition: attachment; filename=\"$filename\""); |
105
|
|
|
// send the generated PDF |
106
|
|
|
echo $pdfAsString; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.