function formatForPDFandHTML($pString)
{
    $style = '';
    $found = true;
    $color = array();
    $bgcolor = array();
    $html = '';
    $bold = '';
    $italic = '';
    $underline = '';
    while ($found) {
        $found = false;
        if (substr($pString, 0, 6) == '#BOLD#') {
            $bold = '1';
            $found = true;
            $pString = substr($pString, 6);
            if (strpos($style, 'B') === false) {
                $style .= 'B';
            }
        }
        if (substr($pString, 0, 8) == '#ITALIC#') {
            $italic = '1';
            $found = true;
            $pString = substr($pString, 8);
            if (strpos($style, 'I') === false) {
                $style .= 'I';
            }
        }
        if (substr($pString, 0, 11) == '#UNDERLINE#') {
            $underline = '1';
            $found = true;
            $pString = substr($pString, 11);
            if (strpos($style, 'U') === false) {
                $style .= 'U';
            }
        }
        if (substr($pString, 0, 7) == '#COLOR#') {
            $html .= ';color:#' . substr($pString, 7, 6);
            $found = true;
            $color = rgbcolor(substr($pString, 7, 6), array());
            $pString = substr($pString, 13);
        }
        if (substr($pString, 0, 9) == '#BGCOLOR#') {
            $html .= ';background-color:#' . substr($pString, 9, 6);
            $found = true;
            $bgcolor = rgbcolor(substr($pString, 9, 6), array());
            $pString = substr($pString, 15);
        }
        $return = array();
        $return['string'] = $pString;
        $return['style'] = $style;
        $return['color'] = $color;
        $return['bgcolor'] = $bgcolor;
        $return['bold'] = $bold;
        $return['italic'] = $italic;
        $return['underline'] = $underline;
        $return['html'] = $html;
        return $return;
    }
}
 public function buildObject($C)
 {
     $lineFormat = formatForPDFandHTML($C->text_string[0]);
     //-- get any formatting stuff from the beginning of the string eg. #BOLD#
     $can_grow = $C->can_grow == '1';
     $temp_name = $this->temp_filename . $this->imagecount;
     $this->imagecount = $this->imagecount + 1;
     $draw = 'DF';
     $BC = $C->border_color;
     if (lresize($C->border_width) == 0 or $BC == 'transparent' or strtolower($BC) == 'white' or strtolower($BC) == '#ffffff' or strtolower($BC) == 'ffffff') {
         $draw = str_replace('D', '', $draw);
     }
     $BC = $C->background_color;
     if ($BC == 'transparent' or strtolower($BC) == 'white' or strtolower($BC) == '#ffffff' or strtolower($BC) == 'ffffff') {
         $draw = str_replace('F', '', $draw);
     }
     if (count($lineFormat['bgcolor']) == 0) {
         //-- no specific bg color for this object
         $color = rgbcolor($C->background_color, $this->html);
     } else {
         $color = $lineFormat['bgcolor'];
         if (strpos($draw, 'F') === false) {
             $draw = $draw . 'F';
         }
     }
     $this->SetFillColor($color['r'], $color['g'], $color['b']);
     $color = rgbcolor($C->border_color, $this->html);
     $this->SetDrawColor($color['r'], $color['g'], $color['b']);
     if (count($lineFormat['color']) == 0) {
         //-- no specific color for this object
         $color = rgbcolor($C->color, $this->html);
     } else {
         $color = $lineFormat['color'];
     }
     $this->SetTextColor($color['r'], $color['g'], $color['b']);
     $field_width = hresize($C->width);
     $this->SetLineWidth(lresize($C->border_width));
     if ($C->type != 'Graph') {
         if ($draw != '') {
             $this->Rect(hresize($C->left), vresize($C->top) + $this->section_top + 2, $field_width, hresize($C->height) + hresize($C->extra_growth), $draw);
         }
     }
     if (strpos($lineFormat['style'], 'B') === false) {
         //-- no specific boldness for this object
         if ($C->font_weight == 'bold') {
             $weight = 'B';
         } else {
             $weight = '';
         }
     } else {
         $weight = 'B';
     }
     $this->SetFont($C->font_name, $weight, fresize($C->font_size));
     $offset_top = hresize($C->original_height) * 0.2;
     if ($C->type == 'Graph') {
         if ($C->format == 'image') {
             $PT = nuRunQuery("SELECT zzsys_image_id FROM zzsys_image WHERE sim_code = '{$C->graph}'");
             $PR = db_fetch_object($PT);
             $logo = fopen(getPHPurl() . "formimage.php?dir=" . $_GET['dir'] . "&iid={$PR->zzsys_image_id}", "r");
         } else {
             $chop_at = strrpos($C->text_string[0], " width='");
             //-- chops off the "width='523' height='246' />" bit of the image
             $url = substr($C->text_string[0], 10, $chop_at - 11);
             $url = str_replace(" ", "%20", $url);
             $logo = fopen(getPHPurl() . $url, "r");
         }
         file_put_contents($temp_name . '.png', $logo);
         if (filesize($temp_name . '.png') > 0) {
             $im = imagecreatefrompng($temp_name . '.png');
             imageinterlace($im, 0);
             imagepng($im, $temp_name . '.png');
             $this->Image($temp_name . '.png', hresize($C->left), vresize($C->top) + $this->section_top + 2, $field_width, hresize($C->height) + hresize($C->extra_growth));
             unlink($temp_name . '.png');
         }
     } else {
         $string_array = array();
         if ($can_grow) {
             //-- already an array
             $string_array = $C->text_string;
         } else {
             //-- create an array
             $swap_br = array(chr(13), '<br/>', '<Br />', '<Br/>', '<BR />', '<BR/>');
             $the_string = str_replace($swap_br, '<br />', $C->text_string[0]);
             $string_array = explode("<br />", $the_string);
         }
         for ($ts = 0; $ts < count($string_array); $ts++) {
             $data = '';
             if ($ts == 0) {
                 $data = $lineFormat['string'];
                 //-- string with formatting removed
             } else {
                 $data = $string_array[$ts];
             }
             $data = str_replace('#totalNumberOfPages#', $this->total_pages, $data);
             $data = str_replace('#thePageNumber#', $this->page_no, $data);
             $data = str_replace('&nbsp;', ' ', $data);
             $data = str_replace('&nbsp', ' ', $data);
             if (strtoupper($data) == '=NOW()' or strtoupper($data) == 'NOW()') {
                 $data = $this->timestamp;
             }
             $text_width = $this->GetStringWidth($data);
             $resized_length = fwresize($text_width);
             $offset_left = 0;
             if ($C->text_align == 'right') {
                 $offset_left = vresize($C->width - $resized_length);
             }
             if ($C->text_align == 'center') {
                 $offset_left = vresize(($C->width - $resized_length) / 2);
             }
             if (!$can_grow and count($string_array) > 1) {
                 //-- dont realign top
                 $text_top = vresize($C->top) + fresize($C->font_size) / 2 * (1 * (1 + $ts)) + $this->section_top + 2;
             } else {
                 $text_top = vresize($C->top) + hresize($C->original_height) * (1 * (1 + $ts)) + $this->section_top + 2;
             }
             $left = vresize($C->left);
             $this->Text(hresize($C->left) + $offset_left, $text_top - $offset_top, $data);
         }
     }
     if ($cHeight != 0) {
     }
 }