| Conditions | 12 |
| Paths | 145 |
| Total Lines | 83 |
| Code Lines | 40 |
| 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('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->owner)); |
||
| 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->owner)); |
||
| 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 | } |
||
| 171 |