$widths = array(25, 40, 20, 100, 20, 20, 20, 20, 20, 20);
$height = 5;
$content = array('Date', 'Customer', 'Ref', 'Item Description', 'Cash', 'Cheque', 'Acct Sales', 'Discount', 'Salesman', 'Remarks');
$pdf->MultiCell($widths[0], $height, $content[0], 1, 'C', 0, 0, '', '', true);
$pdf->MultiCell($widths[1], $height, $content[1], 1, 'C', 0, 0, '', '', true);
$pdf->MultiCell($widths[2], $height, $content[2], 1, 'C', 0, 0, '', '', true);
$pdf->MultiCell($widths[3], $height, $content[3], 1, 'C', 0, 0, '', '', true);
$pdf->MultiCell($widths[4], $height, $content[4], 1, 'C', 0, 0, '', '', true);
$pdf->MultiCell($widths[5], $height, $content[5], 1, 'C', 0, 0, '', '', true);
$pdf->MultiCell($widths[6], $height, $content[6], 1, 'R', 0, 0, '', '', true);
$pdf->MultiCell($widths[7], $height, $content[7], 1, 'R', 0, 0, '', '', true);
$pdf->MultiCell($widths[8], $height, $content[8], 1, 'R', 0, 0, '', '', true);
$pdf->MultiCell($widths[9], $height, $content[9], 1, 'R', 0, 1, '', '', true);
foreach ($templates as $template) {
    $pdf->SetFont('dejavusans', '', 14, '', true);
    $pdf->MultiCell(305, 0, InvoiceTemplateTable::fetch($template->getId()), 1, 'L', 0, 1, '', '', true);
    $pdf->SetFont('dejavusans', '', 8, '', true);
    /*
      foreach($events as $event)
      {
        $invoice=$event->getParent();
        if($invoice->getTemplateId()==$template->getId())
        {
          $content=array(
            MyDateTime::frommysql($event->getDate())->toshortdate(),
            $invoice->getCustomer()." ".$invoice->getCustomerName(),
            $invoice->getInvno()." (".MyDateTime::frommysql($invoice->getDate())->toshortdate().")",
            $event->getType().": ".$event->getDetail1().": ".$event->getDetail2(),
            ($event->getDetail("cashamt") and $invoice->getStatus()!="Cancelled")?$event->getDetail("cashamt"):" ",
            ($event->getDetail("chequeamt") and $invoice->getStatus()!="Cancelled")?$event->getDetail("chequeamt"):" ",
            ($event->getDetail("creditamt") and $invoice->getStatus()!="Cancelled")?$event->getDetail("creditamt"):" ",
$height = 5;
$content = array('Particulars', 'Tin No', 'Ref', 'Item Description', 'Cash', 'Cheque', 'Acct Sales', 'Salesman', 'Remarks');
$pdf->MultiCell($widths[0], $height, $content[0], 1, 'C', 0, 0, '', '', true);
$pdf->MultiCell($widths[1], $height, $content[1], 1, 'C', 0, 0, '', '', true);
$pdf->MultiCell($widths[2], $height, $content[2], 1, 'C', 0, 0, '', '', true);
$pdf->MultiCell($widths[3], $height, $content[3], 1, 'C', 0, 0, '', '', true);
$pdf->MultiCell($widths[4], $height, $content[4], 1, 'C', 0, 0, '', '', true);
$pdf->MultiCell($widths[5], $height, $content[5], 1, 'C', 0, 0, '', '', true);
$pdf->MultiCell($widths[6], $height, $content[6], 1, 'R', 0, 0, '', '', true);
$pdf->MultiCell($widths[7], $height, $content[7], 1, 'R', 0, 0, '', '', true);
$pdf->MultiCell($widths[8], $height, $content[8], 1, 'R', 0, 1, '', '', true);
//===============================
$addresses = array();
foreach (array(2, 4, 1, 3) as $template_id) {
    $pdf->SetFont('dejavusans', '', 14, '', true);
    $pdf->MultiCell(298, 0, InvoiceTemplateTable::fetch($template_id), 1, 'L', 0, 1, '', '', true);
    $pdf->SetFont('dejavusans', '', 8, '', true);
    foreach ($events as $event) {
        $invoice = $event->getParent();
        if ($invoice and $invoice->getTemplateId() == $template_id) {
            //catch addresses if present
            if ($invoice->getCustomer()->getAddress() != "") {
                $addresses[] = array($invoice->getCustomer() . " " . $invoice->getCustomerName(), $invoice->getCustomer()->getTinNo(), $invoice->getInvno(), $invoice->getCustomer()->getAddress());
            }
            $content = array($invoice->getCustomer() . " " . $invoice->getCustomerName(), $invoice->getCustomer()->getTinNo(), $invoice->getInvno() . " (" . MyDateTime::frommysql($invoice->getDate())->toshortdate() . ")", $event->getType() . ": " . $event->getDetail1() . ": " . $event->getDetail2(), ($event->getDetail("cashamt") != 0 and $invoice->getStatus() != "Cancelled") ? $event->getDetail("cashamt") : " ", ($event->getDetail("chequeamt") != 0 and $invoice->getStatus() != "Cancelled") ? $event->getDetail("chequeamt") : " ", ($event->getDetail("creditamt") != 0 and $invoice->getStatus() != "Cancelled") ? $event->getDetail("creditamt") : " ", $invoice->getEmployee() ? $invoice->getEmployee() : " ", $invoice->getStatus() ? $invoice->getStatus() : ($event->getDetail("status") ? $event->getDetail("status") : " "));
            $height = 1;
            foreach ($content as $index => $txt) {
                @($numlines = $pdf->getNumLines($txt, $widths[$index], false, true, '', ''));
                if ($height < $numlines) {
                    $height = $numlines;
                }