Housekeeping stuff:
[openemr.git] / phpmyadmin / libraries / export / pdf.php
blob6cc2b902f40a7d181a950562644a2d66e2f68b86
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Produce a PDF report (export) from a query
6 * @version $Id$
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 if (isset($plugin_list)) {
16 $plugin_list['pdf'] = array(
17 'text' => 'strPDF',
18 'extension' => 'pdf',
19 'mime_type' => 'application/pdf',
20 'force_file' => true,
21 'options' => array(
22 array('type' => 'message_only', 'name' => 'explanation', 'text' => 'strPDFReportExplanation'),
23 array('type' => 'text', 'name' => 'report_title', 'text' => 'strPDFReportTitle'),
24 array('type' => 'hidden', 'name' => 'data'),
26 'options_text' => 'strOptions',
28 } else {
30 /**
31 * Font used in PDF.
33 * @todo Make this configuratble (at least Sans/Serif).
35 define('PMA_PDF_FONT', 'DejaVuSans');
36 require_once './libraries/tcpdf/tcpdf.php';
38 // Adapted from a LGPL script by Philip Clarke
40 class PMA_PDF extends TCPDF
42 var $tablewidths;
43 var $headerset;
44 var $footerset;
46 // overloading of a tcpdf function:
47 function _beginpage($orientation)
49 $this->page++;
50 // solved the problem of overwriting a page, if it already exists
51 if (!isset($this->pages[$this->page])) {
52 $this->pages[$this->page] = '';
54 $this->state = 2;
55 $this->x = $this->lMargin;
56 $this->y = $this->tMargin;
57 $this->lasth = 0;
58 $this->FontFamily = '';
60 //Page orientation
61 if (!$orientation) {
62 $orientation = $this->DefOrientation;
63 } else {
64 $orientation = strtoupper($orientation{0});
65 if ($orientation != $this->DefOrientation) {
66 $this->OrientationChanges[$this->page] = true;
69 if ($orientation != $this->CurOrientation) {
70 //Change orientation
71 if ($orientation == 'P') {
72 $this->wPt = $this->fwPt;
73 $this->hPt = $this->fhPt;
74 $this->w = $this->fw;
75 $this->h = $this->fh;
76 } else {
77 $this->wPt = $this->fhPt;
78 $this->hPt = $this->fwPt;
79 $this->w = $this->fh;
80 $this->h = $this->fw;
82 $this->PageBreakTrigger = $this->h - $this->bMargin;
83 $this->CurOrientation = $orientation;
87 function Header()
89 global $maxY;
91 // Check if header for this page already exists
92 if (!isset($this->headerset[$this->page])) {
93 $fullwidth = 0;
94 foreach ($this->tablewidths as $width) {
95 $fullwidth += $width;
97 $this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*2);
98 $this->cellFontSize = $this->FontSizePt ;
99 $this->SetFont(PMA_PDF_FONT, '', ($this->titleFontSize ? $this->titleFontSize : $this->FontSizePt));
100 $this->Cell(0, $this->FontSizePt, $this->titleText, 0, 1, 'C');
101 $l = ($this->lMargin);
102 $this->SetFont(PMA_PDF_FONT, '', $this->cellFontSize);
103 foreach ($this->colTitles as $col => $txt) {
104 $this->SetXY($l, ($this->tMargin));
105 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt);
106 $l += $this->tablewidths[$col] ;
107 $maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
109 $this->SetXY($this->lMargin, $this->tMargin);
110 $this->setFillColor(200, 200, 200);
111 $l = ($this->lMargin);
112 foreach ($this->colTitles as $col => $txt) {
113 $this->SetXY($l, $this->tMargin);
114 $this->cell($this->tablewidths[$col], $maxY-($this->tMargin), '', 1, 0, 'L', 1);
115 $this->SetXY($l, $this->tMargin);
116 $this->MultiCell($this->tablewidths[$col], $this->FontSizePt, $txt, 0, 'C');
117 $l += $this->tablewidths[$col];
119 $this->setFillColor(255, 255, 255);
120 // set headerset
121 $this->headerset[$this->page] = 1;
124 $this->SetY($maxY);
127 function Footer()
129 // Check if footer for this page already exists
130 if (!isset($this->footerset[$this->page])) {
131 $this->SetY(-15);
132 //Page number
133 $this->Cell(0, 10, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}', 'T', 0, 'C');
135 // set footerset
136 $this->footerset[$this->page] = 1;
140 function morepagestable($lineheight=8)
142 // some things to set and 'remember'
143 $l = $this->lMargin;
144 $startheight = $h = $this->GetY();
145 $startpage = $currpage = $this->page;
147 // calculate the whole width
148 $fullwidth = 0;
149 foreach ($this->tablewidths as $width) {
150 $fullwidth += $width;
153 // Now let's start to write the table
154 $row = 0;
155 $tmpheight = array();
156 $maxpage = 0;
158 while ($data = PMA_DBI_fetch_row($this->results)) {
159 $this->page = $currpage;
160 // write the horizontal borders
161 $this->Line($l, $h, $fullwidth+$l, $h);
162 // write the content and remember the height of the highest col
163 foreach ($data as $col => $txt) {
164 $this->page = $currpage;
165 $this->SetXY($l, $h);
166 if ($this->tablewidths[$col] > 0) {
167 $this->MultiCell($this->tablewidths[$col], $lineheight, $txt, 0, $this->colAlign[$col]);
168 $l += $this->tablewidths[$col];
171 if (!isset($tmpheight[$row.'-'.$this->page])) {
172 $tmpheight[$row.'-'.$this->page] = 0;
174 if ($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
175 $tmpheight[$row.'-'.$this->page] = $this->GetY();
177 if ($this->page > $maxpage) {
178 $maxpage = $this->page;
180 unset($data[$col]);
183 // get the height we were in the last used page
184 $h = $tmpheight[$row.'-'.$maxpage];
185 // set the "pointer" to the left margin
186 $l = $this->lMargin;
187 // set the $currpage to the last page
188 $currpage = $maxpage;
189 unset($data[$row]);
190 $row++;
192 // draw the borders
193 // we start adding a horizontal line on the last page
194 $this->page = $maxpage;
195 $this->Line($l, $h, $fullwidth+$l, $h);
196 // now we start at the top of the document and walk down
197 for ($i = $startpage; $i <= $maxpage; $i++) {
198 $this->page = $i;
199 $l = $this->lMargin;
200 $t = ($i == $startpage) ? $startheight : $this->tMargin;
201 $lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
202 $this->Line($l, $t, $l, $lh);
203 foreach ($this->tablewidths as $width) {
204 $l += $width;
205 $this->Line($l, $t, $l, $lh);
208 // set it to the last page, if not it'll cause some problems
209 $this->page = $maxpage;
213 function mysql_report($query, $attr = array())
215 foreach ($attr as $key => $val){
216 $this->$key = $val ;
220 * Pass 1 for column widths
222 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
223 $this->numFields = PMA_DBI_num_fields($this->results);
224 $this->fields = PMA_DBI_get_fields_meta($this->results);
226 // if column widths not set
227 if (!isset($this->tablewidths)){
229 // sColWidth = starting col width (an average size width)
230 $availableWidth = $this->w - $this->lMargin - $this->rMargin;
231 $this->sColWidth = $availableWidth / $this->numFields;
232 $totalTitleWidth = 0;
234 // loop through results header and set initial col widths/ titles/ alignment
235 // if a col title is less than the starting col width, reduce that column size
236 for ($i = 0; $i < $this->numFields; $i++){
237 $stringWidth = $this->getstringwidth($this->fields[$i]->name) + 6 ;
238 // save the real title's width
239 $titleWidth[$i] = $stringWidth;
240 $totalTitleWidth += $stringWidth;
242 // set any column titles less than the start width to the column title width
243 if ($stringWidth < $this->sColWidth){
244 $colFits[$i] = $stringWidth ;
246 $this->colTitles[$i] = $this->fields[$i]->name;
247 $this->display_column[$i] = true;
249 switch ($this->fields[$i]->type){
250 case 'int':
251 $this->colAlign[$i] = 'R';
252 break;
253 case 'blob':
254 case 'tinyblob':
255 case 'mediumblob':
256 case 'longblob':
258 * @todo do not deactivate completely the display
259 * but show the field's name and [BLOB]
261 if (stristr($this->fields[$i]->flags, 'BINARY')) {
262 $this->display_column[$i] = false;
263 unset($this->colTitles[$i]);
265 $this->colAlign[$i] = 'L';
266 break;
267 default:
268 $this->colAlign[$i] = 'L';
272 // title width verification
273 if ($totalTitleWidth > $availableWidth) {
274 $adjustingMode = true;
275 } else {
276 $adjustingMode = false;
277 // we have enough space for all the titles at their
278 // original width so use the true title's width
279 foreach ($titleWidth as $key => $val) {
280 $colFits[$key] = $val;
284 // loop through the data, any column whose contents is bigger
285 // than the col size is resized
287 * @todo force here a LIMIT to avoid reading all rows
289 while ($row = PMA_DBI_fetch_row($this->results)) {
290 foreach ($colFits as $key => $val) {
291 $stringWidth = $this->getstringwidth($row[$key]) + 6 ;
292 if ($adjustingMode && ($stringWidth > $this->sColWidth)) {
293 // any column whose data's width is bigger than the start width is now discarded
294 unset($colFits[$key]);
295 } else {
296 // if data's width is bigger than the current column width,
297 // enlarge the column (but avoid enlarging it if the
298 // data's width is very big)
299 if ($stringWidth > $val && $stringWidth < ($this->sColWidth * 3)) {
300 $colFits[$key] = $stringWidth ;
306 $totAlreadyFitted = 0;
307 foreach ($colFits as $key => $val){
308 // set fitted columns to smallest size
309 $this->tablewidths[$key] = $val;
310 // to work out how much (if any) space has been freed up
311 $totAlreadyFitted += $val;
314 if ($adjustingMode) {
315 $surplus = (sizeof($colFits) * $this->sColWidth) - $totAlreadyFitted;
316 $surplusToAdd = $surplus / ($this->numFields - sizeof($colFits));
317 } else {
318 $surplusToAdd = 0;
321 for ($i=0; $i < $this->numFields; $i++) {
322 if (!in_array($i, array_keys($colFits))) {
323 $this->tablewidths[$i] = $this->sColWidth + $surplusToAdd;
325 if ($this->display_column[$i] == false) {
326 $this->tablewidths[$i] = 0;
330 ksort($this->tablewidths);
332 PMA_DBI_free_result($this->results);
334 // Pass 2
336 $this->results = PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED);
337 $this->Open();
338 $this->setY($this->tMargin);
339 $this->AddPage();
340 $this->morepagestable($this->FontSizePt);
341 PMA_DBI_free_result($this->results);
343 } // end of mysql_report function
345 } // end of PMA_PDF class
348 * Outputs comment
350 * @param string Text of comment
352 * @return bool Whether it suceeded
354 function PMA_exportComment($text)
356 return TRUE;
360 * Outputs export footer
362 * @return bool Whether it suceeded
364 * @access public
366 function PMA_exportFooter()
368 return TRUE;
372 * Outputs export header
374 * @return bool Whether it suceeded
376 * @access public
378 function PMA_exportHeader()
380 return TRUE;
384 * Outputs database header
386 * @param string Database name
388 * @return bool Whether it suceeded
390 * @access public
392 function PMA_exportDBHeader($db)
394 return TRUE;
398 * Outputs database footer
400 * @param string Database name
402 * @return bool Whether it suceeded
404 * @access public
406 function PMA_exportDBFooter($db)
408 return TRUE;
412 * Outputs create database database
414 * @param string Database name
416 * @return bool Whether it suceeded
418 * @access public
420 function PMA_exportDBCreate($db)
422 return TRUE;
426 * Outputs the content of a table in PDF format
428 * @todo user-defined page orientation, paper size
429 * @param string the database name
430 * @param string the table name
431 * @param string the end of line sequence
432 * @param string the url to go back in case of error
433 * @param string SQL query for obtaining data
435 * @return bool Whether it suceeded
437 * @access public
439 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
441 global $what;
442 global $pdf_report_title;
444 $pdf = new PMA_PDF('L', 'pt', 'A3');
446 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
447 $pdf->AddFont('DejaVuSans', 'B', 'dejavusans-bold.php');
448 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
449 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserif-bold.php');
450 $pdf->SetFont(PMA_PDF_FONT, '', 11.5);
451 $pdf->AliasNbPages();
452 $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
453 $pdf->mysql_report($sql_query, $attr);
455 // instead of $pdf->Output():
456 if ($pdf->state < 3) {
457 $pdf->Close();
459 if (!PMA_exportOutputHandler($pdf->buffer)) {
460 return FALSE;
463 return TRUE;
464 } // end of the 'PMA_exportData()' function