Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
GenController.php
1 <?php
2 /**
3  *
4  */
5 class GenController extends CController
6 {
7 
8  public function actionIndex($url, $key)
9  {
10  if ($this->module->encrypt($url) !== $key) {
11  throw new CHttpException(400, 'Wrong key!');
12  }
13 
14  if (!file_exists($this->pdfFileLocation($url))) {
15  if (!$this->module->allowExternalLinks) {
16  $finalUrl = Yii::app()->createAbsoluteUrl($url);
17  } else {
18  $finalUrl = $url;
19  }
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);
23  if ($cssFile) {
24  $pdfRenderer->setOptions(array('user-style-sheet' => $cssFile));
25  }
26  $pdfRenderer->addPage($finalUrl);
27  if (!$pdfRenderer->saveAs($this->pdfFileLocation($url))) {
28  throw new Exception($pdfRenderer->getError());
29  }
30  }
31  $this->outputToBrowser($this->pdfFileLocation($url));
32  }
33 
34  private function outputToBrowser($file)
35  {
36  Yii::app()->request->sendFile(basename($file), file_get_contents($file));
37 
38  //one way
39 // $this->redirect($this->module->pdfWebPath . '/' . $this->urlToFileName($url));
40 
41  //other way
42 // $pdf = file_get_contents($file);
43 //
44 // header('Content-Type: application/pdf');
45 // header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
46 // header('Pragma: public');
47 // header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
48 // header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
49 // header('Content-Length: ' . strlen($pdf));
50 // header('Content-Disposition: inline; filename="' . basename($file) . '";');
51 // ob_clean();
52 // flush();
53 // echo $pdf;
54 // die;
55  }
56 
57  private function pdfFileLocation($url)
58  {
59  return Yii::getPathOfAlias($this->module->pdfFolder) . '/' . $this->urlToFileName($url);
60  }
61 
62  private function urlToFileName($url)
63  {
64  return str_replace(array('/', ':'), '_', $url) . '.pdf';
65  }
66 
67 }