pdfbulkexport.class.inc.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. // Copyright (C) 2015 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Bulk export: PDF export, based on the HTML export converted to PDF
  20. *
  21. * @copyright Copyright (C) 2015 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class PDFBulkExport extends HTMLBulkExport
  25. {
  26. public function DisplayUsage(Page $oP)
  27. {
  28. $oP->p(" * pdf format options:");
  29. $oP->p(" *\tfields: (mandatory) the comma separated list of field codes to export (e.g: name,org_id,service_name...).");
  30. $oP->p(" *\tpage_size: (optional) size of the page. One of A4, A3, Letter (default is 'A4').");
  31. $oP->p(" *\tpage_orientation: (optional) the orientation of the page. Either Portrait or Landscape (default is 'Portrait').");
  32. $oP->p(" *\tdate_format: the format to use when exporting date and time fields (default = the SQL format). e.g. 'Y-m-d H:i:s'");
  33. }
  34. public function EnumFormParts()
  35. {
  36. return array_merge(array('pdf_options' => array('pdf_options')), parent::EnumFormParts());
  37. }
  38. public function DisplayFormPart(WebPage $oP, $sPartId)
  39. {
  40. switch($sPartId)
  41. {
  42. case 'pdf_options':
  43. $oP->add('<fieldset><legend>'.Dict::S('Core:BulkExport:PDFOptions').'</legend>');
  44. $oP->add('<table class="export_parameters"><tr><td style="vertical-align:top">');
  45. $oP->add('<h3>'.Dict::S('Core:BulkExport:PDFPageFormat').'</h3>');
  46. $oP->add('<table>');
  47. $oP->add('<tr>');
  48. $oP->add('<td>'.Dict::S('Core:BulkExport:PDFPageSize').'</td>');
  49. $oP->add('<td>'.$this->GetSelectCtrl('page_size', array('A3', 'A4', 'Letter'), 'Core:BulkExport:PageSize-', 'A4').'</td>');
  50. $oP->add('</tr>');
  51. $oP->add('<td>'.Dict::S('Core:BulkExport:PDFPageOrientation').'</td>');
  52. $oP->add('<td>'.$this->GetSelectCtrl('page_orientation', array('P', 'L'), 'Core:BulkExport:PageOrientation-', 'L').'</td>');
  53. $oP->add('</tr>');
  54. $oP->add('</table>');
  55. $oP->add('</td><td style="vertical-align:top">');
  56. $sDateTimeFormat = utils::ReadParam('date_format', (string)AttributeDateTime::GetFormat(), true, 'raw_data');
  57. $sDefaultChecked = ($sDateTimeFormat == (string)AttributeDateTime::GetFormat()) ? ' checked' : '';
  58. $sCustomChecked = ($sDateTimeFormat !== (string)AttributeDateTime::GetFormat()) ? ' checked' : '';
  59. $oP->add('<h3>'.Dict::S('Core:BulkExport:DateTimeFormat').'</h3>');
  60. $sDefaultFormat = htmlentities((string)AttributeDateTime::GetFormat(), ENT_QUOTES, 'UTF-8');
  61. $sExample = htmlentities(date((string)AttributeDateTime::GetFormat()), ENT_QUOTES, 'UTF-8');
  62. $oP->add('<input type="radio" id="pdf_date_time_format_default" name="pdf_date_format_radio" value="default"'.$sDefaultChecked.'><label for="pdf_date_time_format_default"> '.Dict::Format('Core:BulkExport:DateTimeFormatDefault_Example', $sDefaultFormat, $sExample).'</label><br/>');
  63. $sFormatInput = '<input type="text" size="15" name="date_format" id="pdf_custom_date_time_format" title="" value="'.htmlentities($sDateTimeFormat, ENT_QUOTES, 'UTF-8').'"/>';
  64. $oP->add('<input type="radio" id="pdf_date_time_format_custom" name="pdf_date_format_radio" value="custom"'.$sCustomChecked.'><label for="pdf_date_time_format_custom"> '.Dict::Format('Core:BulkExport:DateTimeFormatCustom_Format', $sFormatInput).'</label>');
  65. $oP->add('</td></tr></table>');
  66. $oP->add('</fieldset>');
  67. $sJSTooltip = json_encode('<div id="date_format_tooltip">'.Dict::S('UI:CSVImport:CustomDateTimeFormatTooltip').'</div>');
  68. $oP->add_ready_script(
  69. <<<EOF
  70. $('#pdf_custom_date_time_format').tooltip({content: function() { return $sJSTooltip; } });
  71. $('#form_part_pdf_options').on('preview_updated', function() { FormatDatesInPreview('pdf', 'html'); });
  72. $('#pdf_date_time_format_default').on('click', function() { FormatDatesInPreview('pdf', 'html'); });
  73. $('#pdf_date_time_format_custom').on('click', function() { FormatDatesInPreview('pdf', 'html'); });
  74. $('#pdf_custom_date_time_format').on('click', function() { $('#pdf_date_time_format_custom').prop('checked', true); FormatDatesInPreview('pdf', 'html'); }).on('keyup', function() { FormatDatesInPreview('pdf', 'html'); });
  75. EOF
  76. );
  77. break;
  78. default:
  79. return parent:: DisplayFormPart($oP, $sPartId);
  80. }
  81. }
  82. protected function GetSelectCtrl($sName, $aValues, $sDictPrefix, $sDefaultValue)
  83. {
  84. $sCurrentValue = utils::ReadParam($sName, $sDefaultValue, false, 'raw_data');
  85. $aLabels = array();
  86. foreach($aValues as $sVal)
  87. {
  88. $aLabels[$sVal] = Dict::S($sDictPrefix.$sVal);
  89. }
  90. asort($aLabels);
  91. $sHtml = '<select name="'.$sName.'">';
  92. foreach($aLabels as $sVal => $sLabel)
  93. {
  94. $sSelected = ($sVal == $sCurrentValue) ? 'selected' : '';
  95. $sHtml .= '<option value="'.$sVal.'" '.$sSelected.'>'.htmlentities($sLabel, ENT_QUOTES, 'UTF-8').'</option>';
  96. }
  97. $sHtml .= '</select>';
  98. return $sHtml;
  99. }
  100. public function ReadParameters()
  101. {
  102. parent::ReadParameters();
  103. $this->aStatusInfo['page_size'] = utils::ReadParam('page_size', 'A4', true, 'raw_data');
  104. $this->aStatusInfo['page_orientation'] = utils::ReadParam('page_orientation', 'L', true);
  105. $sDateFormatRadio = utils::ReadParam('pdf_date_format_radio', '');
  106. switch($sDateFormatRadio)
  107. {
  108. case 'default':
  109. // Export from the UI => format = same as is the UI
  110. $this->aStatusInfo['date_format'] = (string)AttributeDateTime::GetFormat();
  111. break;
  112. case 'custom':
  113. // Custom format specified from the UI
  114. $this->aStatusInfo['date_format'] = utils::ReadParam('date_format', (string)AttributeDateTime::GetFormat(), true, 'raw_data');
  115. break;
  116. default:
  117. // Export from the command line (or scripted) => default format is SQL, as in previous versions of iTop, unless specified otherwise
  118. $this->aStatusInfo['date_format'] = utils::ReadParam('date_format', (string)AttributeDateTime::GetSQLFormat(), true, 'raw_data');
  119. }
  120. }
  121. public function GetHeader()
  122. {
  123. $this->aStatusInfo['tmp_file'] = $this->MakeTmpFile('data');
  124. $sData = parent::GetHeader();
  125. $hFile = @fopen($this->aStatusInfo['tmp_file'], 'ab');
  126. if ($hFile === false)
  127. {
  128. throw new Exception('PDFBulkExport: Failed to open temporary data file: "'.$this->aStatusInfo['tmp_file'].'" for writing.');
  129. }
  130. fwrite($hFile, $sData."\n");
  131. fclose($hFile);
  132. return '';
  133. }
  134. public function GetNextChunk(&$aStatus)
  135. {
  136. $oPrevFormat = AttributeDateTime::GetFormat();
  137. $oPrevDateFormat = AttributeDate::GetFormat();
  138. $oDateTimeFormat = new DateTimeFormat($this->aStatusInfo['date_format']);
  139. AttributeDateTime::SetFormat($oDateTimeFormat);
  140. AttributeDate::SetFormat(new DateTimeFormat($oDateTimeFormat->ToDateFormat()));
  141. $sData = parent::GetNextChunk($aStatus);
  142. AttributeDateTime::SetFormat($oPrevFormat);
  143. AttributeDate::SetFormat($oPrevDateFormat);
  144. $hFile = @fopen($this->aStatusInfo['tmp_file'], 'ab');
  145. if ($hFile === false)
  146. {
  147. throw new Exception('PDFBulkExport: Failed to open temporary data file: "'.$this->aStatusInfo['tmp_file'].'" for writing.');
  148. }
  149. fwrite($hFile, $sData."\n");
  150. fclose($hFile);
  151. return '';
  152. }
  153. public function GetFooter()
  154. {
  155. $sData = parent::GetFooter();
  156. // We need a lot of time for the PDF conversion
  157. set_time_limit(60*10); // 10 minutes max ???
  158. require_once(APPROOT.'application/pdfpage.class.inc.php');
  159. $oPage = new PDFPage(Dict::Format('Core:BulkExportOf_Class', MetaModel::GetName($this->oSearch->GetClass())), $this->aStatusInfo['page_size'], $this->aStatusInfo['page_orientation']);
  160. $oPDF = $oPage->get_tcpdf();
  161. $oPDF->SetFont('dejavusans', '', 8, '', true);
  162. $oPage->add(file_get_contents($this->aStatusInfo['tmp_file']));
  163. $oPage->add($sData);
  164. $sPDF = $oPage->get_pdf();
  165. return $sPDF;
  166. }
  167. protected function GetValue($oObj, $sAttCode)
  168. {
  169. switch($sAttCode)
  170. {
  171. case 'id':
  172. $sRet = parent::GetValue($oObj, $sAttCode);
  173. break;
  174. default:
  175. $value = $oObj->Get($sAttCode);
  176. if ($value instanceof ormDocument)
  177. {
  178. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  179. if ($oAttDef instanceof AttributeImage)
  180. {
  181. // To limit the image size in the PDF output, we have to enforce the size as height/width because max-width/max-height have no effect
  182. //
  183. $iDefaultMaxWidthPx = 48;
  184. $iDefaultMaxHeightPx = 48;
  185. if ($value->IsEmpty())
  186. {
  187. $iNewWidth = $iDefaultMaxWidthPx;
  188. $iNewHeight = $iDefaultMaxHeightPx;
  189. $sUrl = $oAttDef->Get('default_image');
  190. }
  191. else
  192. {
  193. list($iWidth, $iHeight) = utils::GetImageSize($value->GetData());
  194. $iMaxWidthPx = min($iDefaultMaxWidthPx, $oAttDef->Get('display_max_width'));
  195. $iMaxHeightPx = min($iDefaultMaxHeightPx, $oAttDef->Get('display_max_height'));
  196. $fScale = min($iMaxWidthPx / $iWidth, $iMaxHeightPx / $iHeight);
  197. $iNewWidth = $iWidth * $fScale;
  198. $iNewHeight = $iHeight * $fScale;
  199. $sUrl = 'data:' . $value->GetMimeType() . ';base64,' . base64_encode($value->GetData());
  200. }
  201. $sRet = '<img src="' . $sUrl . '" style="width: ' . $iNewWidth . 'px; height: ' . $iNewHeight . 'px">';
  202. $sRet = '<div class="view-image">'.$sRet.'</div>';
  203. }
  204. else
  205. {
  206. $sRet = parent::GetValue($oObj, $sAttCode);
  207. }
  208. }
  209. else
  210. {
  211. $sRet = parent::GetValue($oObj, $sAttCode);
  212. }
  213. }
  214. return $sRet;
  215. }
  216. public function GetSupportedFormats()
  217. {
  218. return array('pdf' => Dict::S('Core:BulkExport:PDFFormat'));
  219. }
  220. public function GetMimeType()
  221. {
  222. return 'application/x-pdf';
  223. }
  224. public function GetFileExtension()
  225. {
  226. return 'pdf';
  227. }
  228. }