ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilHTMLToPDFTransformerUsingWebkit.php
Go to the documentation of this file.
1 <?php
2  /*
3  +----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +----------------------------------------------------------------------------+
22 */
23 
24 include_once "./Services/PDF/classes/class.ilHTMLToPDFTransformer.php";
25 
34 {
35  private $wkhtmltopdf;
36  private $setting;
37 
38  function __construct($filename = "")
39  {
41  $this->setting = new ilSetting("pdf");
42  $this->wkhtmltopdf = $this->setting->get("path_to_wkhtml");
43  }
44 
45  private function getArgs()
46  {
47  $args = array();
48  $singleargs = array();
49  foreach ($this->setting->getAll() as $arg => $value)
50  {
51  if (strpos($arg, 'args_') === 0)
52  {
53  if (strlen($value))
54  {
55  if (strpos($value, '--') === 0)
56  {
57  array_push($singleargs, $value);
58  }
59  else
60  {
61  $args['--'.preg_replace('/_/', '-', substr($arg, 5))] = $value;
62  }
63  }
64  }
65  }
66  $args_string = '';
67  foreach ($args as $key => $value)
68  {
69  $args_string .= $key . ' ' . '"' . $value . '"' . ' ';
70  }
71  if (count($singleargs)) $args_string .= join($singleargs, ' ');
72 // global $ilLog;
73 // $ilLog->write($args_string);
74  return $args_string;
75  }
76 
77  public function deliverPDFFromHTMLString($a_string)
78  {
79  include_once "./Services/Utilities/classes/class.ilUtil.php";
80  $cover_file = ilUtil::ilTempnam() . ".html";
81  $pdf_file = ilUtil::ilTempnam() . ".pdf";
82  $html_file = ilUtil::ilTempnam() . ".html";
83  $fp = fopen($html_file, "w"); fwrite($fp, $a_string); fclose($fp);
84 
85 /* $cover = new ilTemplate("tpl.print_cover.html", TRUE, TRUE, "Services/PDF");
86  $cover->setVariable("IMAGE", ilUtil::getImagePath('ilias_logo_big.png'));
87  $cover->setVariable("IMAGE_ALT", 'ILIAS Logo');
88  $html = str_replace("src=\"./", "src=\"" . ILIAS_HTTP_PATH . "/", $cover->get());
89  $fp = fopen($cover_file, "w"); fwrite($fp, $html); fclose($fp);*/
90 
91 // $args = $this->getArgs() . " cover $cover_file $html_file $pdf_file";
92  $args = $this->getArgs() . " $html_file $pdf_file";
93 
94  ilUtil::execQuoted($this->wkhtmltopdf, $args);
95  ilUtil::deliverFile($pdf_file, $this->filename);
96  }
97 
98  public function createPDFFileFromHTMLString($a_string, $a_target)
99  {
100  include_once "./Services/Utilities/classes/class.ilUtil.php";
101  $cover_file = ilUtil::ilTempnam() . ".html";
102  $pdf_file = ilUtil::ilTempnam() . ".pdf";
103  $html_file = ilUtil::ilTempnam() . ".html";
104  $fp = fopen($html_file, "w"); fwrite($fp, $a_string); fclose($fp);
105 
106 /* $cover = new ilTemplate("tpl.print_cover.html", TRUE, TRUE, "Services/PDF");
107  $cover->setVariable("IMAGE", ilUtil::getImagePath('ilias_logo_big.png'));
108  $cover->setVariable("IMAGE_ALT", 'ILIAS Logo');
109  $html = str_replace("src=\"./", "src=\"" . ILIAS_HTTP_PATH . "/", $cover->get());
110  $fp = fopen($cover_file, "w"); fwrite($fp, $html); fclose($fp);*/
111 
112 // $args = $this->getArgs() . " cover $cover_file $html_file $pdf_file";
113  $args = $this->getArgs() . " $html_file $pdf_file";
114 
115  ilUtil::execQuoted($this->wkhtmltopdf, $args);
116  if (!rename($pdf_file, $a_target))
117  {
118  $ilLog->write("Could not rename $pdf_file to $a_target\n");
119  // TODO complain; unlink temp file? check beforehand whether target exists?
120  }
121  }
122 
123  public function deliverPDFFromHTMLFile($html_file)
124  {
125  include_once "./Services/Utilities/classes/class.ilUtil.php";
126  $pdf_file = ilUtil::ilTempnam() . ".pdf";
127  $args = $this->getArgs() . " $html_file $pdf_file";
128  ilUtil::execQuoted($this->wkhtmltopdf, $args);
129  ilUtil::deliverFile($pdf_file, $this->filename);
130  }
131 
132  public function createPDFFileFromHTMLFile($a_source, $a_target)
133  {
134  global $ilLog;
135 
136  include_once "./Services/Utilities/classes/class.ilUtil.php";
137  $temp_file = ilUtil::ilTempnam() . ".pdf";
138  $args = $this->getArgs() . " $a_source $temp_file";
139  ilUtil::execQuoted($this->wkhtmltopdf, $args);
140  if (!rename($temp_file, $a_target))
141  {
142  $ilLog->write("Could not rename $temp_file to $a_target\n");
143  // TODO complain; unlink temp file? check beforehand whether target exists?
144  }
145  }
146 }
147 
148 ?>