1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CWP\PDFExport\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\Filesystem; |
6
|
|
|
use SilverStripe\Control\Director; |
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
8
|
|
|
use SilverStripe\Control\HTTPResponse; |
9
|
|
|
use SilverStripe\Core\Config\Config; |
10
|
|
|
use SilverStripe\Core\Environment; |
11
|
|
|
use SilverStripe\Core\Extension; |
12
|
|
|
use SilverStripe\Versioned\Versioned; |
13
|
|
|
|
14
|
|
|
class PdfExportControllerExtension extends Extension |
15
|
|
|
{ |
16
|
|
|
private static $allowed_actions = [ |
|
|
|
|
17
|
|
|
'downloadpdf', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Serve the page rendered as PDF. |
22
|
|
|
* |
23
|
|
|
* @return HTTPResponse|false |
24
|
|
|
*/ |
25
|
|
|
public function downloadpdf() |
26
|
|
|
{ |
27
|
|
|
if (!$this->owner->data()->get('pdf_export')) { |
28
|
|
|
return false; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// We only allow producing live pdf. There is no way to secure the draft files. |
32
|
|
|
Versioned::set_stage(Versioned::LIVE); |
33
|
|
|
|
34
|
|
|
$path = $this->owner->data()->getPdfFilename(); |
35
|
|
|
if (!file_exists($path)) { |
36
|
|
|
$this->owner->generatePDF(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return HTTPRequest::send_file(file_get_contents($path), basename($path), 'application/pdf'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* This will return either pdf_base_url from YML, CWP_SECURE_DOMAIN from _ss_environment, or blank. In that |
44
|
|
|
* order of importance. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function getPDFBaseURL() |
49
|
|
|
{ |
50
|
|
|
// if base url YML is defined in YML, use that |
51
|
|
|
if ($this->owner->data()->get('pdf_base_url')) { |
52
|
|
|
$pdfBaseUrl = $this->owner->data()->get('pdf_base_url').'/'; |
53
|
|
|
// otherwise, if we are CWP use the secure domain |
54
|
|
|
} elseif (Environment::getEnv('CWP_SECURE_DOMAIN')) { |
55
|
|
|
$pdfBaseUrl = Environment::getEnv('CWP_SECURE_DOMAIN') . '/'; |
56
|
|
|
// or if neither, leave blank |
57
|
|
|
} else { |
58
|
|
|
$pdfBaseUrl = ''; |
59
|
|
|
} |
60
|
|
|
return $pdfBaseUrl; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Don't use the proxy if the pdf domain is the CWP secure domain or if we aren't on a CWP server |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getPDFProxy($pdfBaseUrl) |
69
|
|
|
{ |
70
|
|
|
if (!Environment::getEnv('CWP_SECURE_DOMAIN') |
71
|
|
|
|| $pdfBaseUrl == Environment::getEnv('CWP_SECURE_DOMAIN') . '/' |
72
|
|
|
) { |
73
|
|
|
$proxy = ''; |
74
|
|
|
} else { |
75
|
|
|
$proxy = ' --proxy ' . Environment::getEnv('SS_OUTBOUND_PROXY') |
76
|
|
|
. ':' . Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
77
|
|
|
} |
78
|
|
|
return $proxy; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Render the page as PDF using wkhtmltopdf. |
83
|
|
|
* |
84
|
|
|
* @return HTTPResponse|false |
85
|
|
|
*/ |
86
|
|
|
public function generatePDF() |
87
|
|
|
{ |
88
|
|
|
if (!$this->owner->data()->get('pdf_export')) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$binaryPath = $this->owner->data()->get('wkhtmltopdf_binary'); |
93
|
|
|
if (!$binaryPath || !is_executable($binaryPath)) { |
94
|
|
|
if (Environment::getEnv('WKHTMLTOPDF_BINARY') |
95
|
|
|
&& is_executable(Environment::getEnv('WKHTMLTOPDF_BINARY')) |
96
|
|
|
) { |
97
|
|
|
$binaryPath = Environment::getEnv('WKHTMLTOPDF_BINARY'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!$binaryPath) { |
102
|
|
|
user_error('Neither WKHTMLTOPDF_BINARY nor BasePage.wkhtmltopdf_binary are defined', E_USER_ERROR); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (Versioned::get_reading_mode() == 'Stage.Stage') { |
106
|
|
|
user_error('Generating PDFs on draft is not supported', E_USER_ERROR); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
set_time_limit(60); |
110
|
|
|
|
111
|
|
|
// prepare the paths |
112
|
|
|
$pdfFile = $this->owner->data()->getPdfFilename(); |
113
|
|
|
$bodyFile = str_replace('.pdf', '_pdf.html', $pdfFile); |
114
|
|
|
$footerFile = str_replace('.pdf', '_pdffooter.html', $pdfFile); |
115
|
|
|
|
116
|
|
|
// make sure the work directory exists |
117
|
|
|
if (!file_exists(dirname($pdfFile))) { |
118
|
|
|
Filesystem::makeFolder(dirname($pdfFile)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
//decide the domain to use in generation |
122
|
|
|
$pdfBaseUrl = $this->owner->getPDFBaseURL(); |
123
|
|
|
|
124
|
|
|
// Force http protocol on CWP - fetching from localhost without using the proxy, SSL terminates on gateway. |
125
|
|
|
if (Environment::getEnv('CWP_ENVIRONMENT')) { |
126
|
|
|
Config::modify()->set(Director::class, 'alternate_protocol', 'http'); |
127
|
|
|
//only set alternate protocol if CWP_SECURE_DOMAIN is defined OR pdf_base_url is |
128
|
|
|
if ($pdfBaseUrl) { |
129
|
|
|
Config::modify()->set(Director::class, 'alternate_base_url', 'http://' . $pdfBaseUrl); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$bodyViewer = $this->owner->getViewer('pdf'); |
134
|
|
|
|
135
|
|
|
// write the output of this page to HTML, ready for conversion to PDF |
136
|
|
|
file_put_contents($bodyFile, $bodyViewer->process($this)); |
137
|
|
|
|
138
|
|
|
// get the viewer for the current template with _pdffooter |
139
|
|
|
$footerViewer = $this->owner->getViewer('pdffooter'); |
140
|
|
|
|
141
|
|
|
// write the output of the footer template to HTML, ready for conversion to PDF |
142
|
|
|
file_put_contents($footerFile, $footerViewer->process($this)); |
143
|
|
|
|
144
|
|
|
//decide what the proxy should look like |
145
|
|
|
$proxy = $this->owner->getPDFProxy($pdfBaseUrl); |
146
|
|
|
|
147
|
|
|
// finally, generate the PDF |
148
|
|
|
$command = $binaryPath . $proxy . ' --outline -B 40pt -L 20pt -R 20pt -T 20pt --encoding utf-8 ' |
149
|
|
|
. '--orientation Portrait --disable-javascript --quiet --print-media-type '; |
150
|
|
|
$retVal = 0; |
151
|
|
|
$output = []; |
152
|
|
|
exec( |
153
|
|
|
$command . " --footer-html \"$footerFile\" \"$bodyFile\" \"$pdfFile\" &> /dev/stdout", |
154
|
|
|
$output, |
155
|
|
|
$retVal |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
// remove temporary file |
159
|
|
|
unlink($bodyFile); |
160
|
|
|
unlink($footerFile); |
161
|
|
|
|
162
|
|
|
// output any errors |
163
|
|
|
if ($retVal != 0) { |
|
|
|
|
164
|
|
|
user_error('wkhtmltopdf failed: ' . implode("\n", $output), E_USER_ERROR); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// serve the generated file |
168
|
|
|
return HTTPRequest::send_file(file_get_contents($pdfFile), basename($pdfFile), 'application/pdf'); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|