$form->assign('useSelectedPageFormat', ts('Should the new template always use the selected Page Format?'));
$form->assign('totalSelectedContacts', count($form->_contactIds));
+ $form->add('select', 'document_type', ts('Document Type'), CRM_Core_SelectValues::documentFormat());
+
CRM_Mailing_BAO_Mailing::commonCompose($form);
$buttons = array();
'type' => 'submit',
'name' => ts('Download Document'),
'isDefault' => TRUE,
+ 'icon' => 'fa-download',
);
$buttons[] = array(
'type' => 'submit',
$buttonName = $form->controller->getButtonName();
$skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE;
$skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE;
+ $html = array();
foreach ($form->_contactIds as $item => $contactId) {
$params = array('contact_id' => $contactId);
self::createActivities($form, $html_message, $form->_contactIds);
}
- CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues);
+ $type = $formValues['document_type'];
+
+ if ($type == 'pdf') {
+ CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues);
+ }
+ else {
+ CRM_Utils_PDF_Document::html2doc($html, "CiviLetter.$type", $formValues);
+ }
$form->postProcessHook();
--- /dev/null
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7 |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2015 |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM. |
+ | |
+ | CiviCRM is free software; you can copy, modify, and distribute it |
+ | under the terms of the GNU Affero General Public License |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
+ | |
+ | CiviCRM is distributed in the hope that it will be useful, but |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
+ | See the GNU Affero General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU Affero General Public |
+ | License and the CiviCRM Licensing Exception along |
+ | with this program; if not, contact CiviCRM LLC |
+ | at info[AT]civicrm[DOT]org. If you have questions about the |
+ | GNU Affero General Public License or the licensing of CiviCRM, |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2015
+ */
+class CRM_Utils_PDF_Document {
+
+ /**
+ * @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);
+ }
+ $formats = array(
+ 'docx' => 'Word2007',
+ 'odt' => 'ODText',
+ 'html' => 'HTML',
+ // todo
+ 'pdf' => 'PDF',
+ );
+ $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $formats[$ext]);
+
+ // TODO: Split document generation and output into separate functions
+ CRM_Utils_System::setHttpHeader('Content-Type', "application/$ext");
+ CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
+ $objWriter->save("php://output");
+ }
+
+ /**
+ * @param $value
+ * @param $metric
+ * @return int
+ */
+ public static function toTwip($value, $metric) {
+ $point = CRM_Utils_PDF_Utils::convertMetric($value, $metric, 'pt');
+ return \PhpOffice\PhpWord\Shared\Converter::pointToTwip($point);
+ }
+
+}