<?php
App::import('Vendor','xtcpdf');



$tcpdf = new XTCPDF('P', 'mm', 'A4', true, false, 'UTF-8');


$textfont = 'times'; // looks better, finer, and more condensed than 'dejavusans'

$tcpdf->setHeaderFont(array('times','',10));
//$tcpdf->SetLeftMargin(29);
//$tcpdf->SetMargins(2, 2);


$tcpdf->SetAuthor("CMC Technologies");
$tcpdf->SetTitle("CMC Technologies Quote: ".$quote['Enquiry']['title']);
$tcpdf->SetAutoPageBreak( false );

$tcpdf->xheadercolor = array(150,0,0);
$tcpdf->xheadertext = 'CMC TECHNOLOGIES';
$tcpdf->xfootertext = 'Copyright © %d CMC Technologies. All rights reserved.';

$tcpdf->SetHeaderMargin(2);

$tcpdf->setPrintHeader(false);
$tcpdf->setPrintFooter(false);


$pageProducts = $tcpdf->calculateProductPage($quote['Currency'],
    $enquiry['Enquiry']['gst'], $products, 'LineItem', $commercialDetails);


$page1done = false; //Have we already made the first page? If so, don't show the CMC header/footer

foreach ($quote['QuotePage'] as $page) {

    $tcpdf->AddPage();

    if($page1done == false) {
        $tcpdf->Page1Header();

        if( (!$enquiry['Enquiry']['customer_reference']) || ($enquiry['Enquiry']['customer_refernece'] == '') ) {

            $enquiry_date = date('d/m/Y',$time->toUnix($enquiry['Enquiry']['created']));


            $enquiry['Enquiry']['customer_reference'] = "Enquiry on $enquiry_date";
        }

        if ($quote['Quote']['revision'] > 0) {
            $cmcRef = $enquiry['Enquiry']['title'].' rev '.$quote['Quote']['revision'];
        }
        else {
            $cmcRef = $enquiry['Enquiry']['title'];
        }


        $tcpdf->DetailsBox($enquiry['Customer']['name'], $enquiry['Contact']['email'], $enquiry['Contact']['first_name'].' '.$enquiry['Contact']['last_name'],
            $enquiry['User']['first_name'].' '.$enquiry['User']['last_name'], $enquiry['User']['email'], $cmcRef,
            $enquiry['Enquiry']['customer_reference'], $quote['Quote']['date_issued']);

    }
    else {
        $tcpdf->continuedHeader($enquiry['Enquiry']['title']);
    }



    $currentX = $tcpdf->GetX();
    $currentY = $tcpdf->GetY();

    $tcpdf->SetTextColor(0);
    $tcpdf->SetFont('times', '', 12);


    $tcpdf->writeHTMLCell(0, 0, $currentX, $currentY+5, $page['content'], '', 1, 0, true, 'L', true);


    /* Only show the footer if it's the first page */
    if($page1done == false) {
        $tcpdf->Page1Footer();
        $page1done = true;
    }



}



print_r($products);



//debug($commercialDetails);

//$tcpdf->productPageHTML($principle_name, $quote['Currency'], $enquiry['Enquiry']['gst'], $products, 'LineItem', $commercialDetails);




$tcpdf->productPage($quote['Currency'], $enquiry['Enquiry']['gst'], $products, 'LineItem', $commercialDetails,
    $pageProducts);




$output_dir = '/Users/karlcordes/Sites/quotenik/app/webroot/pdf/';

$debuglevel = Configure::read('debug');

if($debuglevel == 0) {
    //$output_dir = '/var/www/cakephp/app/webroot/pdf/';
}


if($quote['Quote']['revision'] > 0) {
    $filename = $enquiry['Enquiry']['title'].'rev'.$quote['Quote']['revision'].'.pdf';
}
else {
    $filename = $enquiry['Enquiry']['title'].'.pdf';
}


$tcpdf->Output($output_dir.$filename, 'F');

echo "<br> Wrote: ".$output_dir.$filename;

App::import('Vendor', 'xfpdi');


//$newpdf = new concat_pdf();

$newpdf = new XFPDI();

$newpdf->SetMargins(2, 2);
$newpdf->setPrintHeader(false);
$newpdf->setPrintFooter(false);

$newpdf->setFiles(array($output_dir.$filename, $output_dir.'CMC_terms_and_conditions2006_A4.pdf'));
$newpdf->concat();
$newpdf->Output($output_dir.$filename, "F");

//$tcpdf->Output('cmcquote.pdf', 'D');


?>