pdfbulkexport.class.inc.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. require_once(APPROOT.'application/pdfpage.class.inc.php');
  25. class PDFBulkExport extends HTMLBulkExport
  26. {
  27. public function DisplayUsage(Page $oP)
  28. {
  29. $oP->p(" * pdf format options:");
  30. $oP->p(" *\tfields: (mandatory) the comma separated list of field codes to export (e.g: name,org_id,service_name...).");
  31. $oP->p(" *\tpage_size: (optional) size of the page. One of A4, A3, Letter (default is 'A4').");
  32. $oP->p(" *\tpage_orientation: (optional) the orientation of the page. Either Portrait or Landscape (default is 'Portrait').");
  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>');
  45. $oP->add('<tr>');
  46. $oP->add('<td>'.Dict::S('Core:BulkExport:PDFPageSize').'</td>');
  47. $oP->add('<td>'.$this->GetSelectCtrl('page_size', array('A3', 'A4', 'Letter'), 'Core:BulkExport:PageSize-', 'A4').'</td>');
  48. $oP->add('</tr>');
  49. $oP->add('<td>'.Dict::S('Core:BulkExport:PDFPageOrientation').'</td>');
  50. $oP->add('<td>'.$this->GetSelectCtrl('page_orientation', array('P', 'L'), 'Core:BulkExport:PageOrientation-', 'L').'</td>');
  51. $oP->add('</tr>');
  52. $oP->add('</table>');
  53. $oP->add('</fieldset>');
  54. break;
  55. default:
  56. return parent:: DisplayFormPart($oP, $sPartId);
  57. }
  58. }
  59. protected function GetSelectCtrl($sName, $aValues, $sDictPrefix, $sDefaultValue)
  60. {
  61. $sCurrentValue = utils::ReadParam($sName, $sDefaultValue, false, 'raw_data');
  62. $aLabels = array();
  63. foreach($aValues as $sVal)
  64. {
  65. $aLabels[$sVal] = Dict::S($sDictPrefix.$sVal);
  66. }
  67. asort($aLabels);
  68. $sHtml = '<select name="'.$sName.'"/>';
  69. foreach($aLabels as $sVal => $sLabel)
  70. {
  71. $sSelected = ($sVal == $sCurrentValue) ? 'selected' : '';
  72. $sHtml .= '<option value="'.$sVal.'" '.$sSelected.'>'.htmlentities($sLabel, ENT_QUOTES, 'UTF-8').'</option>';
  73. }
  74. $sHtml .= '</select>';
  75. return $sHtml;
  76. }
  77. public function ReadParameters()
  78. {
  79. parent::ReadParameters();
  80. $this->aStatusInfo['page_size'] = utils::ReadParam('page_size', 'A4', true, 'raw_data');
  81. $this->aStatusInfo['page_orientation'] = utils::ReadParam('page_orientation', 'L', true);
  82. }
  83. public function GetHeader()
  84. {
  85. $this->aStatusInfo['tmp_file'] = $this->MakeTmpFile('data');
  86. $sData = parent::GetHeader();
  87. $hFile = @fopen($this->aStatusInfo['tmp_file'], 'ab');
  88. if ($hFile === false)
  89. {
  90. throw new Exception('PDFBulkExport: Failed to open temporary data file: "'.$this->aStatusInfo['tmp_file'].'" for writing.');
  91. }
  92. fwrite($hFile, $sData."\n");
  93. fclose($hFile);
  94. return '';
  95. }
  96. public function GetNextChunk(&$aStatus)
  97. {
  98. $sData = parent::GetNextChunk($aStatus);
  99. $hFile = @fopen($this->aStatusInfo['tmp_file'], 'ab');
  100. if ($hFile === false)
  101. {
  102. throw new Exception('PDFBulkExport: Failed to open temporary data file: "'.$this->aStatusInfo['tmp_file'].'" for writing.');
  103. }
  104. fwrite($hFile, $sData."\n");
  105. fclose($hFile);
  106. return '';
  107. }
  108. public function GetFooter()
  109. {
  110. $sData = parent::GetFooter();
  111. $oPage = new PDFPage(Dict::Format('Core:BulkExportOf_Class', MetaModel::GetName($this->oSearch->GetClass())), $this->aStatusInfo['page_size'], $this->aStatusInfo['page_orientation']);
  112. $oPage->add(file_get_contents($this->aStatusInfo['tmp_file']));
  113. $oPage->add($sData);
  114. $sPDF = $oPage->get_pdf();
  115. return $sPDF;
  116. }
  117. public function GetSupportedFormats()
  118. {
  119. return array('pdf' => Dict::S('Core:BulkExport:PDFFormat'));
  120. }
  121. public function GetMimeType()
  122. {
  123. return 'application/x-pdf';
  124. }
  125. public function GetFileExtension()
  126. {
  127. return 'pdf';
  128. }
  129. }