CRM-14567
authorjitendrapurohit <jituraj7760@gmail.com>
Mon, 23 Jun 2014 11:24:48 +0000 (16:54 +0530)
committerjitendrapurohit <jituraj7760@gmail.com>
Mon, 23 Jun 2014 11:24:48 +0000 (16:54 +0530)
CRM/Contact/Form/Task/PDFLetterCommon.php
CRM/Core/BAO/PdfFormat.php
CRM/Utils/PDF/Utils.php
templates/CRM/Contact/Form/Task/PDFLetterCommon.tpl

index 5127d365d14e510132beda06af9c916b47175e25..769082425c2de3c130ecb4943d539420d6e7ab24 100644 (file)
@@ -155,6 +155,17 @@ class CRM_Contact_Form_Task_PDFLetterCommon {
       array('size' => 8, 'maxlength' => 8, 'onkeyup' => "showUpdateFormatChkBox();"),
       TRUE
     );
+
+    $config = CRM_Core_Config::singleton();
+    if ($config->wkhtmltopdfPath == false) {
+      $form->add(
+        'text',
+        'stationery',
+        ts('Stationery (relative path to PDF you wish to use as the background)'),
+        array('size' => 25, 'maxlength' => 900, 'onkeyup' => "showUpdateFormatChkBox();"),
+        FALSE
+      );
+    }
     $form->add('checkbox', 'bind_format', ts('Always use this Page Format with the selected Template'));
     $form->add('checkbox', 'update_format', ts('Update Page Format (this will affect all templates that use this format)'));
 
@@ -172,6 +183,15 @@ class CRM_Contact_Form_Task_PDFLetterCommon {
         NULL,
         FALSE
       );
+
+      if ($form->_caseId) {
+        $ccid = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseContact', $form->_caseId,
+                'contact_id', 'case_id'
+        );
+        $cancelURL = CRM_Utils_System::url('civicrm/contact/view/case',
+                     "&reset=1&action=view&cid={$ccid}&id={$form->_caseId}"
+        );
+      }
       if ($form->get('action') == CRM_Core_Action::VIEW) {
         $form->addButtons(array(
             array(
@@ -439,6 +459,15 @@ class CRM_Contact_Form_Task_PDFLetterCommon {
       );
       CRM_Activity_BAO_ActivityContact::create($activityTargetParams);
     }
+
+    if (isset($form->_caseId) && is_numeric($form->_caseId)) {
+      // if case-id is found the file the activity on the case
+      $caseParams = array(
+        'activity_id' => empty($activity->id) ? $activityIds[$contactId] : $activity->id,
+        'case_id' => $form->_caseId,
+      );
+      CRM_Case_BAO_Case::processCaseActivity($caseParams);
+    }
   }
 
   /**
index 8b99265f6e6bd03096bcf75e162c5073b8769251..96a0f3ae2157064b70dc1484b0d91662ecc84b76 100644 (file)
@@ -53,6 +53,11 @@ class CRM_Core_BAO_PdfFormat extends CRM_Core_DAO_OptionValue {
       'type' => CRM_Utils_Type::T_STRING,
       'default' => 'letter',
     ),
+    'stationery' => array(
+      'name' => 'stationery',
+      'type' =>CRM_Utils_Type::T_STRING,
+      'default' => '',
+    ),
     'orientation' => array(
       'name' => 'orientation',
       'type' => CRM_Utils_Type::T_STRING,
index 8104ea91639a4c56e7b4649cb37ba192bf2bc0e1..868526dfac9d3114550f22e70858addd3873f659 100644 (file)
@@ -67,9 +67,18 @@ class CRM_Utils_PDF_Utils {
     $orientation = CRM_Core_BAO_PdfFormat::getValue('orientation', $format);
     $metric      = CRM_Core_BAO_PdfFormat::getValue('metric', $format);
     $t           = CRM_Core_BAO_PdfFormat::getValue('margin_top', $format);
-    $r           = CRM_Core_BAO_PdfFormat::getValue('margin_right', $format);
+    $r            = CRM_Core_BAO_PdfFormat::getValue('margin_right', $format);
     $b           = CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format);
     $l           = CRM_Core_BAO_PdfFormat::getValue('margin_left', $format);
+
+    $stationery_path_partial  = CRM_Core_BAO_PdfFormat::getValue('stationery', $format);
+
+    $stationery_path = NULL;
+    if(strlen($stationery_path_partial)) {
+           $doc_root = $_SERVER['DOCUMENT_ROOT'];
+           $stationery_path = $doc_root."/".$stationery_path_partial;
+    }
+
     $margins     = array($metric,$t,$r,$b,$l);
 
     $config = CRM_Core_Config::singleton();
@@ -109,10 +118,58 @@ class CRM_Utils_PDF_Utils {
       return self::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName);
     }
     else {
-      return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
+      //return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
+      return self::_html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName,  $stationery_path);
     }
   }
 
+  static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) {
+    // Documentation on the TCPDF library can be found at: http://www.tcpdf.org
+    // This function also uses the FPDI library documented at: http://www.setasign.com/products/fpdi/about/
+    // Syntax borrowed from https://github.com/jake-mw/CDNTaxReceipts/blob/master/cdntaxreceipts.functions.inc
+    require_once 'tcpdf/tcpdf.php';
+    require_once('FPDI/fpdi.php'); // This library is only in the 'packages' area as of version 4.5
+
+    $paper_size_arr  = array( $paper_size[2], $paper_size[3]);
+
+    // print_r(  $paper_size_arr );
+    $pdf = new TCPDF($orientation, 'pt', $paper_size_arr, 'UTF-8', FALSE);
+    $pdf->Open();
+
+    if (is_readable($stationery_path)){
+      $pdf->SetStationery( $stationery_path );
+    }
+
+    $pdf->SetAuthor('');
+    $pdf->SetKeywords('CiviCRM.org');
+    $pdf->setPageUnit( $margins[0] ) ;
+    $pdf->SetMargins($margins[4], $margins[1], $margins[2], true);
+
+    $pdf->setJPEGQuality('100');
+    $pdf->SetAutoPageBreak(true, $margins[3]);
+
+    $pdf->AddPage();
+
+    $ln = true ;
+    $fill = false ;
+    $reset_parm = false;
+    $cell = false;
+    $align = '' ;
+    //print $html;
+    // output the HTML content
+    $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align);
+
+    // reset pointer to the last page
+    $pdf->lastPage();
+
+    // close and output the PDF
+    $pdf->Close();
+    $pdf_file =  'CiviLetter'.'.pdf';
+    $pdf->Output($pdf_file, 'D');
+    CRM_Utils_System::civiExit(1);
+
+  }
+
   /**
    * @param $paper_size
    * @param $orientation
index 71a69b40cf2ea604205dc7e03a4ee198e1ca1a2e..1fbf89818f8f58fdda640727b50d60a710a19d1c 100644 (file)
         <td class="label-left">{$form.margin_left.label}</td><td>{$form.margin_left.html}</td>
         <td class="label-left">{$form.margin_right.label}</td><td>{$form.margin_right.html}</td>
       </tr>
+      <tr>
+        <td class="label-left">{$form.stationery.label}</td><td>{$form.stationery.html}</td>
+        <td colspan="2">&nbsp;</td>
+      </tr>
     </table>
         <div id="bindFormat">{$form.bind_format.html}&nbsp;{$form.bind_format.label}</div>
         <div id="updateFormat" style="display: none">{$form.update_format.html}&nbsp;{$form.update_format.label}</div>