pdfbulkexport.class.inc.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. }
  33. public function EnumFormParts()
  34. {
  35. return array_merge(array('pdf_options' => array('pdf_options')), parent::EnumFormParts());
  36. }
  37. public function DisplayFormPart(WebPage $oP, $sPartId)
  38. {
  39. switch($sPartId)
  40. {
  41. case 'pdf_options':
  42. $oP->add('<fieldset><legend>'.Dict::S('Core:BulkExport:PDFOptions').'</legend>');
  43. $oP->add('<table>');
  44. $oP->add('<tr>');
  45. $oP->add('<td>'.Dict::S('Core:BulkExport:PDFPageSize').'</td>');
  46. $oP->add('<td>'.$this->GetSelectCtrl('page_size', array('A3', 'A4', 'Letter'), 'Core:BulkExport:PageSize-', 'A4').'</td>');
  47. $oP->add('</tr>');
  48. $oP->add('<td>'.Dict::S('Core:BulkExport:PDFPageOrientation').'</td>');
  49. $oP->add('<td>'.$this->GetSelectCtrl('page_orientation', array('P', 'L'), 'Core:BulkExport:PageOrientation-', 'L').'</td>');
  50. $oP->add('</tr>');
  51. $oP->add('</table>');
  52. $oP->add('</fieldset>');
  53. break;
  54. default:
  55. return parent:: DisplayFormPart($oP, $sPartId);
  56. }
  57. }
  58. protected function GetSelectCtrl($sName, $aValues, $sDictPrefix, $sDefaultValue)
  59. {
  60. $sCurrentValue = utils::ReadParam($sName, $sDefaultValue, false, 'raw_data');
  61. $aLabels = array();
  62. foreach($aValues as $sVal)
  63. {
  64. $aLabels[$sVal] = Dict::S($sDictPrefix.$sVal);
  65. }
  66. asort($aLabels);
  67. $sHtml = '<select name="'.$sName.'">';
  68. foreach($aLabels as $sVal => $sLabel)
  69. {
  70. $sSelected = ($sVal == $sCurrentValue) ? 'selected' : '';
  71. $sHtml .= '<option value="'.$sVal.'" '.$sSelected.'>'.htmlentities($sLabel, ENT_QUOTES, 'UTF-8').'</option>';
  72. }
  73. $sHtml .= '</select>';
  74. return $sHtml;
  75. }
  76. public function ReadParameters()
  77. {
  78. parent::ReadParameters();
  79. $this->aStatusInfo['page_size'] = utils::ReadParam('page_size', 'A4', true, 'raw_data');
  80. $this->aStatusInfo['page_orientation'] = utils::ReadParam('page_orientation', 'L', true);
  81. }
  82. public function GetHeader()
  83. {
  84. $this->aStatusInfo['tmp_file'] = $this->MakeTmpFile('data');
  85. $sData = parent::GetHeader();
  86. $hFile = @fopen($this->aStatusInfo['tmp_file'], 'ab');
  87. if ($hFile === false)
  88. {
  89. throw new Exception('PDFBulkExport: Failed to open temporary data file: "'.$this->aStatusInfo['tmp_file'].'" for writing.');
  90. }
  91. fwrite($hFile, $sData."\n");
  92. fclose($hFile);
  93. return '';
  94. }
  95. public function GetNextChunk(&$aStatus)
  96. {
  97. $sData = parent::GetNextChunk($aStatus);
  98. $hFile = @fopen($this->aStatusInfo['tmp_file'], 'ab');
  99. if ($hFile === false)
  100. {
  101. throw new Exception('PDFBulkExport: Failed to open temporary data file: "'.$this->aStatusInfo['tmp_file'].'" for writing.');
  102. }
  103. fwrite($hFile, $sData."\n");
  104. fclose($hFile);
  105. return '';
  106. }
  107. public function GetFooter()
  108. {
  109. $sData = parent::GetFooter();
  110. require_once(APPROOT.'application/pdfpage.class.inc.php');
  111. $oPage = new PDFPage(Dict::Format('Core:BulkExportOf_Class', MetaModel::GetName($this->oSearch->GetClass())), $this->aStatusInfo['page_size'], $this->aStatusInfo['page_orientation']);
  112. $oPDF = $oPage->get_tcpdf();
  113. $oPDF->SetFont('dejavusans', '', 8, '', true);
  114. $oPage->add(file_get_contents($this->aStatusInfo['tmp_file']));
  115. $oPage->add($sData);
  116. $sPDF = $oPage->get_pdf();
  117. return $sPDF;
  118. }
  119. public function GetSupportedFormats()
  120. {
  121. return array('pdf' => Dict::S('Core:BulkExport:PDFFormat'));
  122. }
  123. public function GetMimeType()
  124. {
  125. return 'application/x-pdf';
  126. }
  127. public function GetFileExtension()
  128. {
  129. return 'pdf';
  130. }
  131. }