Conditions | 12 |
Paths | 145 |
Total Lines | 86 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
86 | public function generatePDF() |
||
87 | { |
||
88 | if (!$this->owner->data()->config()->get('pdf_export')) { |
||
89 | return false; |
||
90 | } |
||
91 | |||
92 | $binaryPath = $this->owner->data()->config()->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( |
||
103 | 'Neither WKHTMLTOPDF_BINARY nor ' . get_class($this->owner->data()) . '.wkhtmltopdf_binary are defined', |
||
104 | E_USER_ERROR |
||
105 | ); |
||
106 | } |
||
107 | |||
108 | if (Versioned::get_reading_mode() == 'Stage.Stage') { |
||
109 | user_error('Generating PDFs on draft is not supported', E_USER_ERROR); |
||
110 | } |
||
111 | |||
112 | set_time_limit(60); |
||
113 | |||
114 | // prepare the paths |
||
115 | $pdfFile = $this->owner->data()->getPdfFilename(); |
||
116 | $bodyFile = str_replace('.pdf', '_pdf.html', $pdfFile); |
||
117 | $footerFile = str_replace('.pdf', '_pdffooter.html', $pdfFile); |
||
118 | |||
119 | // make sure the work directory exists |
||
120 | if (!file_exists(dirname($pdfFile))) { |
||
121 | Filesystem::makeFolder(dirname($pdfFile)); |
||
122 | } |
||
123 | |||
124 | //decide the domain to use in generation |
||
125 | $pdfBaseUrl = $this->owner->getPDFBaseURL(); |
||
126 | |||
127 | // Force http protocol on CWP - fetching from localhost without using the proxy, SSL terminates on gateway. |
||
128 | if (Environment::getEnv('CWP_ENVIRONMENT')) { |
||
129 | Config::modify()->set(Director::class, 'alternate_protocol', 'http'); |
||
130 | //only set alternate protocol if CWP_SECURE_DOMAIN is defined OR pdf_base_url is |
||
131 | if ($pdfBaseUrl) { |
||
132 | Config::modify()->set(Director::class, 'alternate_base_url', 'http://' . $pdfBaseUrl); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | $bodyViewer = $this->owner->getViewer('pdf'); |
||
137 | |||
138 | // write the output of this page to HTML, ready for conversion to PDF |
||
139 | file_put_contents($bodyFile, $bodyViewer->process($this->owner)); |
||
140 | |||
141 | // get the viewer for the current template with _pdffooter |
||
142 | $footerViewer = $this->owner->getViewer('pdffooter'); |
||
143 | |||
144 | // write the output of the footer template to HTML, ready for conversion to PDF |
||
145 | file_put_contents($footerFile, $footerViewer->process($this->owner)); |
||
146 | |||
147 | //decide what the proxy should look like |
||
148 | $proxy = $this->owner->getPDFProxy($pdfBaseUrl); |
||
149 | |||
150 | // finally, generate the PDF |
||
151 | $command = $binaryPath . $proxy . ' --outline -B 40pt -L 20pt -R 20pt -T 20pt --encoding utf-8 ' |
||
152 | . '--orientation Portrait --disable-javascript --quiet --print-media-type '; |
||
153 | $retVal = 0; |
||
154 | $output = []; |
||
155 | exec( |
||
156 | $command . " --footer-html \"$footerFile\" \"$bodyFile\" \"$pdfFile\" &> /dev/stdout", |
||
157 | $output, |
||
158 | $retVal |
||
159 | ); |
||
160 | |||
161 | // remove temporary file |
||
162 | unlink($bodyFile); |
||
163 | unlink($footerFile); |
||
164 | |||
165 | // output any errors |
||
166 | if ($retVal != 0) { |
||
167 | user_error('wkhtmltopdf failed: ' . implode("\n", $output), E_USER_ERROR); |
||
168 | } |
||
169 | |||
170 | // serve the generated file |
||
171 | return HTTPRequest::send_file(file_get_contents($pdfFile), basename($pdfFile), 'application/pdf'); |
||
172 | } |
||
174 |