We had a requirement to fill an rtf template file with some info from the system, the templates had some pictures that needed to be retrieved from the system and placed in the rtf file.
The deadline for this requirement was very short, and the system where this was implemented was a large crm system so we didn’t had much time to dig up into the code to look for any useful class, so what we did was very simple.
Whenever a new document would be created, we get the template related to that client opened the rtf file put it on a string and do a string replace for each field to be replaced, this is the code for this:
$this->keyVaribles = array (
'<organization_name>' => $account->name,
'<company_logos>' => $img->getContent(),
'<dates_of_event>' => $account->date,
'<account_link>' => $account->link,
'<calendar>' => $account->cal,
'<signature>' => $account->signature,
);
// read file
$file = $bean->document_revision_id;
$handle = fopen ($file, 'r');
$content = fread($handle, filesize($file));
// replace content
foreach($this->keyVaribles as $from=> $to) {
$content = str_replace($from, $to, $content);
}
// save file
file_put_contents($file, $content);
fclose($handle);
This worked for all the text fields, but there was one field
/**
*
* This is the short Description for the Class
*
* This is the long description for the Class
*
* @author Ivan Villareal
* @version $Id$
* @package Package
* @subpackage SubPackage
*
*/
class image
{
private $_cmInPoints = 0.026434;
private $_twipsInCm = 567;
private $_imgFile;
private $_width;
private $_height;
private $_defaultWidth;
private $_defaultHeight;
public function __construct($fileName, $width = 0, $height = 0)
{
$this->_imgFile = $fileName;
$this->_width = $width;
$this->_height = $height;
$imgSize = getimagesize($fileName);
$this->_defaultWidth = $imgSize[0];
$this->_defaultHeight = $imgSize[1];
}
private function _getWidth()
{
if (!empty($this->_width)) {
$width = $this->_width;
} else if (!empty($this->_height)) {
$width = ($this->_defaultWidth / $this->_defaultHeight) * $this->_height;
} else {
$width = $this->_defaultWidth * $this->_cmInPoints;
}
return round($width * $this->_twipsInCm);
}
private function _getHeight()
{
if (!empty($this->_height)) {
$height = $this->_height;
} else if (!empty($this->_height)) {
$height = ($this->_defaultHeight / $this->_defaultWidth) * $this->_width;
} else {
$height = $this->_defaultHeight * $this->_cmInPoints;
}
return round($height * $this->_twipsInCm);
}
private function _fileToHex()
{
$f = fopen($this->_imgFile, "r");
$string = fread($f, filesize($this->_imgFile));
fclose($f);
$str = '';
for ($i = 0; $i < strlen($string); $i ++) {
$hex = dechex( ord($string{$i}));
if (strlen($hex) == 1) {
$hex = '0'.$hex;
}
$str .= $hex;
}
return $str;
}
public function getContent()
{
$content = '{\pict';
$content .= '\picwgoal'.$this->_getWidth();
$content .= '\pichgoal'.$this->_getHeight();
$content .= $this->_fileToHex();
$content .= '}';
return $content;
}
}
As you can see this class is very simple and you only have to instantiate it with the image file to be converted, and call the getContents
method to get the rtf code for that image.
This worked pretty well so far, but more testing is needed before deploying this.