function buildFinalObject($OLP)
{
    //-- OLP = one long page
    $Final_Object = new nuReport($OLP, $OLP->displayJavaScript());
    $Final_Object->page_height = $OLP->page_height;
    $Final_Object->page_width = $OLP->page_width;
    $new_page = new nuPage();
    $reportSectionsArray = $GLOBALS['nuSections'];
    unset($GLOBALS['nuSections']);
    for ($G = 0; $G < count($reportSectionsArray); $G++) {
        unset($section_array);
        unset($record);
        unset($controls);
        unset($S);
        $nuS = gzuncompress($reportSectionsArray[$G]['the_section']);
        eval($nuS);
        //-- make section_array, record and controls from table record
        $S = new nuSection($emptySection, $new_page);
        //-- build a section using nothing
        $S->load_from_array($section_array, $record);
        for ($ct = 0; $ct < count($controls); $ct++) {
            //-- add controls to rebuilt section
            $S->controls[$ct] = new nuControl(null, null);
            $S->controls[$ct]->load_from_array($controls[$ct]);
        }
        if ($S->original_height > $OLP->get_remaining_height()) {
            //====start another page because the next section won't fit
            $new_section = new nuSection($S, $new_page);
            $new_section->height = $OLP->get_remaining_height();
            //-- build a blank section the size of the remaining height
            $new_section->name = 'Padding';
            //-- name this section as 'PageBreak'
            buildClassCode($new_section, count($Final_Object->pages));
            //==========page footer===========================
            if ($S->name != "Report_Header") {
                $page_footer = $OLP->page_footer;
                $temp_section = new nuSection($page_footer, $new_page);
                for ($pf = 0; $pf < count($page_footer->controls); $pf++) {
                    $temp_control = new nuControl($page_footer->controls[$pf], $S);
                    $temp_control->text_string = formatControlValue($temp_control, $S->record, $OLP);
                    $temp_section->controls[] = $temp_control;
                }
                $temp_section->record = $new_section->record;
                buildClassCode($temp_section, count($Final_Object->pages));
            }
            //==========end page footer=======================
            $Final_Object->pages[] = $new_page;
            $new_page = new nuPage();
            //-- build new page
            //==========page header===========================
            if ($S->name != "Report_Header") {
                $page_header = $OLP->page_header;
                $temp_section = new nuSection($page_header, $new_page);
                for ($pf = 0; $pf < count($page_header->controls); $pf++) {
                    $temp_control = new nuControl($page_header->controls[$pf], $S);
                    $temp_control->text_string = formatControlValue($temp_control, $S->record, $OLP);
                    $temp_section->controls[] = $temp_control;
                }
                $temp_section->record = $new_section->record;
                buildClassCode($temp_section, count($Final_Object->pages));
            }
            //==========end page header=======================
            $OLP->reset_remaining_height();
        }
        while (!$S->finished('while')) {
            $new_section = new nuSection($S, $new_page);
            for ($c = 0; $c < count($S->controls); $c++) {
                //-- LOOP THROUGH ALL CONTROLS
                if ($S->controls[$c]->top + 1 < $OLP->get_remaining_height()) {
                    $new_control = new nuControl($S->controls[$c], $S);
                    //-- setup control properties
                    if ($new_control->type == 'PageBreak') {
                        $new_section->height = $new_section->section_height();
                        //-- resize height to lowest control
                        $OLP->reduce_remaining_height($new_section->height);
                        //-- reset the remaining height of the page
                        buildClassCode($new_section, count($Final_Object->pages));
                        $new_section = new nuSection($S, $new_page);
                        $new_section->height = $OLP->get_remaining_height();
                        //-- build a blank section the size of the remaining height
                        $new_section->name = 'PageBreak';
                        //-- name this section as 'PageBreak'
                        break;
                    } else {
                        $new_control->top = $S->controls[$c]->top();
                        //-- set height to 0 if not first time through
                        $usable_height = $OLP->get_remaining_height() - $new_control->top;
                        //-- area this control has to fit in
                        $new_control->text_string = $S->controls[$c]->get_lines($usable_height);
                        //-- put fittable rows into the control
                        if ($new_control->type == 'Graph') {
                            $new_control->last_used = 0;
                            //-- set graph as completed
                        }
                        $new_control->height = count($new_control->text_string) * $new_control->height;
                        //-- resize height of this control
                        $new_section->controls[] = $new_control;
                        //-- add control to current section
                    }
                } else {
                    if ($OLP->get_remaining_height() > 0) {
                        $S->controls[$c]->top = $S->controls[$c]->top - $OLP->get_remaining_height();
                        //-- reduce control's top for next section
                    }
                }
            }
            if ($new_control->type != 'PageBreak') {
                $new_section->height = $new_section->section_height();
                //-- resize height to lowest control
            }
            $OLP->reduce_remaining_height($new_section->height);
            //-- reset the remaining height of the page
            buildClassCode($new_section, count($Final_Object->pages));
            if (!$S->finished('if') or $new_control->type == 'PageBreak') {
                //-- add a page break
                $new_section = new nuSection($S, $new_page);
                $new_section->height = $OLP->get_remaining_height();
                //-- build a blank section the size of the remaining height
                $new_section->name = 'Padding';
                //-- name this section as 'PageBreak'
                buildClassCode($new_section, count($Final_Object->pages));
                //==========page footer===========================
                if ($S->name != "Report_Header") {
                    $page_footer = $OLP->page_footer;
                    $temp_section = new nuSection($page_footer, $new_page);
                    for ($pf = 0; $pf < count($page_footer->controls); $pf++) {
                        $temp_control = new nuControl($page_footer->controls[$pf], $S);
                        $temp_control->text_string = formatControlValue($temp_control, $S->record, $OLP);
                        $temp_section->controls[] = $temp_control;
                    }
                    $temp_section->record = $new_section->record;
                    buildClassCode($temp_section, count($Final_Object->pages));
                }
                //==========end page footer=======================
                $Final_Object->pages[] = $new_page;
                $new_page = new nuPage();
                //-- build new page
                //==========page header===========================
                if ($S->name != "Report_Header") {
                    $page_header = $OLP->page_header;
                    $temp_section = new nuSection($page_header, $new_page);
                    for ($pf = 0; $pf < count($page_header->controls); $pf++) {
                        $temp_control = new nuControl($page_header->controls[$pf], $S);
                        $temp_control->text_string = formatControlValue($temp_control, $S->record, $OLP);
                        $temp_section->controls[] = $temp_control;
                    }
                    $temp_section->record = $new_section->record;
                    buildClassCode($temp_section, count($Final_Object->pages));
                }
                //==========end page header=======================
                //-- push down remaining objects by the remaining gap that couldn't be used on this page
                $push_down_from = $S->push_down_from();
                $add_height = $OLP->get_remaining_height();
                $grow_section = false;
                for ($pd = $push_down_from; $pd < count($S->controls); $pd++) {
                    $S->controls[$pd]->top = $S->controls[$pd]->top + $add_height;
                    $grow_section = true;
                }
                if ($grow_section) {
                    $S->height = $S->height + $add_height;
                }
                $OLP->reset_remaining_height();
            }
        }
    }
    //==========padding between report footer and page footer
    $new_section = new nuSection($S, $new_page);
    $new_section->height = $OLP->get_remaining_height();
    //-- build a blank section the size of the remaining height
    $new_section->name = 'PageBreak';
    //-- name this section as 'PageBreak'
    buildClassCode($new_section, count($Final_Object->pages));
    //==========page footer===========================
    $page_footer = $OLP->page_footer;
    $temp_section = new nuSection($page_footer, $new_page);
    $temp_section->record = $S->record;
    for ($pf = 0; $pf < count($page_footer->controls); $pf++) {
        $temp_control = new nuControl($page_footer->controls[$pf], $S);
        $temp_control->text_string = formatControlValue($temp_control, $S->record, $OLP);
        $temp_section->controls[] = $temp_control;
    }
    $temp_section->record = $new_section->record;
    buildClassCode($temp_section, count($Final_Object->pages));
    $Final_Object->pages[] = $new_page;
    build_report($Final_Object);
    tofile('end ' . date('Y-m-d H:i:s'));
}
function build_report($report_object)
{
    //-- this function is called from report_object.php
    $page_height = $report_object->page_height;
    $sectionArray = $GLOBALS['nuSections'];
    $total_pages = $sectionArray[count($sectionArray) - 1]['the_page'] + 1;
    $obj = array();
    $st = '';
    $the_page = -1;
    $timestamp = date('Y-m-d H:i:s');
    tofile('mid html 1: ' . date("H:i:s"));
    for ($G = 0; $G < count($sectionArray); $G++) {
        unset($section_array);
        unset($record);
        unset($controls);
        unset($S);
        $nuS = gzuncompress($sectionArray[$G]['the_section']);
        eval($nuS);
        //-- make section_array, record and controls from table record
        $S = new nuSection($emptySection, $new_page);
        //-- build a section using nothing
        $S->load_from_array($section_array, $record);
        for ($ct = 0; $ct < count($controls); $ct++) {
            //-- add controls to rebuilt section
            $S->controls[$ct] = new nuControl(null, null);
            $S->controls[$ct]->load_from_array($controls[$ct]);
        }
        if (!in_array($S->name, $obj)) {
            $style = 'position:relative;height:' . $S->height . "px";
            if ($S->background_color == '#ebebeb') {
                $S->background_color = '#FFFFFF';
            }
            $style .= ';background-color:' . $S->background_color . ';border-width:0px';
            $st .= '   .' . $S->name . ' { ' . $style . "}\n";
            $obj[] = $S->name;
        }
        for ($c = 0; $c < count($S->controls); $c++) {
            if (!in_array($S->controls[$c]->name, $obj)) {
                $name = $S->controls[$c]->name;
                $style = 'overflow:hidden;position:absolute;font-size:' . parseint($S->controls[$c]->font_size) . "px";
                $style .= ';font-family:' . $S->controls[$c]->font_name;
                $style .= ';font-weight:' . $S->controls[$c]->font_weight;
                $style .= ';background-color:' . $S->controls[$c]->background_color;
                $style .= ';color:' . $S->controls[$c]->color;
                $style .= ';text-align:' . $S->controls[$c]->text_align;
                $style .= ';top:' . parseint($S->controls[$c]->top) . "px";
                $style .= ';left:' . parseint($S->controls[$c]->left) . "px";
                $style .= ';width:' . parseint($S->controls[$c]->width) . "px";
                $style .= ';border-width:' . parseint($S->controls[$c]->border_width) . "px";
                $style .= ';border-color:' . $S->controls[$c]->border_color;
                $style .= ';border-style:' . $S->controls[$c]->border_style;
                $st .= '   .' . $S->controls[$c]->name . ' { ' . $style . "}\n";
                $obj[] = $S->controls[$c]->name;
            }
        }
    }
    $st1 = "";
    $st1 .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n";
    $st1 .= "<html><!--version 3-->\n<head>\n<title></title>\n";
    $st1 .= "<meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\">\n";
    $st1 .= "<script type='text/javascript' src='common.js'></script>\n";
    $st1 .= "<style type='text/css'>\n\n\n";
    print $st1 . $st . "</style>\n\n\n\n" . $report_object->javascript . "\n\n\n\n\n</head><body onload='LoadThis()' >\n\n\n\n";
    for ($G = 0; $G < count($sectionArray); $G++) {
        unset($section_array);
        unset($record);
        unset($controls);
        unset($S);
        $nuS = gzuncompress($sectionArray[$G]['the_section']);
        eval($nuS);
        //-- make section_array, record and controls from table record
        $S = new nuSection($emptySection, $new_page);
        //-- build a section using nothing
        $S->load_from_array($section_array, $record);
        for ($ct = 0; $ct < count($controls); $ct++) {
            //-- add controls to rebuilt section
            $S->controls[$ct] = new nuControl(null, null);
            $S->controls[$ct]->load_from_array($controls[$ct]);
        }
        if ($sectionArray[$G]['the_page'] != $the_page) {
            //--end and start new page
            if ($the_page != -1) {
                //--if not first loop
                print "</div>\n\n";
                print "<div style='position:relative;page-break-before:always;font-size:1px'>.</div>\n\n";
                //15/07/09 nick: added in px
            }
            print "<div style='height:{$page_height}" . "px;'>\n";
            $the_page = $sectionArray[$G]['the_page'];
        }
        if (!($S->height == '0' || $S->height == '0px')) {
            //--dont build it if the height is zero
            print "   \n\n   <div class='{$S->name}' style='height:{$S->height}" . "px;'>\n\n";
            for ($c = 0; $c < count($S->controls); $c++) {
                $lineFormat = formatForPDFandHTML($S->controls[$c]->text_string[0]);
                //-- get any formatting stuff from the beginning of the string eg. #BOLD#
                $cHeight = $S->controls[$c]->height + $S->controls[$c]->extra_growth;
                $cSupposedToBeLine = $S->controls[$c]->height == 0 || $S->controls[$c]->height == '0';
                $cName = $S->controls[$c]->name;
                $cTop = $S->controls[$c]->top;
                $data = '';
                $style = $lineFormat['html'];
                $tags = '';
                $endtags = '';
                if ($lineFormat['bold'] == '1') {
                    $tags = $tags . '<B>';
                    $endtags = $endtags . '</B>';
                }
                if ($lineFormat['italic'] == '1') {
                    $tags = $tags . '<I>';
                    $endtags = $endtags . '</I>';
                }
                if ($lineFormat['underline'] == '1') {
                    $tags = $tags . '<U>';
                    $endtags = $endtags . '</U>';
                }
                for ($ts = 0; $ts < count($S->controls[$c]->text_string); $ts++) {
                    if ($ts == 0) {
                        $data = $data . iif($ts == 0, '', '<br />') . $lineFormat['string'];
                    } else {
                        $data = $data . iif($ts == 0, '', '<br />') . $S->controls[$c]->text_string[$ts];
                    }
                }
                $CC = $S->controls[$c];
                if (!($cHeight == '0px' || $cHeight == '0') && !$cSupposedToBeLine) {
                    $data = str_replace('#totalNumberOfPages#', $total_pages, $data);
                    $data = str_replace('#thePageNumber#', $sectionArray[$G]['the_page'] + 1, $data);
                    if (strtoupper($data) == '=NOW()' or strtoupper($data) == 'NOW()') {
                        //-- set a timestamp
                        $data = $timestamp;
                    }
                    print "           <div class='{$cName}' style='top:{$cTop}" . "px;height:{$cHeight}{$style}" . "px'>{$tags}{$data}{$endtags}</div>\n";
                } else {
                    //what we are drawing is supposed to be a line, so set b/l/r border widths to 0
                    print "           <div class='{$cName}' style='top:{$cTop}" . "px;height:{$cHeight}{$style}" . "px;border-left-width:0px;border-right-width:0px;border-bottom-width:0px;'>{$tags}{$data}{$endtags}</div>\n";
                }
            }
        }
        print "\n   </div>\n";
    }
    print "</div>\n\n</html>\n\n";
}
function build_report($report_object)
{
    //-- this function is called from report_object.php
    if ($report_object->paper_type == '') {
        $report_object->paper_type = 'A4';
    }
    if ($report_object->orientation == '') {
        $report_object->orientation = 'P';
    }
    $pdf = new nuPDF($report_object->orientation, 'mm', $report_object->paper_type);
    $page_height = $report_object->page_height;
    $sectionArray = $GLOBALS['nuSections'];
    $pdf->total_pages = $sectionArray[count($sectionArray) - 1]['the_page'] + 1;
    $obj = array();
    $st = '';
    $the_page = -1;
    for ($G = 0; $G < count($sectionArray); $G++) {
        unset($section_array);
        unset($record);
        unset($controls);
        unset($S);
        $nuS = gzuncompress($sectionArray[$G]['the_section']);
        eval($nuS);
        //-- make section_array, record and controls from table record
        $S = new nuSection($emptySection, $new_page);
        //-- build a section using nothing
        $S->load_from_array($section_array, $record);
        for ($ct = 0; $ct < count($controls); $ct++) {
            //-- add controls to rebuilt section
            $S->controls[$ct] = new nuControl(null, null);
            $S->controls[$ct]->load_from_array($controls[$ct]);
        }
        if ($sectionArray[$G]['the_page'] != $the_page) {
            //--start new page
            $pdf->section_top = 0;
            $the_page = $sectionArray[$G]['the_page'];
            $pdf->AddPage();
            $pdf->page_no = $pdf->page_no + 1;
        }
        $pdf->buildSection($S);
    }
    $pdf->Output('bob.pdf', 'I');
}