CRM-16005 - Use and configure the dompdf provided by composer.
[civicrm-core.git] / CRM / Utils / PDF / Utils.php
index 0f5ac5b1dac3b2508088513ad8bebcdff01aa662..f7dc760449e4427d7b694cf47e1f6f799e0ef6e3 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -59,19 +59,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 +79,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,6 +124,17 @@ class CRM_Utils_PDF_Utils {
     }
   }
 
+  /**
+   * 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/
@@ -131,7 +142,7 @@ class CRM_Utils_PDF_Utils {
     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]);
+    $paper_size_arr = array($paper_size[2], $paper_size[3]);
 
     $pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
     $pdf->Open();
@@ -179,8 +190,11 @@ class CRM_Utils_PDF_Utils {
    * @return string
    */
   public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) {
-    require_once 'packages/dompdf/dompdf_config.inc.php';
-    spl_autoload_register('DOMPDF_autoload');
+    // CRM-12165 - Remote file support required for image handling.
+    define('DOMPDF_ENABLE_REMOTE', TRUE);
+    define('DOMPDF_ENABLE_AUTOLOAD', FALSE);
+    require_once 'vendor/dompdf/dompdf/dompdf_config.inc.php';
+
     $dompdf = new DOMPDF();
     $dompdf->set_paper($paper_size, $orientation);
     $dompdf->load_html($html);
@@ -224,10 +238,9 @@ class CRM_Utils_PDF_Utils {
     }
   }
 
-  /*
-   * convert value from one metric to another
-   */
   /**
+   * convert value from one metric to another.
+   *
    * @param $value
    * @param $from
    * @param $to
@@ -302,7 +315,7 @@ class CRM_Utils_PDF_Utils {
    * @param string $author
    * @param string $title
    */
-  static function &pdflib(
+  public static function &pdflib(
     $fileName,
     $searchPath,
     &$values,
@@ -396,14 +409,15 @@ class CRM_Utils_PDF_Utils {
         return $buf;
       }
     }
-    catch(PDFlibException$excp) {
+    catch (PDFlibException$excp) {
       CRM_Core_Error::statusBounce('PDFlib Error: Exception' .
         "[" . $excp->get_errnum() . "] " . $excp->get_apiname() . ": " .
         $excp->get_errmsg()
       );
     }
-    catch(Exception$excp) {
+    catch (Exception$excp) {
       CRM_Core_Error::statusBounce("PDFlib Error: " . $excp->get_errmsg());
     }
   }
+
 }