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