public function createPdf($contract)
 {
     $contractFilename = templateToPdf(EXTERNAL_CONTRACT_TEMPLATE, $this->getExternalContractVariables($contract), 3);
     rename($contractFilename, "{$GLOBALS['sugar_config']['cache_dir']}/modules/Emails/attachments/{$contract->id}");
     $filename = utf8_decode($contract->name);
     // fix for #541, see http://de2.php.net/manual/de/function.utf8-decode.php
     header("Location: index.php?entryPoint=download&id={$contract->id}&type=oqc_ExternalContract&isTempFile=true&tempName={$filename}.{$contract->version}.pdf");
 }
 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;
 }
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;
}