Conditions | 13 |
Paths | 289 |
Total Lines | 90 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
87 | public function generatePDF() |
||
88 | { |
||
89 | if (!$this->owner->data()->config()->get('pdf_export')) { |
||
90 | return false; |
||
91 | } |
||
92 | |||
93 | $binaryPath = $this->owner->data()->config()->get('wkhtmltopdf_binary'); |
||
94 | if ($binaryPath) { |
||
95 | Deprecation::notice('3.0', 'wkhtmltopdf_binary config is deprecated. '. |
||
96 | 'Use WKHTMLTOPDF_BINARY env var instead.'); |
||
97 | } |
||
98 | if (!$binaryPath || !is_executable($binaryPath)) { |
||
99 | if (Environment::getEnv('WKHTMLTOPDF_BINARY') |
||
100 | && is_executable(Environment::getEnv('WKHTMLTOPDF_BINARY')) |
||
101 | ) { |
||
102 | $binaryPath = Environment::getEnv('WKHTMLTOPDF_BINARY'); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | if (!$binaryPath) { |
||
107 | user_error( |
||
108 | 'Neither WKHTMLTOPDF_BINARY nor ' . get_class($this->owner->data()) . '.wkhtmltopdf_binary are defined', |
||
109 | E_USER_ERROR |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | if (Versioned::get_reading_mode() == 'Stage.Stage') { |
||
114 | user_error('Generating PDFs on draft is not supported', E_USER_ERROR); |
||
115 | } |
||
116 | |||
117 | set_time_limit(60); |
||
118 | |||
119 | // prepare the paths |
||
120 | $pdfFile = $this->owner->data()->getPdfFilename(); |
||
121 | $bodyFile = str_replace('.pdf', '_pdf.html', $pdfFile); |
||
122 | $footerFile = str_replace('.pdf', '_pdffooter.html', $pdfFile); |
||
123 | |||
124 | // make sure the work directory exists |
||
125 | if (!file_exists(dirname($pdfFile))) { |
||
126 | Filesystem::makeFolder(dirname($pdfFile)); |
||
127 | } |
||
128 | |||
129 | //decide the domain to use in generation |
||
130 | $pdfBaseUrl = $this->owner->getPDFBaseURL(); |
||
131 | |||
132 | // Force http protocol on CWP - fetching from localhost without using the proxy, SSL terminates on gateway. |
||
133 | if (Environment::getEnv('CWP_ENVIRONMENT')) { |
||
134 | Config::modify()->set(Director::class, 'alternate_protocol', 'http'); |
||
135 | //only set alternate protocol if CWP_SECURE_DOMAIN is defined OR pdf_base_url is |
||
136 | if ($pdfBaseUrl) { |
||
137 | Config::modify()->set(Director::class, 'alternate_base_url', 'http://' . $pdfBaseUrl); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | $bodyViewer = $this->owner->getViewer('pdf'); |
||
142 | |||
143 | // write the output of this page to HTML, ready for conversion to PDF |
||
144 | file_put_contents($bodyFile, $bodyViewer->process($this->owner)); |
||
145 | |||
146 | // get the viewer for the current template with _pdffooter |
||
147 | $footerViewer = $this->owner->getViewer('pdffooter'); |
||
148 | |||
149 | // write the output of the footer template to HTML, ready for conversion to PDF |
||
150 | file_put_contents($footerFile, $footerViewer->process($this->owner)); |
||
151 | |||
152 | //decide what the proxy should look like |
||
153 | $proxy = $this->owner->getPDFProxy($pdfBaseUrl); |
||
154 | |||
155 | // finally, generate the PDF |
||
156 | $command = $binaryPath . $proxy . ' --outline -B 40pt -L 20pt -R 20pt -T 20pt --encoding utf-8 ' |
||
157 | . '--orientation Portrait --disable-javascript --quiet --print-media-type '; |
||
158 | $retVal = 0; |
||
159 | $output = []; |
||
160 | exec( |
||
161 | $command . " --footer-html \"$footerFile\" \"$bodyFile\" \"$pdfFile\" &> /dev/stdout", |
||
162 | $output, |
||
163 | $retVal |
||
164 | ); |
||
165 | |||
166 | // remove temporary file |
||
167 | unlink($bodyFile); |
||
168 | unlink($footerFile); |
||
169 | |||
170 | // output any errors |
||
171 | if ($retVal != 0) { |
||
172 | user_error('wkhtmltopdf failed: ' . implode("\n", $output), E_USER_ERROR); |
||
173 | } |
||
174 | |||
175 | // serve the generated file |
||
176 | return HTTPRequest::send_file(file_get_contents($pdfFile), basename($pdfFile), 'application/pdf'); |
||
177 | } |
||
179 |