<?php
/**
* @package PhpOpenDocumentReports
* @author Eric Letard
* @copyright GPL License 2009
* @license http://www.gnu.org/copyleft/gpl.html GPL License
* @version 0.3
*/
include_once("http_class.php");
/**
* class PODRJODConverter.
*
*
* Bridge between PODR and OpenOffice via JodConverter webservice.
*
* To enable conversion, you need to have a jod_podr webservice.
* If you have large tables ( > 300 lines), OOo will take too much time, you need to cut your tables to have better performances (please see the test_big_tables example)
*
*
* @package PhpOpenDocumentReports
* @author Eric Letard
* @copyright GPL License 2009
* @license http://www.gnu.org/copyleft/gpl.html GPL License
* @version 0.3
*/
class PODRJOD {
/**
* Known available conversions options, need to be completed
* input => array of output formats allowed
* @var array
*/
public static $allowed_conversions=array(
"odt"=>array("pdf","rtf","html","doc"),
"doc"=>array("pdf","rtf","html","odt"),
"rtf"=>array("pdf","odt","html","doc"),
"html"=>array("pdf","rtf","odt","doc"),
"docx"=>array("pdf","rtf","odt","doc","html"),
);
/**
* Connects to JOD and returns filename.
* @param PODRFileString $data
* @return PODRFileString result
*/
public static function convertFile(PODRFileString $data) {
if($data->getContext()->getConfig("EnableConversion", true))
{
$filename=self::convertFileInternal($data->getContext()->getConfig("InputFileName", ""),
$data->getFileName(),
$data->getContext()->getConfig("OutputFileName", ""),
$data->getContext()->getConfig("TempDir", null));
$data->setFilename($filename);
}
return $data;
}
/**
* This method can be used independantly of all PODR.
* @param string $inputfilename
* @param string $tempfilename
* @param string $outputfilename
* @return string converted filename
*/
public static function convertFileInternal($inputfilename,$tempfilename,$outputfilename,$tempdir=null)
{
$ext=PODREngine::getExtension($outputfilename);
$ext_temp=PODREngine::getExtension($tempfilename);
$ext_input=PODREngine::getExtension($inputfilename);
$filename=$tempfilename;
if($ext!==$ext_input) {
//force valid extension for OOo
if($ext_temp!==$ext_input) {
$filename=$filename.".".$ext_input;
rename($tempfilename,$filename);
@unlink($tempfilename);
}
if(!isset(self::$allowed_conversions[$ext_input])) {
throw new Exception("Unsupported Conversion Format : $ext_input");
}
elseif(!in_array($ext,self::$allowed_conversions[$ext_input])) {
throw new Exception("Unsupported Output Conversion Format : $ext");
}
else
$filename=self::convert($filename,$ext,$tempdir);
}
return $filename;
}
/**
* Connection to JOD
* Be carefull with filename use slashes in your paths
* @param <string> $filenameIn
* @param <string> $ext
* @param <string> $fileFamilly
* @return string output file
* @throws OdfConvertException
*/
protected static function convert($filenameIn,$ext,$tempdir) {
$fileOut=tempnam($tempdir, md5(uniqid()));
$out= $fileOut.".$ext";
//prepare the http request
$http=new http_class;
$http->timeout=0;
$http->data_timeout=0;
$http->debug=0;
$http->html_debug=1;
$url="http://127.0.0.1:7890/converter/document.$ext";
$error=$http->GetRequestArguments($url,$arguments);
$arguments["RequestMethod"]="POST";
$arguments["PostValues"]=array(
"MAX_FILE_SIZE"=>"10000"
);
$arguments["PostFiles"]=array(
"toconvertfile"=>array(
"Data"=>file_get_contents($filenameIn),
"Name"=>basename($filenameIn),
),
);
try {
$data=self::sendRequest($http,$arguments);
file_put_contents($out, $data);
}catch(Exception $e) {
@unlink($filenameIn);
@unlink($fileOut);
throw $e;
}
@unlink($filenameIn);
@unlink($fileOut);
return $out;
}
private static function sendRequest($http,$arguments) {
$error=$http->Open($arguments);
$bodyFull="";
$headers=array();
if($error=="") {
$error=$http->SendRequest($arguments);
if($error=="") {
$error=$http->ReadReplyHeaders($headers);
if($error=="") {
for(;;) {
$error=$http->ReadReplyBody($body,512);
if($error!=""|| strlen($body)==0)
break;
$bodyFull.=$body;
}
}
}
}
/*Verification des Erreurs*/
if(isset($headers['exception'])) {
throw new Exception($headers['exception']);
}
$http->Close();
return $bodyFull;
}
}
?>