function createNewRevision($contract, &$document)
{
    // TODO add some logic to select different templates for Contracts/Quotes/Additions
    //1. Get template paths. 2. get contract/services variables.3 Proceed to fetching partial templates. 4. Proceed to fetching main template and creating pdf file.
    if ($contract->oqc_template == null) {
        $beanName = substr(trim($contract->object_name), 4);
        $templatePaths = getTemplatesPath($beanName);
    } else {
        $templatePaths = getTemplatesPath($contract->oqc_template);
    }
    $pdfData = getPdfVariables($contract);
    $segmentFilenames = array();
    $segmentFilenames['titlePage'] = titlePageToLatex($pdfData, $templatePaths);
    $segmentFilenames['textBlocks'] = textBlocksToLatex($contract, $pdfData, $templatePaths);
    //if ($segmentFilenames['textBlocks'] == null ) {
    //$GLOBALS['log']->fatal('OQC: TextBloks are empty or You do not have html2tex convertor installed!');
    //return null;
    //	}
    $segmentFilenames['services'] = servicesToLatex($pdfData, $templatePaths);
    $contractFilename = null;
    $contractFilename = templateToPdf($templatePaths["CONTRACT_TEMPLATE"], $segmentFilenames);
    if ($contractFilename == null) {
        //$GLOBALS['log']->fatal('OQC: You likely do not have PDFLATEX package installed!');
        return null;
    }
    if (DEBUG_PDF_CREATION) {
        $contractWithAttachmentsFilename = $contractFilename;
        // Add information about pdftk to the log file
        $returnTestValue = null;
        $outputArray = array();
        $returnTestValue = execute(PDFTK, '--version', $outputArray);
        $GLOBALS['log']->error('createNewRevision: pdftk $returnTestValue is: ' . $returnTestValue);
        if (!empty($outputArray)) {
            $addString = "\n\rpdftk --version output: \n\r" . implode("\n", $outputArray);
        } else {
            $addString = "\n\rpdftk --version output: null \nreturn value: {$returnTestValue}";
        }
        sugar_file_put_contents($contractWithAttachmentsFilename, $addString, FILE_APPEND | LOCK_EX);
    } else {
        $contractWithAttachmentsFilename = attachDocuments($contract, $contractFilename);
        if ($contractWithAttachmentsFilename == null) {
            $GLOBALS['log']->fatal('OQC: You likely do not have PDFTK package installed!');
            $contractWithAttachmentsFilename = $contractFilename;
        }
    }
    // unlink segmentFilenames
    foreach ($segmentFilenames as $filename) {
        unlink($filename);
    }
    $revision = new DocumentRevision();
    $revision->document_id = $document->id;
    $revision->revision = $contract->version;
    //1.7.6 Make document version the same as seed
    if (DEBUG_PDF_CREATION) {
        $revision->file_mime_type = 'text/plain';
        $revision->file_ext = 'log';
        $revision->filename = $document->category_id . '_' . $contract->svnumber . '_v' . $revision->revision . '.log';
    } else {
        $revision->file_mime_type = 'application/pdf';
        $revision->file_ext = 'pdf';
        $revision->filename = $document->category_id . '_' . $contract->svnumber . '_v' . $revision->revision . '.pdf';
    }
    $revision->save();
    rename($contractWithAttachmentsFilename, getDocumentFilename($revision->id));
    $document->document_revision_id = $revision->id;
    //$document->save();
    return true;
}
 private function categoryPagesToPdf($productCatalog)
 {
     if ($productCatalog->oqc_template == null) {
         $beanName = substr(trim($productCatalog->object_name), 4);
         $templatePaths = getTemplatesPath($beanName);
     } else {
         $templatePaths = getTemplatesPath($productCatalog->oqc_template);
     }
     $filename = templateToPdf($templatePaths['PRODUCT_CATALOG_TEMPLATE'], $this->getProductCatalogVariables($productCatalog), 3);
     //TODO cleanup here the temporary pdf files generated from product attachments
     if (!$filename) {
         return null;
     }
     return $filename;
 }
</head>
<body onunload='if(changed) alert("Warning!\r\n\nYour last changes will be discarded because you navigated off this page prior to save them.")'>
<?

require_once "lib_menu.php";
echoMenu(3,"gotoPage");

?>
<TABLE class=topts width=100% cellspacing=10 cellpadding=5 border=0 style='font-size:12px;'>
<TR valign=top>
<TD>
<div class=header>Language</div>
<?

echo "<span>Your template is in <b>$language</b> | Select if you want to start from a default template in <select onclick='changeLanguage(this.value);'><option value=''>other language...</option></span>" . PHP_EOL;
$path = getTemplatesPath();
foreach (glob($path."*.tpt") as $lang)
{
	$pinfo = pathinfo($lang);
	$lang = $pinfo["filename"];
	if ($tptExists || $lang != $language)
		echo "<option value='$lang'>$lang</option>" . PHP_EOL;
}
echo "</select><p>" . PHP_EOL;
?>
<div class=header>Text</div>
<table width=100%><tr valign=top><td>
compose your text<br>
<form>
<textarea id=text name=text autocomplete=off rows=8 cols=70 oninput='textChanged();' style=''>
<?php