ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilHTMLToPDFTransformerUsingFOP.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  public function createPDFFileFromHTMLFile($a_source, $a_target)
36  {
37  $input = file_get_contents($a_source);
38  $this->createPDFFileFromFO($this->processHTML2FO($input), $a_target);
39  }
40 
41  public function createPDFFileFromHTMLString($a_string, $a_target)
42  {
43  $this->createPDFFileFromFO($this->processHTML2FO($a_string), $a_target);
44  }
45 
46  public function deliverPDFFromHTMLString($a_string)
47  {
48  $this->deliverPDFFromFO($this->processHTML2FO($a_string));
49  }
50 
51  public function deliverPDFFromHTMLFile($a_path)
52  {
53  if (@file_exists($a_path))
54  {
55  $this->deliverPDFFromHTMLString(file_get_contents($a_path));
56  }
57  }
58 
66  protected function processHTML2FO($print_output)
67  {
68  if (!@file_exists($this->xsl)) return "";
69  if (extension_loaded("tidy"))
70  {
71  $config = array(
72  "indent" => false,
73  "output-xml" => true,
74  "numeric-entities" => true
75  );
76  $tidy = new tidy();
77  $tidy->parseString($print_output, $config, 'utf8');
78  $tidy->cleanRepair();
79  $print_output = tidy_get_output($tidy);
80  $print_output = preg_replace("/^.*?(<html)/", "\\1", $print_output);
81  }
82  $xsl = file_get_contents($this->xsl);
83  $args = array( '/_xml' => $print_output, '/_xsl' => $xsl );
84  $xh = xslt_create();
85  $params = array();
86  $output = xslt_process($xh, "arg:/_xml", "arg:/_xsl", NULL, $args, $params);
87  xslt_error($xh);
88  xslt_free($xh);
89  return $output;
90  }
91 
98  protected function deliverPDFFromFO($fo)
99  {
100  global $ilLog;
101 
102  include_once "./Services/Utilities/classes/class.ilUtil.php";
103  $fo_file = ilUtil::ilTempnam() . ".fo";
104  $fp = fopen($fo_file, "w"); fwrite($fp, $fo); fclose($fp);
105  include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php';
106  try
107  {
108  $pdf_base64 = ilRpcClientFactory::factory('RPCTransformationHandler')->ilFO2PDF($fo);
109  ilUtil::deliverData($pdf_base64->scalar, $this->filename, "application/pdf", false, true);
110  return true;
111  }
112  catch(XML_RPC2_FaultException $e)
113  {
114  $ilLog->write(__METHOD__.': '.$e->getMessage());
115  return false;
116  }
117  catch(Exception $e)
118  {
119  $ilLog->write(__METHOD__.': '.$e->getMessage());
120  return false;
121  }
122  }
123 
124  protected function createPDFFileFromFO($fo, $target)
125  {
126  global $ilLog;
127 
128  include_once "./Services/Utilities/classes/class.ilUtil.php";
129  $fo_file = ilUtil::ilTempnam() . ".fo";
130  $fp = fopen($fo_file, "w"); fwrite($fp, $fo); fclose($fp);
131  include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php';
132  try
133  {
134  $pdf_base64 = ilRpcClientFactory::factory('RPCTransformationHandler')->ilFO2PDF($fo);
135  //$pdf_binary = base64_decode($pdf_base64);
136  //file_put_contents($target, $pdf_binary);
137  file_put_contents($target, $pdf_base64->scalar);
138  return true;
139  }
140  catch(XML_RPC2_FaultException $e)
141  {
142  $ilLog->write(__METHOD__.': '.$e->getMessage());
143  return false;
144  }
145  catch(Exception $e)
146  {
147  $ilLog->write(__METHOD__.': '.$e->getMessage());
148  return false;
149  }
150  // TODO: catch possible exceptions caused by file_put_contents failing
151  }
152 }
153 
154 ?>