8 public function actionIndex($url, $key)
10 if ($this->module->encrypt($url) !== $key) {
11 throw new CHttpException(400,
'Wrong key!');
14 if (!file_exists($this->pdfFileLocation($url))) {
15 if (!$this->module->allowExternalLinks) {
16 $finalUrl = Yii::app()->createAbsoluteUrl($url);
20 $html = file_get_contents($finalUrl);
21 $pdfRenderer =
new WkHtmlToPdf(array(
'bin' => $this->module->wkhtmltopdfBin));
22 $cssFile = PdfHelper::grabCssToSingleFile($html, Yii::getPathOfAlias($this->module->assetsFolder), $this->module->cssExpirationTime);
24 $pdfRenderer->setOptions(array(
'user-style-sheet' => $cssFile));
26 $pdfRenderer->addPage($finalUrl);
27 if (!$pdfRenderer->saveAs($this->pdfFileLocation($url))) {
28 throw new Exception($pdfRenderer->getError());
31 $this->outputToBrowser($this->pdfFileLocation($url));
34 private function outputToBrowser($file)
36 Yii::app()->request->sendFile(basename($file), file_get_contents($file));
57 private function pdfFileLocation($url)
59 return Yii::getPathOfAlias($this->module->pdfFolder) .
'/' . $this->urlToFileName($url);
62 private function urlToFileName($url)
64 return str_replace(array(
'/',
':'),
'_', $url) .
'.pdf';