Tuned up PdfLatex font settings
[civicrm-core.git] / CRM / Utils / PDF / Utils.php
index 72c0d6c5bffeba1df5a01fc21f0d920b9712d972..0a166a9f688327b998e6ac9f6dc0c779c6d1b944 100644 (file)
 
 use Dompdf\Dompdf;
 use Dompdf\Options;
+
 /**
  *
  * @package CRM
  * @copyright CiviCRM LLC (c) 2004-2019
  */
+
 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{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}
+\setDefaultTransitions{\fontspec{FreeSerif}}{}
+\setTransitionsForLatin{\fontspec{FreeSerif}}{}
+\setTransitionsForArabics{\fontspec{FreeSerif}}{}
+\setTransitionsForCJK{\fontspec{WenQuanYi Zen Hei}}{}
+\setTransitionsForDiacritics{\fontspec{FreeSerif}}{}
+\setTransitionTo{Hebrew}{\fontspec{FreeSerif}}
+\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/drupal-7.27/sites/all/modules/civicrm/sigjohns.pdf}
+       \\\\
+
+       \ifx\@empty\fromsig
+           \fromname
+       \else \fromsig \fi\strut}
+   \par}
+\medskipamount=\parskip
+
+%% This line might be necessary, but it was not able to find utf8.def on my
+%% machine.
+%% \usepackage[utf8x]{inputenc}
+\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 . '"');
+      echo $pdf;
+//      CRM_Utils_PDF_Utils::enqueuePDF($pdf);
+
+    }
+  }
+
   /**
    * @param array $text
    *   List of HTML snippets.
@@ -141,7 +250,8 @@ class CRM_Utils_PDF_Utils {
     // 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
+    // This library is only in the 'packages' area as of version 4.5
+    require_once 'FPDI/fpdi.php';
 
     $paper_size_arr = [$paper_size[2], $paper_size[3]];
 
@@ -229,6 +339,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;
@@ -237,6 +348,8 @@ class CRM_Utils_PDF_Utils {
       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);
+
     }
   }