Merge pull request #4772 from jitendrapurohit/CRM-15750
[civicrm-core.git] / CRM / Utils / PDF / Utils.php
index 544ae42c6154cc2ad569048541f179cbfe139bbe..5386287681d3174c1cafdcdae0ca8d75f390ad24 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  */
 class CRM_Utils_PDF_Utils {
 
+  /**
+   * @param $text
+   * @param string $fileName
+   * @param bool $output
+   * @param null $pdfFormat
+   *
+   * @return string|void
+   */
   static function html2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
     if (is_array($text)) {
       $pages = &$text;
@@ -62,6 +70,15 @@ class CRM_Utils_PDF_Utils {
     $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();
@@ -78,6 +95,7 @@ class CRM_Utils_PDF_Utils {
     // Strip <html>, <header>, and <body> tags from each page
     $htmlElementstoStrip = array(
       '@<head[^>]*?>.*?</head>@siu',
+      '@<script[^>]*?>.*?</script>@siu',
       '@<body>@siu',
       '@</body>@siu',
       '@<html[^>]*?>@siu',
@@ -102,9 +120,64 @@ class CRM_Utils_PDF_Utils {
     }
     else {
       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]);
+
+    $pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
+    $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 = '' ;
+
+    // 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
+   * @param $html
+   * @param $output
+   * @param string $fileName
+   *
+   * @return string
+   */
   static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) {
     require_once 'packages/dompdf/dompdf_config.inc.php';
     spl_autoload_register('DOMPDF_autoload');
@@ -121,6 +194,14 @@ class CRM_Utils_PDF_Utils {
     }
   }
 
+  /**
+   * @param $paper_size
+   * @param $orientation
+   * @param $margins
+   * @param $html
+   * @param $output
+   * @param string $fileName
+   */
   static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) {
     require_once 'packages/snappy/src/autoload.php';
     $config = CRM_Core_Config::singleton();
@@ -144,7 +225,15 @@ class CRM_Utils_PDF_Utils {
   }
 
   /*
-   * function to convert value from one metric to another
+   * convert value from one metric to another
+   */
+  /**
+   * @param $value
+   * @param $from
+   * @param $to
+   * @param null $precision
+   *
+   * @return float|int
    */
   static function convertMetric($value, $from, $to, $precision = NULL) {
     switch ($from . $to) {
@@ -202,6 +291,17 @@ class CRM_Utils_PDF_Utils {
     return $value;
   }
 
+  /**
+   * @param string $fileName
+   * @param $searchPath
+   * @param $values
+   * @param int $numPages
+   * @param bool $echo
+   * @param string $output
+   * @param string $creator
+   * @param string $author
+   * @param string $title
+   */
   static function &pdflib($fileName,
     $searchPath,
     &$values,
@@ -273,13 +373,6 @@ class CRM_Utils_PDF_Utils {
             'embedding encoding=winansi'
           );
 
-          /**
-           if ( $res == 0 ) {
-           CRM_Core_Error::debug( "$key, $value: $res", $pdf->get_errmsg( ) );
-           } else {
-           CRM_Core_Error::debug( "SUCCESS: $key, $value", null );
-           }
-           **/
         }
 
         $pdf->end_page_ext('');