|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CWP\PDFExport\Extensions; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Director; |
|
6
|
|
|
use SilverStripe\ORM\DataExtension; |
|
7
|
|
|
use SilverStripe\Versioned\Versioned; |
|
8
|
|
|
|
|
9
|
|
|
class PdfExportExtension extends DataExtension |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @config |
|
13
|
|
|
* @var bool |
|
14
|
|
|
*/ |
|
15
|
|
|
private static $pdf_export = false; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Domain to generate PDF's from, DOES not include protocol |
|
19
|
|
|
* i.e. google.com not http://google.com |
|
20
|
|
|
* @config |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private static $pdf_base_url = ''; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Allow custom overriding of the path to the WKHTMLTOPDF binary, in cases |
|
27
|
|
|
* where multiple versions of the binary are available to choose from. This |
|
28
|
|
|
* should be the full path to the binary (e.g. /usr/local/bin/wkhtmltopdf) |
|
29
|
|
|
* @see BasePage_Controller::generatePDF(); |
|
30
|
|
|
* |
|
31
|
|
|
* @config |
|
32
|
|
|
* @var string|null |
|
33
|
|
|
*/ |
|
34
|
|
|
private static $wkhtmltopdf_binary = null; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Where to store generated PDF files |
|
38
|
|
|
* |
|
39
|
|
|
* @config |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private static $generated_pdf_path = 'assets/_generated_pdfs'; |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Return the full filename of the pdf file, including path & extension |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getPdfFilename() |
|
48
|
|
|
{ |
|
49
|
|
|
$baseName = sprintf('%s-%s', $this->owner->URLSegment, $this->owner->ID); |
|
50
|
|
|
|
|
51
|
|
|
$folderPath = $this->owner->config()->get('generated_pdf_path'); |
|
52
|
|
|
if ($folderPath[0] != '/') { |
|
53
|
|
|
$folderPath = BASE_PATH . '/' . $folderPath; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return sprintf('%s/%s.pdf', $folderPath, $baseName); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Build pdf link for template. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function PdfLink() |
|
63
|
|
|
{ |
|
64
|
|
|
if (!$this->owner->config()->get('pdf_export')) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$path = $this->getPdfFilename(); |
|
69
|
|
|
|
|
70
|
|
|
if ((Versioned::get_stage() === Versioned::LIVE) && file_exists($path)) { |
|
71
|
|
|
return Director::baseURL() . preg_replace('#^/#', '', Director::makeRelative($path)); |
|
72
|
|
|
} |
|
73
|
|
|
return $this->owner->Link('downloadpdf'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Remove linked pdf when publishing the page, as it would be out of date. |
|
78
|
|
|
*/ |
|
79
|
|
|
public function onAfterPublish() |
|
80
|
|
|
{ |
|
81
|
|
|
$filepath = $this->getPdfFilename(); |
|
82
|
|
|
if (file_exists($filepath)) { |
|
83
|
|
|
unlink($filepath); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Remove linked pdf when unpublishing the page, so it's no longer valid. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function onAfterUnpublish() |
|
91
|
|
|
{ |
|
92
|
|
|
$filepath = $this->getPdfFilename(); |
|
93
|
|
|
if (file_exists($filepath)) { |
|
94
|
|
|
unlink($filepath); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|