Comment fixes for CRM/Utils directory
[civicrm-core.git] / CRM / Utils / PDF / Utils.php
index 52a17dcd2c2441dd971ed6b9f5b8fb4ec4443c9d..9f103d72a8ca1857fcf02e48ed146981d2cedfeb 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
+
+// CRM-12165 - Remote file support required for image handling.
+define("DOMPDF_ENABLE_REMOTE", TRUE);
+define('DOMPDF_ENABLE_AUTOLOAD', FALSE);
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
- * $Id$
- *
+ * @copyright CiviCRM LLC (c) 2004-2015
  */
 class CRM_Utils_PDF_Utils {
 
@@ -42,7 +44,7 @@ class CRM_Utils_PDF_Utils {
    *
    * @return string|void
    */
-  static function html2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
+  public static function html2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
     if (is_array($text)) {
       $pages = &$text;
     }
@@ -59,19 +61,19 @@ class CRM_Utils_PDF_Utils {
       // PDF Page Format ID passed in
       $format = CRM_Core_BAO_PdfFormat::getById($pdfFormat);
     }
-    $paperSize    = CRM_Core_BAO_PaperSize::getByName($format['paper_size']);
-    $paper_width  = self::convertMetric($paperSize['width'], $paperSize['metric'], 'pt');
+    $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']);
+    $paper_width = self::convertMetric($paperSize['width'], $paperSize['metric'], 'pt');
     $paper_height = self::convertMetric($paperSize['height'], $paperSize['metric'], 'pt');
     // dompdf requires dimensions in points
-    $paper_size  = array(0, 0, $paper_width, $paper_height);
+    $paper_size = array(0, 0, $paper_width, $paper_height);
     $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);
-    $b           = CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format);
-    $l           = CRM_Core_BAO_PdfFormat::getValue('margin_left', $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);
+    $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_partial = CRM_Core_BAO_PdfFormat::getValue('stationery', $format);
 
     $stationery_path = NULL;
     if (strlen($stationery_path_partial)) {
@@ -79,7 +81,7 @@ class CRM_Utils_PDF_Utils {
       $stationery_path = $doc_root . "/" . $stationery_path_partial;
     }
 
-    $margins     = array($metric,$t,$r,$b,$l);
+    $margins = array($metric, $t, $r, $b, $l);
 
     $config = CRM_Core_Config::singleton();
     $html = "
@@ -124,37 +126,48 @@ class CRM_Utils_PDF_Utils {
     }
   }
 
-  static function _html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path) {
+  /**
+   * Convert html to tcpdf.
+   *
+   * @param $paper_size
+   * @param $orientation
+   * @param $margins
+   * @param $html
+   * @param $output
+   * @param $fileName
+   * @param $stationery_path
+   */
+  public 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
+    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]);
+    $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 );
+    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->setPageUnit($margins[0]);
+    $pdf->SetMargins($margins[4], $margins[1], $margins[2], TRUE);
 
     $pdf->setJPEGQuality('100');
-    $pdf->SetAutoPageBreak(true, $margins[3]);
+    $pdf->SetAutoPageBreak(TRUE, $margins[3]);
 
     $pdf->AddPage();
 
-    $ln = true ;
-    $fill = false ;
-    $reset_parm = false;
-    $cell = false;
-    $align = '' ;
+    $ln = TRUE;
+    $fill = FALSE;
+    $reset_parm = FALSE;
+    $cell = FALSE;
+    $align = '';
 
     // output the HTML content
     $pdf->writeHTML($html, $ln, $fill, $reset_parm, $cell, $align);
@@ -164,7 +177,7 @@ class CRM_Utils_PDF_Utils {
 
     // close and output the PDF
     $pdf->Close();
-    $pdf_file =  'CiviLetter'.'.pdf';
+    $pdf_file = 'CiviLetter' . '.pdf';
     $pdf->Output($pdf_file, 'D');
     CRM_Utils_System::civiExit(1);
   }
@@ -174,15 +187,12 @@ class CRM_Utils_PDF_Utils {
    * @param $orientation
    * @param $html
    * @param $output
-   * @param $fileName
+   * @param string $fileName
    *
    * @return string
    */
-  static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) {
-    // CRM-12165 - Remote file support required for image handling.
-    define("DOMPDF_ENABLE_REMOTE", true);
-    define('DOMPDF_ENABLE_AUTOLOAD', false);
-    require_once 'packages/vendor/dompdf/dompdf/dompdf_config.inc.php';
+  public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) {
+    require_once 'vendor/dompdf/dompdf/dompdf_config.inc.php';
 
     $dompdf = new DOMPDF();
     $dompdf->set_paper($paper_size, $orientation);
@@ -203,9 +213,9 @@ class CRM_Utils_PDF_Utils {
    * @param $margins
    * @param $html
    * @param $output
-   * @param $fileName
+   * @param string $fileName
    */
-  static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) {
+  public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) {
     require_once 'packages/snappy/src/autoload.php';
     $config = CRM_Core_Config::singleton();
     $snappy = new Knp\Snappy\Pdf($config->wkhtmltopdfPath);
@@ -221,16 +231,15 @@ class CRM_Utils_PDF_Utils {
       return $pdf;
     }
     else {
-      header('Content-Type: application/pdf');
-      header('Content-Disposition: attachment; filename="' . $fileName . '"');
+      CRM_Utils_System::setHttpHeader('Content-Type', 'application/pdf');
+      CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
       echo $pdf;
     }
   }
 
-  /*
-   * function to convert value from one metric to another
-   */
   /**
+   * convert value from one metric to another.
+   *
    * @param $value
    * @param $from
    * @param $to
@@ -238,7 +247,7 @@ class CRM_Utils_PDF_Utils {
    *
    * @return float|int
    */
-  static function convertMetric($value, $from, $to, $precision = NULL) {
+  public static function convertMetric($value, $from, $to, $precision = NULL) {
     switch ($from . $to) {
       case 'incm':
         $value *= 2.54;
@@ -294,127 +303,4 @@ class CRM_Utils_PDF_Utils {
     return $value;
   }
 
-  /**
-   * @param $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,
-    $numPages = 1,
-    $echo     = TRUE,
-    $output   = 'College_Match_App',
-    $creator  = 'CiviCRM',
-    $author   = 'http://www.civicrm.org/',
-    $title    = '2006 College Match Scholarship Application'
-  ) {
-    try {
-      $pdf = new PDFlib();
-      $pdf->set_parameter("compatibility", "1.6");
-      $pdf->set_parameter("licensefile", "/home/paras/bin/license/pdflib.txt");
-
-      if ($pdf->begin_document('', '') == 0) {
-        CRM_Core_Error::statusBounce("PDFlib Error: " . $pdf->get_errmsg());
-      }
-
-      $config = CRM_Core_Config::singleton();
-      $pdf->set_parameter('resourcefile', $config->templateDir . '/Quest/pdf/pdflib.upr');
-      $pdf->set_parameter('textformat', 'utf8');
-
-      /* Set the search path for fonts and PDF files */
-
-      $pdf->set_parameter('SearchPath', $searchPath);
-
-      /* This line is required to avoid problems on Japanese systems */
-
-      $pdf->set_parameter('hypertextencoding', 'winansi');
-
-      $pdf->set_info('Creator', $creator);
-      $pdf->set_info('Author', $author);
-      $pdf->set_info('Title', $title);
-
-      $blockContainer = $pdf->open_pdi($fileName, '', 0);
-      if ($blockContainer == 0) {
-        CRM_Core_Error::statusBounce('PDFlib Error: ' . $pdf->get_errmsg());
-      }
-
-      for ($i = 1; $i <= $numPages; $i++) {
-        $page = $pdf->open_pdi_page($blockContainer, $i, '');
-        if ($page == 0) {
-          CRM_Core_Error::statusBounce('PDFlib Error: ' . $pdf->get_errmsg());
-        }
-
-        /* dummy page size */
-        $pdf->begin_page_ext(20, 20, '');
-
-        /* This will adjust the page size to the block container's size. */
-
-        $pdf->fit_pdi_page($page, 0, 0, 'adjustpage');
-
-
-        $status = array();
-        /* Fill all text blocks with dynamic data */
-
-        foreach ($values as $key => $value) {
-          if (is_array($value)) {
-            continue;
-          }
-
-          // pdflib does like the forward slash character, hence convert
-          $value = str_replace('/', '_', $value);
-
-          $res = $pdf->fill_textblock($page,
-            $key,
-            $value,
-            '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('');
-        $pdf->close_pdi_page($page);
-      }
-
-      $pdf->end_document('');
-      $pdf->close_pdi($blockContainer);
-
-      $buf = $pdf->get_buffer();
-      $len = strlen($buf);
-
-      if ($echo) {
-        header('Content-type: application/pdf');
-        header("Content-Length: $len");
-        header("Content-Disposition: inline; filename={$output}.pdf");
-        echo $buf;
-        CRM_Utils_System::civiExit();
-      }
-      else {
-        return $buf;
-      }
-    }
-    catch(PDFlibException$excp) {
-      CRM_Core_Error::statusBounce('PDFlib Error: Exception' .
-        "[" . $excp->get_errnum() . "] " . $excp->get_apiname() . ": " .
-        $excp->get_errmsg()
-      );
-    }
-    catch(Exception$excp) {
-      CRM_Core_Error::statusBounce("PDFlib Error: " . $excp->get_errmsg());
-    }
-  }
 }
-