Beispiel #1
0
 /**
  * @param $text
  * @param string $fileName
  * @param bool $output
  * @param null $pdfFormat
  *
  * @return string|void
  */
 public static function html2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL)
 {
     if (is_array($text)) {
         $pages =& $text;
     } else {
         $pages = array($text);
     }
     // Get PDF Page Format
     $format = CRM_Core_BAO_PdfFormat::getDefaultValues();
     if (is_array($pdfFormat)) {
         // PDF Page Format parameters passed in
         $format = array_merge($format, $pdfFormat);
     } else {
         // 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');
     $paper_height = self::convertMetric($paperSize['height'], $paperSize['metric'], 'pt');
     // dompdf requires dimensions in points
     $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);
     $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();
     // Add a special region for the HTML header of PDF files:
     $pdfHeaderRegion = CRM_Core_Region::instance('export-document-header', FALSE);
     $htmlHeader = $pdfHeaderRegion ? $pdfHeaderRegion->render('', FALSE) : '';
     $html = "\n<html>\n  <head>\n    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n    <style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style>\n    <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style>\n    {$htmlHeader}\n  </head>\n  <body>\n    <div id=\"crm-container\">\n";
     // Strip <html>, <header>, and <body> tags from each page
     $htmlElementstoStrip = array('@<head[^>]*?>.*?</head>@siu', '@<script[^>]*?>.*?</script>@siu', '@<body>@siu', '@</body>@siu', '@<html[^>]*?>@siu', '@</html>@siu', '@<!DOCTYPE[^>]*?>@siu');
     $htmlElementsInstead = array('', '', '', '', '', '');
     foreach ($pages as &$page) {
         $page = preg_replace($htmlElementstoStrip, $htmlElementsInstead, $page);
     }
     // Glue the pages together
     $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
     $html .= "\n    </div>\n  </body>\n</html>";
     if ($config->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);
     }
 }
 static function html2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL)
 {
     if (is_array($text)) {
         $pages =& $text;
     } else {
         $pages = array($text);
     }
     // Get PDF Page Format
     $format = CRM_Core_BAO_PdfFormat::getDefaultValues();
     if (is_array($pdfFormat)) {
         // PDF Page Format parameters passed in
         $format = array_merge($format, $pdfFormat);
     } else {
         // 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');
     $paper_height = self::convertMetric($paperSize['height'], $paperSize['metric'], 'pt');
     // dompdf requires dimensions in points
     $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);
     $config = CRM_Core_Config::singleton();
     $html = "\n<html>\n  <head>\n    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n    <style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style>\n    <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style>\n  </head>\n  <body>\n    <div id=\"crm-container\">\n";
     // Strip <html>, <header>, and <body> tags from each page
     $htmlElementstoStrip = array('@<head[^>]*?>.*?</head>@siu', '@<body>@siu', '@</body>@siu', '@<html[^>]*?>@siu', '@</html>@siu', '@<!DOCTYPE[^>]*?>@siu');
     $htmlElementsInstead = array('', '', '', '', '', '');
     foreach ($pages as &$page) {
         $page = preg_replace($htmlElementstoStrip, $htmlElementsInstead, $page);
     }
     // Glue the pages together
     $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
     $html .= "\n    </div>\n  </body>\n</html>";
     if ($config->wkhtmltopdfPath) {
         return self::_html2pdf_wkhtmltopdf($paper_size, $orientation, $html, $output, $fileName);
     } else {
         return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
     }
 }
Beispiel #3
0
 /**
  * @param array $pages
  * @param string $fileName
  * @param array|int $format
  */
 public static function html2doc($pages, $fileName, $format = array())
 {
     if (is_array($format)) {
         // PDF Page Format parameters passed in - merge with defaults
         $format += CRM_Core_BAO_PdfFormat::getDefaultValues();
     } else {
         // PDF Page Format ID passed in
         $format = CRM_Core_BAO_PdfFormat::getById($format);
     }
     $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']);
     $metric = CRM_Core_BAO_PdfFormat::getValue('metric', $format);
     $pageStyle = array('orientation' => CRM_Core_BAO_PdfFormat::getValue('orientation', $format), 'pageSizeW' => self::toTwip($paperSize['width'], $paperSize['metric']), 'pageSizeH' => self::toTwip($paperSize['height'], $paperSize['metric']), 'marginTop' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_top', $format), $metric), 'marginRight' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_right', $format), $metric), 'marginBottom' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format), $metric), 'marginLeft' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_left', $format), $metric));
     $ext = pathinfo($fileName, PATHINFO_EXTENSION);
     $phpWord = new \PhpOffice\PhpWord\PhpWord();
     $phpWord->getDocInfo()->setCreator(CRM_Core_DAO::getFieldValue('CRM_Contact_BAO_Contact', CRM_Core_Session::getLoggedInContactID(), 'display_name'));
     foreach ((array) $pages as $page => $html) {
         $section = $phpWord->addSection($pageStyle + array('breakType' => 'nextPage'));
         \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
     }
     self::printDoc($phpWord, $ext, $fileName);
 }