Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
PdfHelper.php
1 <?php
2 /**
3  *
4  */
5 class PdfHelper
6 {
7 
8  public static function grabCssToSingleFile($html, $folder, $expirationTime = 3600)
9  {
10  $css = self::findCss($html);
11  if (!empty($css)) {
12  $cssContentFile = $folder . '/' . md5(serialize($css)) . '.css';
13  if (!file_exists($cssContentFile) || time() - filemtime($cssContentFile) > $expirationTime) {
14  $cssContent = '';
15  foreach ($css as $cssUrl) {
16  $content = self::downloadCss($cssUrl);
17  $cssContent .= $content;
18  }
19  file_put_contents($cssContentFile, $cssContent);
20  }
21  return $cssContentFile;
22  } else {
23  return false;
24  }
25  }
26 
27  public static function findCss($html)
28  {
29  $css = array();
30  preg_match_all('/(<link.*\/>)/', $html, $links);
31  if (!empty($links[0])) {
32  foreach ($links[0] as $l) {
33  preg_match_all('/\s+(?<name>.*?)=(?<value>".*?")/', $l, $rawAttributes);
34  $attributes = array();
35  for ($i = 0; $i < count($rawAttributes['name']); $i++) {
36  $attributes[$rawAttributes['name'][$i]] = trim($rawAttributes['value'][$i], ' "');
37  }
38  if (isset($attributes['rel'], $attributes['href']) && $attributes['rel'] == 'stylesheet') {
39  $css[] = $attributes['href'];
40  }
41  }
42  }
43  return $css;
44  }
45 
46  private static function downloadCss($file)
47  {
48  if (!preg_match('/^(http|https):/', $file)) {
49  $file = Yii::app()->createAbsoluteUrl($file);
50  }
51  return file_get_contents($file);
52  }
53 
54 }