ExportPdf.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Plugins\Export;
  4. use PhpMyAdmin\Plugins\Export\Helpers\Pdf;
  5. use PhpMyAdmin\Plugins\ExportPlugin;
  6. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
  7. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
  8. use PhpMyAdmin\Properties\Options\Items\RadioPropertyItem;
  9. use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
  10. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  11. use TCPDF;
  12. use function __;
  13. use function class_exists;
  14. /**
  15. * Produce a PDF report (export) from a query
  16. */
  17. class ExportPdf extends ExportPlugin
  18. {
  19. /**
  20. * PhpMyAdmin\Plugins\Export\Helpers\Pdf instance
  21. *
  22. * @var Pdf
  23. */
  24. private $pdf;
  25. /**
  26. * PDF Report Title
  27. *
  28. * @var string
  29. */
  30. private $pdfReportTitle = '';
  31. /**
  32. * @psalm-return non-empty-lowercase-string
  33. */
  34. public function getName(): string
  35. {
  36. return 'pdf';
  37. }
  38. /**
  39. * Initialize the local variables that are used for export PDF.
  40. */
  41. protected function init(): void
  42. {
  43. if (! empty($_POST['pdf_report_title'])) {
  44. $this->pdfReportTitle = $_POST['pdf_report_title'];
  45. }
  46. $this->setPdf(new Pdf('L', 'pt', 'A3'));
  47. }
  48. protected function setProperties(): ExportPluginProperties
  49. {
  50. $exportPluginProperties = new ExportPluginProperties();
  51. $exportPluginProperties->setText('PDF');
  52. $exportPluginProperties->setExtension('pdf');
  53. $exportPluginProperties->setMimeType('application/pdf');
  54. $exportPluginProperties->setForceFile(true);
  55. $exportPluginProperties->setOptionsText(__('Options'));
  56. // create the root group that will be the options field for
  57. // $exportPluginProperties
  58. // this will be shown as "Format specific options"
  59. $exportSpecificOptions = new OptionsPropertyRootGroup('Format Specific Options');
  60. // general options main group
  61. $generalOptions = new OptionsPropertyMainGroup('general_opts');
  62. // create primary items and add them to the group
  63. $leaf = new TextPropertyItem(
  64. 'report_title',
  65. __('Report title:')
  66. );
  67. $generalOptions->addProperty($leaf);
  68. // add the group to the root group
  69. $exportSpecificOptions->addProperty($generalOptions);
  70. // what to dump (structure/data/both) main group
  71. $dumpWhat = new OptionsPropertyMainGroup(
  72. 'dump_what',
  73. __('Dump table')
  74. );
  75. $leaf = new RadioPropertyItem('structure_or_data');
  76. $leaf->setValues(
  77. [
  78. 'structure' => __('structure'),
  79. 'data' => __('data'),
  80. 'structure_and_data' => __('structure and data'),
  81. ]
  82. );
  83. $dumpWhat->addProperty($leaf);
  84. // add the group to the root group
  85. $exportSpecificOptions->addProperty($dumpWhat);
  86. // set the options for the export plugin property item
  87. $exportPluginProperties->setOptions($exportSpecificOptions);
  88. return $exportPluginProperties;
  89. }
  90. /**
  91. * Outputs export header
  92. */
  93. public function exportHeader(): bool
  94. {
  95. $pdf = $this->getPdf();
  96. $pdf->Open();
  97. $pdf->setTitleFontSize(18);
  98. $pdf->setTitleText($this->pdfReportTitle);
  99. $pdf->setTopMargin(30);
  100. return true;
  101. }
  102. /**
  103. * Outputs export footer
  104. */
  105. public function exportFooter(): bool
  106. {
  107. $pdf = $this->getPdf();
  108. // instead of $pdf->Output():
  109. return $this->export->outputHandler($pdf->getPDFData());
  110. }
  111. /**
  112. * Outputs database header
  113. *
  114. * @param string $db Database name
  115. * @param string $dbAlias Aliases of db
  116. */
  117. public function exportDBHeader($db, $dbAlias = ''): bool
  118. {
  119. return true;
  120. }
  121. /**
  122. * Outputs database footer
  123. *
  124. * @param string $db Database name
  125. */
  126. public function exportDBFooter($db): bool
  127. {
  128. return true;
  129. }
  130. /**
  131. * Outputs CREATE DATABASE statement
  132. *
  133. * @param string $db Database name
  134. * @param string $exportType 'server', 'database', 'table'
  135. * @param string $dbAlias Aliases of db
  136. */
  137. public function exportDBCreate($db, $exportType, $dbAlias = ''): bool
  138. {
  139. return true;
  140. }
  141. /**
  142. * Outputs the content of a table in NHibernate format
  143. *
  144. * @param string $db database name
  145. * @param string $table table name
  146. * @param string $crlf the end of line sequence
  147. * @param string $errorUrl the url to go back in case of error
  148. * @param string $sqlQuery SQL query for obtaining data
  149. * @param array $aliases Aliases of db/table/columns
  150. */
  151. public function exportData(
  152. $db,
  153. $table,
  154. $crlf,
  155. $errorUrl,
  156. $sqlQuery,
  157. array $aliases = []
  158. ): bool {
  159. $db_alias = $db;
  160. $table_alias = $table;
  161. $this->initAlias($aliases, $db_alias, $table_alias);
  162. $pdf = $this->getPdf();
  163. $pdf->setCurrentDb($db);
  164. $pdf->setCurrentTable($table);
  165. $pdf->setDbAlias($db_alias);
  166. $pdf->setTableAlias($table_alias);
  167. $pdf->setAliases($aliases);
  168. $pdf->setPurpose(__('Dumping data'));
  169. $pdf->mysqlReport($sqlQuery);
  170. return true;
  171. }
  172. /**
  173. * Outputs result of raw query in PDF format
  174. *
  175. * @param string $errorUrl the url to go back in case of error
  176. * @param string|null $db the database where the query is executed
  177. * @param string $sqlQuery the rawquery to output
  178. * @param string $crlf the end of line sequence
  179. */
  180. public function exportRawQuery(string $errorUrl, ?string $db, string $sqlQuery, string $crlf): bool
  181. {
  182. global $dbi;
  183. $pdf = $this->getPdf();
  184. $pdf->setDbAlias('----');
  185. $pdf->setTableAlias('----');
  186. $pdf->setPurpose(__('Query result data'));
  187. if ($db !== null) {
  188. $pdf->setCurrentDb($db);
  189. $dbi->selectDb($db);
  190. }
  191. $pdf->mysqlReport($sqlQuery);
  192. return true;
  193. }
  194. /**
  195. * Outputs table structure
  196. *
  197. * @param string $db database name
  198. * @param string $table table name
  199. * @param string $crlf the end of line sequence
  200. * @param string $errorUrl the url to go back in case of error
  201. * @param string $exportMode 'create_table', 'triggers', 'create_view',
  202. * 'stand_in'
  203. * @param string $exportType 'server', 'database', 'table'
  204. * @param bool $do_relation whether to include relation comments
  205. * @param bool $do_comments whether to include the pmadb-style column
  206. * comments as comments in the structure;
  207. * this is deprecated but the parameter is
  208. * left here because /export calls
  209. * PMA_exportStructure() also for other
  210. * export types which use this parameter
  211. * @param bool $do_mime whether to include mime comments
  212. * @param bool $dates whether to include creation/update/check dates
  213. * @param array $aliases aliases for db/table/columns
  214. */
  215. public function exportStructure(
  216. $db,
  217. $table,
  218. $crlf,
  219. $errorUrl,
  220. $exportMode,
  221. $exportType,
  222. $do_relation = false,
  223. $do_comments = false,
  224. $do_mime = false,
  225. $dates = false,
  226. array $aliases = []
  227. ): bool {
  228. $db_alias = $db;
  229. $table_alias = $table;
  230. $purpose = '';
  231. $this->initAlias($aliases, $db_alias, $table_alias);
  232. $pdf = $this->getPdf();
  233. // getting purpose to show at top
  234. switch ($exportMode) {
  235. case 'create_table':
  236. $purpose = __('Table structure');
  237. break;
  238. case 'triggers':
  239. $purpose = __('Triggers');
  240. break;
  241. case 'create_view':
  242. $purpose = __('View structure');
  243. break;
  244. case 'stand_in':
  245. $purpose = __('Stand in');
  246. }
  247. $pdf->setCurrentDb($db);
  248. $pdf->setCurrentTable($table);
  249. $pdf->setDbAlias($db_alias);
  250. $pdf->setTableAlias($table_alias);
  251. $pdf->setAliases($aliases);
  252. $pdf->setPurpose($purpose);
  253. /**
  254. * comment display set true as presently in pdf
  255. * format, no option is present to take user input.
  256. */
  257. $do_comments = true;
  258. switch ($exportMode) {
  259. case 'create_table':
  260. $pdf->getTableDef($db, $table, $do_relation, $do_comments, $do_mime, false, $aliases);
  261. break;
  262. case 'triggers':
  263. $pdf->getTriggers($db, $table);
  264. break;
  265. case 'create_view':
  266. $pdf->getTableDef($db, $table, $do_relation, $do_comments, $do_mime, false, $aliases);
  267. break;
  268. case 'stand_in':
  269. /* export a stand-in definition to resolve view dependencies
  270. * Yet to develop this function
  271. * $pdf->getTableDefStandIn($db, $table, $crlf);
  272. */
  273. }
  274. return true;
  275. }
  276. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  277. /**
  278. * Gets the PhpMyAdmin\Plugins\Export\Helpers\Pdf instance
  279. *
  280. * @return Pdf
  281. */
  282. private function getPdf()
  283. {
  284. return $this->pdf;
  285. }
  286. /**
  287. * Instantiates the PhpMyAdmin\Plugins\Export\Helpers\Pdf class
  288. *
  289. * @param Pdf $pdf The instance
  290. */
  291. private function setPdf($pdf): void
  292. {
  293. $this->pdf = $pdf;
  294. }
  295. public static function isAvailable(): bool
  296. {
  297. return class_exists(TCPDF::class);
  298. }
  299. }