The old pdf test system doesn't work anymore, defaulting to production method
[civicrm-core.git] / CRM / Utils / PDF / Utils.php
index a751baa1e38b9b72825d80d38a2c2e270598eaa3..9353ea2ba397b9f6f7e45912fdcea8261973778b 100644 (file)
@@ -18,8 +18,115 @@ use Dompdf\Options;
  * @package CRM
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
+
 class CRM_Utils_PDF_Utils {
 
+  public static function enqueuePDF($pdf) {
+
+    $fname = time().'_lp.pdf';
+    file_put_contents('/tmp/'.$fname, $pdf);
+    header('Location: /civicrm/lp-setup?file='.$fname);
+    exit;
+  }
+
+  public static function latex2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL) {
+         /* FIXME: get $paper_size, $orientation, $margins */
+
+    if (is_array($text)) {
+      $pages = &$text;
+    }
+    else {
+      $pages = array($text);
+    }
+
+    $head='\documentclass[12pt]{letter}
+\usepackage{url}
+\usepackage{ucs}
+\usepackage{array}
+\usepackage{graphicx}
+\usepackage[T1]{fontenc}
+\usepackage{fullpage}
+\usepackage{fontspec,xunicode}
+%% VERY IMPORTANT.  Configures supported languages and fonts to use for each one.
+\usepackage[Latin, Hebrew, Arabics, CJK, Diacritics]{ucharclasses}
+\newfontfamily{\normalfont}[Ligatures=TeX]{FreeSerif}
+\newfontfamily{\cjkfont}{WenQuanYi Zen Hei}
+\setDefaultTransitions{\normalfont}{}
+\setTransitionsForLatin{\normalfont}{}
+\setTransitionsForArabics{\normalfont}{}
+\setTransitionsForCJK{\cjkfont}{}
+\setTransitionsForDiacritics{\normalfont}{}
+\setTransitionTo{Hebrew}{\normalfont}
+\setmainfont{FreeSerif}
+
+\newcommand{\fsfclosing}[1]{\par\nobreak\vspace{\parskip}
+  \stopbreaks
+  \noindent
+  \ifx\@empty\fromaddress\else
+  \hspace*{\longindentation}\fi
+  \parbox{\indentedwidth}{\raggedright
+       \ignorespaces #1\\\\[1\medskipamount]
+       \hspace*{-0.25in}\includegraphics[scale=1.0]{/var/www/html/sites/all/modules/civicrm/sigjohns.pdf}
+       \\\\
+
+       \ifx\@empty\fromsig
+           \fromname
+       \else \fromsig \fi\strut}
+   \par}
+\medskipamount=\parskip
+
+\pagestyle{empty}
+\tolerance=8000
+\address{\vspace{0.05in}}
+\signature{John Sullivan \\\\ Executive Director}
+\usepackage[
+top    = 1.5in,
+bottom = 1.25in,
+left   = 1.0in,
+right  = 1.0in]{geometry}
+\begin{document}
+';
+                $footer='
+\end{document}';
+
+    $latex = $head;
+    foreach ($pages as $page) {
+                       $latex.=$page;
+               }
+    $latex.=$footer;
+
+               $descriptorspec = array(
+        0 => array("pipe", "r"),
+        1 => array("pipe", "w")
+               );
+
+
+
+               $process = proc_open("/usr/local/bin/pdflatex_wrapper.sh", $descriptorspec, $pipes);
+
+
+         if (is_resource($process)) {
+                       fwrite($pipes[0], $latex);
+                       fclose($pipes[0]);
+
+      $pdf = stream_get_contents($pipes[1]);
+                       fclose($pipes[1]);
+         } else {
+                       CRM_Core_Error::debug_log_message("ERROR creating PDF. Check /tmp/pdflatex_*");
+               }
+
+    if ($output) {
+      return $pdf;
+    }
+    else {
+      header('Content-Type: application/pdf');
+      header('Content-Disposition: attachment; filename="' . $fileName . '"');
+      CRM_Utils_PDF_Utils::enqueuePDF($pdf);
+
+    }
+  }
+
   /**
    * @param array $text
    *   List of HTML snippets.
@@ -190,12 +297,13 @@ class CRM_Utils_PDF_Utils {
     }
     // CRM-19183 remove .pdf extension from filename
     $fileName = basename($fileName, ".pdf");
-    if (CIVICRM_UF === 'UnitTests' && headers_sent()) {
+    if (CIVICRM_UF === 'UnitTests') {
       // Streaming content will 'die' in unit tests unless ob_start()
       // has been called.
       throw new CRM_Core_Exception_PrematureExitException('_html2pdf_dompdf called', [
         'html' => $html,
         'fileName' => $fileName,
+        'output' => 'pdf',
       ]);
     }
     $dompdf->stream($fileName);
@@ -220,6 +328,7 @@ class CRM_Utils_PDF_Utils {
     $snappy->setOption("margin-right", $margins[2] . $margins[0]);
     $snappy->setOption("margin-bottom", $margins[3] . $margins[0]);
     $snappy->setOption("margin-left", $margins[4] . $margins[0]);
+    $html = preg_replace('/{ }/', ' ', $html);
     $pdf = $snappy->getOutputFromHtml($html);
     if ($output) {
       return $pdf;
@@ -227,7 +336,8 @@ class CRM_Utils_PDF_Utils {
     else {
       CRM_Utils_System::setHttpHeader('Content-Type', 'application/pdf');
       CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
-      echo $pdf;
+      CRM_Utils_PDF_Utils::enqueuePDF($pdf);
+
     }
   }