Location: PHPKode > scripts > WPhp > wphp/WPhpDocument.php
<?php
/* 
$Id: WPhpDocument.php,v 1.4 2005/04/22 12:06:55 josheli Exp $
*/

include 'wphp_defines.php';

class WPhpDocument 
{

  var $file_name;
  var $_data;
  var $_genericHeader;

  function WPhpDocument($file_name = '') 
  {
    $this->_data = '';

    if(!empty($file_name))
    {
      $this->file_name = $file_name;
    }
  }

  function save($file_name = '') 
  {
    if(!empty($file_name))
    {
      $this->file_name = $file_name;
    }
    $file = $this->getHeaderData() . $this->getData();
    $fp = fopen($this->file_name,'wb');
    fwrite($fp, $file);
    fclose($fp);
  }

  function promptDownload($file_name = '') 
  {
    if(!empty($file_name))
    {
      $this->file_name = $file_name;
    }

    $file = $this->getHeaderData() . $this->getData();

    header("Cache-Control: public");
    header("Expires: 0");
    header("Content-Disposition: attachment; filename=".$this->file_name);
    header("Content-Type: application/download-now");

    echo $file; 
  }

  function getData() 
  {
    return $this->_data;
  }

  function _makeGenericHeader() 
  {
    $this->_genericHeader = BEGIN_HEADER . $this->_getFileLengthAsBinary() . END_HEADER;
  }

  function getHeaderData() 
  {
    if(empty($this->_genericHeader))
    {
      $this->_makeGenericHeader();
    }
    return $this->_genericHeader;
  }
  
  function writeText($text, $attribute = '') 
  {
    if(!empty($attribute))
    {
      $this->_data .= $this->attributeOn($attribute);
    }

    $this->_data .= $this->_convertText($text);
    
    if(!empty($attribute))
    {
      $this->_data .= $this->attributeOff($attribute);
    }
  }

  function insertPageBreak() 
  {
    $this->_data .= WP_HARD_EOP;
  }

  function centerPageVertically() 
  {
  	$this->_data .= VALIGN_CURRENT_PAGE;
  }

  function setPageJustification($justification = 0) 
  {
    $func = 'D3050C000001000$000C00D3';

    if(!in_array($justification, array(LEFT_JUSTIFY_PAGE, CENTER_JUSTIFY_PAGE, RIGHT_JUSTIFY_PAGE)))
    {
    	return false;
    }
    $this->_data .= hex2bin(str_replace('$', $justification, $func));
  }

  function _getFileLengthAsBinary()
  {
    $fileLen = dechex(strlen($this->getData())+HEADER_LENGTH);
    $low = substr($fileLen,-2,2);
    $high = str_replace($low,'',$fileLen);
    if(strlen($high)==1) $high = '0'.$high;
    return hex2bin($low.$high);
  }

  function _convertText($text) 
  {
    return preg_replace("/\n|\r\n|\r/", WP_HARD_EOL, str_replace(' ', WP_SOFT_SPACE, $text)); 
  }


  function attributeOn($attribute) 
  {
    if(!is_array($attribute))
    {
      $attribute = array($attribute);
    }
    foreach($attribute as $attr)
    {
      $this->_data .= WP_ATTRIBUTE_ON . $attr . WP_ATTRIBUTE_ON;
    }
  }
  function attributeOff($attribute) 
  {
    if(!is_array($attribute))
    {
      $attribute = array($attribute);
    }
    foreach($attribute as $attr)
    {
      $this->_data .= WP_ATTRIBUTE_OFF . $attr . WP_ATTRIBUTE_OFF;
    }
  }

  function startBlink() 
  {
    $this->attributeOn(WP_ATTRIBUTE_BLINK);
  }

  function endBlink() 
  {
    $this->attributeOff(WP_ATTRIBUTE_BLINK);
  }

  function startBold() 
  {
    $this->attributeOn(WP_ATTRIBUTE_BOLD);
  }

  function endBold() 
  {
    $this->attributeOff(WP_ATTRIBUTE_BOLD);
  }

  function startDoubleUnderline() 
  {
    $this->attributeOn(WP_ATTRIBUTE_DOUBLE_UNDERLINE);
  }

  function endDoubleUnderline() 
  {
    $this->attributeOff(WP_ATTRIBUTE_DOUBLE_UNDERLINE);
  }

  function startItalics() 
  {
    $this->attributeOn(WP_ATTRIBUTE_ITALICS);
  }

  function endItalics() 
  {
    $this->attributeOff(WP_ATTRIBUTE_ITALICS);
  }
  
  function startSubscript() 
  {
    $this->attributeOn(WP_ATTRIBUTE_SUBSCRIPT);
  }

  function endSubscript() 
  {
    $this->attributeOff(WP_ATTRIBUTE_SUBSCRIPT);
  }

  function startSuperscript() 
  {
    $this->attributeOn(WP_ATTRIBUTE_SUPERSCRIPT);
  }

  function endSuperscript() 
  {
    $this->attributeOff(WP_ATTRIBUTE_SUPERSCRIPT);
  }
  
  function startChapterHeading() 
  {
    $this->attributeOn(WP_ATTRIBUTE_CHAPTER_HEADING);
  }

  function endChapterHeading() 
  {
    $this->attributeOff(WP_ATTRIBUTE_CHAPTER_HEADING);
  }

  function startHeading() 
  {
    $this->attributeOn(WP_ATTRIBUTE_HEADING);
  }

  function endHeading() 
  {
    $this->attributeOff(WP_ATTRIBUTE_HEADING);
  }

  function startSubHeading() 
  {
    $this->attributeOn(WP_ATTRIBUTE_SUB_HEADING);
  }

  function endSubHeading() 
  {
    $this->attributeOff(WP_ATTRIBUTE_SUB_HEADING);
  }

  function startSmallPrint() 
  {
    $this->attributeOn(WP_ATTRIBUTE_SMALL_PRINT);
  }

  function endSmallPrint() 
  {
    $this->attributeOff(WP_ATTRIBUTE_SMALL_PRINT);
  }

  function startFinePrint() 
  {
    $this->attributeOn(WP_ATTRIBUTE_FINE_PRINT);
  }

  function endFinePrint() 
  {
    $this->attributeOff(WP_ATTRIBUTE_FINE_PRINT);
  }

  function startOutline() 
  {
    $this->attributeOn(WP_ATTRIBUTE_OUTLINE);
  }

  function endOutline() 
  {
    $this->attributeOff(WP_ATTRIBUTE_OUTLINE);
  }

  function startShadow() 
  {
    $this->attributeOn(WP_ATTRIBUTE_SHADOW);
  }

  function endShadow() 
  {
    $this->attributeOff(WP_ATTRIBUTE_SHADOW);
  }

  function startRedline() 
  {
    $this->attributeOn(WP_ATTRIBUTE_REDLINE);
  }

  function endRedline() 
  {
    $this->attributeOff(WP_ATTRIBUTE_REDLINE);
  }
  
  function startSmallCaps() 
  {
    $this->attributeOn(WP_ATTRIBUTE_SMALL_CAPS);
  }

  function endSmallCaps() 
  {
    $this->attributeOff(WP_ATTRIBUTE_SMALL_CAPS);
  }

  function startStrikeout() 
  {
    $this->attributeOn(WP_ATTRIBUTE_STRIKEOUT);
  }

  function endStrikeout() 
  {
    $this->attributeOff(WP_ATTRIBUTE_STRIKEOUT);
  }

  function startUnderline() 
  {
    $this->attributeOn(WP_ATTRIBUTE_UNDERLINE);
  }

  function endUnderline() 
  {
    $this->attributeOff(WP_ATTRIBUTE_UNDERLINE);
  }
}

?>
Return current item: WPhp