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.
*
* @return string|void
*/
- public 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;
}
else {
- $pages = array($text);
+ $pages = [$text];
}
// Get PDF Page Format
$format = CRM_Core_BAO_PdfFormat::getDefaultValues();
// PDF Page Format parameters passed in
$format = array_merge($format, $pdfFormat);
}
- else {
+ elseif (!empty($pdfFormat)) {
// PDF Page Format ID passed in
$format = CRM_Core_BAO_PdfFormat::getById($pdfFormat);
}
$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 = [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);
$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();
+ $margins = [$metric, $t, $r, $b, $l];
// Add a special region for the HTML header of PDF files:
$pdfHeaderRegion = CRM_Core_Region::instance('export-document-header', FALSE);
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
<style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style>
- <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style>
+ <style type=\"text/css\">@import url(" . CRM_Core_Config::singleton()->userFrameworkResourceURL . "css/print.css);</style>
{$htmlHeader}
</head>
<body>
<div id=\"crm-container\">\n";
// Strip <html>, <header>, and <body> tags from each page
- $htmlElementstoStrip = array(
+ $htmlElementstoStrip = [
'@<head[^>]*?>.*?</head>@siu',
'@<script[^>]*?>.*?</script>@siu',
'@<body>@siu',
'@<html[^>]*?>@siu',
'@</html>@siu',
'@<!DOCTYPE[^>]*?>@siu',
- );
- $htmlElementsInstead = array('', '', '', '', '', '');
+ ];
+ $htmlElementsInstead = ['', '', '', '', '', ''];
foreach ($pages as & $page) {
$page = preg_replace($htmlElementstoStrip,
$htmlElementsInstead,
</div>
</body>
</html>";
- if ($config->wkhtmltopdfPath) {
+ if (CRM_Core_Config::singleton()->wkhtmltopdfPath) {
return self::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName);
}
else {
return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
- //return self::_html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName, $stationery_path);
}
}
// 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 = array($paper_size[2], $paper_size[3]);
+ $paper_size_arr = [$paper_size[2], $paper_size[3]];
$pdf = new TCPDF($orientation, 'pt', $paper_size_arr);
$pdf->Open();
$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;
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);
+
}
}