Merge pull request #20811 from elcapo/billing-fields
[civicrm-core.git] / CRM / Utils / PDF / Document.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 require_once 'TbsZip/tbszip.php';
19
20 /**
21 * Class CRM_Utils_PDF_Document.
22 */
23 class CRM_Utils_PDF_Document {
24
25 public static $ooxmlMap = [
26 'docx' => [
27 'dataFile' => 'word/document.xml',
28 'startTag' => '<w:body>',
29 'pageBreak' => '<w:p><w:pPr><w:pStyle w:val="Normal"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr></w:r><w:r><w:br w:type="page"/></w:r></w:p>',
30 'endTag' => '</w:body></w:document>',
31 ],
32 'odt' => [
33 'dataFile' => 'content.xml',
34 'startTag' => '<office:body>',
35 'pageBreak' => '<text:p text:style-name="Standard"></text:p>',
36 'endTag' => '</office:body></office:document-content>',
37 ],
38 ];
39
40 /**
41 * Convert html to a Doc file.
42 *
43 * @param array $pages
44 * List of HTML snippets.
45 * @param string $fileName
46 * The logical filename to return to client.
47 * Ex: "HelloWorld.odt".
48 * @param array|int $format
49 */
50 public static function html2doc($pages, $fileName, $format = []) {
51 if (is_array($format)) {
52 // PDF Page Format parameters passed in - merge with defaults
53 $format += CRM_Core_BAO_PdfFormat::getDefaultValues();
54 }
55 else {
56 // PDF Page Format ID passed in
57 $format = CRM_Core_BAO_PdfFormat::getById($format);
58 }
59 $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']);
60
61 $metric = CRM_Core_BAO_PdfFormat::getValue('metric', $format);
62 $pageStyle = [
63 'orientation' => CRM_Core_BAO_PdfFormat::getValue('orientation', $format),
64 'pageSizeW' => self::toTwip($paperSize['width'], $paperSize['metric']),
65 'pageSizeH' => self::toTwip($paperSize['height'], $paperSize['metric']),
66 'marginTop' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_top', $format), $metric),
67 'marginRight' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_right', $format), $metric),
68 'marginBottom' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format), $metric),
69 'marginLeft' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_left', $format), $metric),
70 ];
71 if (CIVICRM_UF === 'UnitTests' && headers_sent()) {
72 // Streaming content will 'die' in unit tests unless ob_start()
73 // has been called.
74 throw new CRM_Core_Exception_PrematureExitException('_html2doc called', [
75 'html' => $pages,
76 'fileName' => $fileName,
77 'pageStyle' => $pageStyle,
78 ]);
79 }
80
81 $ext = pathinfo($fileName, PATHINFO_EXTENSION);
82
83 $phpWord = new \PhpOffice\PhpWord\PhpWord();
84
85 $phpWord->getDocInfo()
86 ->setCreator(CRM_Core_DAO::getFieldValue('CRM_Contact_BAO_Contact', CRM_Core_Session::getLoggedInContactID(), 'display_name'));
87
88 foreach ((array) $pages as $page => $html) {
89 $section = $phpWord->addSection($pageStyle + ['breakType' => 'nextPage']);
90 \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
91 }
92
93 self::printDoc($phpWord, $ext, $fileName);
94 }
95
96 /**
97 * @param object|string $phpWord
98 * @param string $ext
99 * File extension/type.
100 * Ex: docx, odt, html.
101 * @param string $fileName
102 * The logical filename to return to client.
103 * Ex: "HelloWorld.odt".
104 * Alternatively, a full path of a file to display. This seems sketchy.
105 * Ex: "/var/lib/data/HelloWorld.odt".
106 */
107 public static function printDoc($phpWord, $ext, $fileName) {
108 $formats = [
109 'docx' => 'Word2007',
110 'odt' => 'ODText',
111 'html' => 'HTML',
112 // todo
113 'pdf' => 'PDF',
114 ];
115
116 if (realpath($fileName)) {
117 $phpWord = \PhpOffice\PhpWord\IOFactory::load($fileName, $formats[$ext]);
118 }
119
120 //CRM-20015
121 \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(TRUE);
122 $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $formats[$ext]);
123
124 CRM_Utils_System::setHttpHeader('Content-Type', "application/$ext");
125 CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
126 $objWriter->save("php://output");
127 }
128
129 /**
130 * @param $value
131 * @param $metric
132 * @return int
133 */
134 public static function toTwip($value, $metric) {
135 $point = CRM_Utils_PDF_Utils::convertMetric($value, $metric, 'pt');
136 return \PhpOffice\PhpWord\Shared\Converter::pointToTwip($point);
137 }
138
139 /**
140 * @param array $path docx/odt file path
141 * @param string $type File type
142 *
143 * @return array
144 * Return extracted content of document in HTML and document type
145 */
146 public static function docReader($path, $type) {
147 $type = array_search($type, CRM_Core_SelectValues::documentApplicationType());
148 $fileType = ($type == 'docx') ? 'Word2007' : 'ODText';
149
150 $phpWord = \PhpOffice\PhpWord\IOFactory::load($path, $fileType);
151 $phpWordHTML = new \PhpOffice\PhpWord\Writer\HTML($phpWord);
152
153 // return the html content for tokenreplacment and eventually used for document download
154 return [$phpWordHTML->getWriterPart('Body')->write(), $type];
155 }
156
157 /**
158 * Extract content of docx/odt file
159 *
160 * @param string $filePath Document file path
161 * @param string $docType File type of document
162 *
163 * @return array
164 * [string, clsTbsZip]
165 */
166 public static function unzipDoc($filePath, $docType) {
167 $dataFile = self::$ooxmlMap[$docType]['dataFile'];
168
169 $zip = new clsTbsZip();
170 $zip->Open($filePath);
171 $content = $zip->FileRead($dataFile);
172
173 return [$content, $zip];
174 }
175
176 /**
177 * Modify contents of docx/odt file(s) and later merged into one final document
178 *
179 * @param array $contents
180 * Content of formatted/token-replaced document.
181 * List of HTML snippets.
182 * @param string $fileName
183 * The logical filename to return to client.
184 * Ex: "HelloWorld.odt".
185 * @param string $docType
186 * Document type e.g. odt/docx
187 * @param clsTbsZip $zip
188 * Zip archive
189 * @param bool $returnFinalContent
190 * Return the content of file document as a string used in unit test
191 *
192 * @return string
193 */
194 public static function printDocuments($contents, $fileName, $docType, $zip, $returnFinalContent = FALSE) {
195 $dataMap = self::$ooxmlMap[$docType];
196
197 $finalContent = $zip->FileRead($dataMap['dataFile']);
198
199 // token-replaced document contents of each contact will be merged into final document
200 foreach ($contents as $key => $content) {
201 if ($key == 0) {
202 $finalContent = $content;
203 continue;
204 }
205
206 // 1. fetch the start position of document body
207 // 2. later fetch only the body part starting from position $start
208 // 3. replace closing body tag with pageBreak
209 // 4. append the $content to the finalContent
210 $start = strpos($content, $dataMap['startTag']);
211 $content = substr($content, $start);
212 $content = str_replace($dataMap['startTag'], $dataMap['pageBreak'], $content);
213 $finalContent = str_replace($dataMap['endTag'], $content, $finalContent);
214 }
215
216 if ($returnFinalContent) {
217 return $finalContent;
218 }
219
220 // Replace the loaded document file content located at $filePath with $finaContent
221 $zip->FileReplace($dataMap['dataFile'], $finalContent, TBSZIP_STRING);
222
223 $zip->Flush(TBSZIP_DOWNLOAD, $fileName);
224 }
225
226 }