3 // vim: expandtab sw=4 ts=4 sts=4:
6 * Produce a PDF report (export) from a query
9 if (isset($plugin_list)) {
10 $plugin_list['pdf'] = array(
13 'mime_type' => 'application/pdf',
16 array('type' => 'message_only', 'name' => 'explanation', 'text' => 'strPDFReportExplanation'),
17 array('type' => 'text', 'name' => 'report_title', 'text' => 'strPDFReportTitle'),
18 array('type' => 'hidden', 'name' => 'data'),
20 'options_text' => 'strPDFOptions',
27 * @todo Make this configuratble (at least Sans/Serif).
29 define('PMA_PDF_FONT', 'DejaVuSans');
30 require_once('./libraries/tcpdf/tcpdf.php');
32 // Adapted from a LGPL script by Philip Clarke
34 class PMA_PDF
extends TCPDF
40 // overloading of a tcpdf function:
41 function _beginpage($orientation)
44 // solved the problem of overwriting a page, if it already exists
45 if (!isset($this->pages
[$this->page
])) {
46 $this->pages
[$this->page
] = '';
49 $this->x
= $this->lMargin
;
50 $this->y
= $this->tMargin
;
52 $this->FontFamily
= '';
56 $orientation = $this->DefOrientation
;
58 $orientation = strtoupper($orientation{0});
59 if ($orientation != $this->DefOrientation
) {
60 $this->OrientationChanges
[$this->page
] = true;
63 if ($orientation != $this->CurOrientation
) {
65 if ($orientation == 'P') {
66 $this->wPt
= $this->fwPt
;
67 $this->hPt
= $this->fhPt
;
71 $this->wPt
= $this->fhPt
;
72 $this->hPt
= $this->fwPt
;
76 $this->PageBreakTrigger
= $this->h
- $this->bMargin
;
77 $this->CurOrientation
= $orientation;
85 // Check if header for this page already exists
86 if (!isset($this->headerset
[$this->page
])) {
88 foreach ($this->tablewidths
as $width) {
91 $this->SetY(($this->tMargin
) - ($this->FontSizePt
/$this->k
)*2);
92 $this->cellFontSize
= $this->FontSizePt
;
93 $this->SetFont(PMA_PDF_FONT
, '', ($this->titleFontSize ?
$this->titleFontSize
: $this->FontSizePt
));
94 $this->Cell(0, $this->FontSizePt
, $this->titleText
, 0, 1, 'C');
95 $l = ($this->lMargin
);
96 $this->SetFont(PMA_PDF_FONT
, '', $this->cellFontSize
);
97 foreach ($this->colTitles
as $col => $txt) {
98 $this->SetXY($l, ($this->tMargin
));
99 $this->MultiCell($this->tablewidths
[$col], $this->FontSizePt
, $txt);
100 $l +
= $this->tablewidths
[$col] ;
101 $maxY = ($maxY < $this->getY()) ?
$this->getY() : $maxY ;
103 $this->SetXY($this->lMargin
, $this->tMargin
);
104 $this->setFillColor(200, 200, 200);
105 $l = ($this->lMargin
);
106 foreach ($this->colTitles
as $col => $txt) {
107 $this->SetXY($l, $this->tMargin
);
108 $this->cell($this->tablewidths
[$col], $maxY-($this->tMargin
), '', 1, 0, 'L', 1);
109 $this->SetXY($l, $this->tMargin
);
110 $this->MultiCell($this->tablewidths
[$col], $this->FontSizePt
, $txt, 0, 'C');
111 $l +
= $this->tablewidths
[$col];
113 $this->setFillColor(255, 255, 255);
115 $this->headerset
[$this->page
] = 1;
123 // Check if footer for this page already exists
124 if (!isset($this->footerset
[$this->page
])) {
127 $this->Cell(0, 10, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}', 'T', 0, 'C');
130 $this->footerset
[$this->page
] = 1;
134 function morepagestable($lineheight=8)
136 // some things to set and 'remember'
138 $startheight = $h = $this->GetY();
139 $startpage = $currpage = $this->page
;
141 // calculate the whole width
143 foreach ($this->tablewidths
as $width) {
144 $fullwidth +
= $width;
147 // Now let's start to write the table
149 $tmpheight = array();
152 while ($data = PMA_DBI_fetch_row($this->results
)) {
153 $this->page
= $currpage;
154 // write the horizontal borders
155 $this->Line($l, $h, $fullwidth+
$l, $h);
156 // write the content and remember the height of the highest col
157 foreach ($data as $col => $txt) {
158 $this->page
= $currpage;
159 $this->SetXY($l, $h);
160 if ($this->tablewidths
[$col] > 0) {
161 $this->MultiCell($this->tablewidths
[$col], $lineheight, $txt, 0, $this->colAlign
[$col]);
162 $l +
= $this->tablewidths
[$col];
165 if (!isset($tmpheight[$row.'-'.$this->page
])) {
166 $tmpheight[$row.'-'.$this->page
] = 0;
168 if ($tmpheight[$row.'-'.$this->page
] < $this->GetY()) {
169 $tmpheight[$row.'-'.$this->page
] = $this->GetY();
171 if ($this->page
> $maxpage) {
172 $maxpage = $this->page
;
177 // get the height we were in the last used page
178 $h = $tmpheight[$row.'-'.$maxpage];
179 // set the "pointer" to the left margin
181 // set the $currpage to the last page
182 $currpage = $maxpage;
187 // we start adding a horizontal line on the last page
188 $this->page
= $maxpage;
189 $this->Line($l, $h, $fullwidth+
$l, $h);
190 // now we start at the top of the document and walk down
191 for ($i = $startpage; $i <= $maxpage; $i++
) {
194 $t = ($i == $startpage) ?
$startheight : $this->tMargin
;
195 $lh = ($i == $maxpage) ?
$h : $this->h
-$this->bMargin
;
196 $this->Line($l, $t, $l, $lh);
197 foreach ($this->tablewidths
as $width) {
199 $this->Line($l, $t, $l, $lh);
202 // set it to the last page, if not it'll cause some problems
203 $this->page
= $maxpage;
207 function mysql_report($query, $attr = array())
209 foreach ($attr as $key => $val){
214 * Pass 1 for column widths
215 * @todo force here a LIMIT to speed up pass 1 ?
217 $this->results
= PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED
);
218 $this->numFields
= PMA_DBI_num_fields($this->results
);
219 $this->fields
= PMA_DBI_get_fields_meta($this->results
);
221 // if column widths not set
222 if (!isset($this->tablewidths
)){
224 // starting col width
225 $this->sColWidth
= ($this->w
- $this->lMargin
- $this->rMargin
) / $this->numFields
;
227 // loop through results header and set initial col widths/ titles/ alignment
228 // if a col title is less than the starting col width / reduce that column size
229 for ($i=0; $i < $this->numFields
; $i++
){
230 $stringWidth = $this->getstringwidth($this->fields
[$i]->name
) +
6 ;
231 // set any column titles less than the start width to the column title width
232 if (($stringWidth) < $this->sColWidth
){
233 $colFits[$i] = $stringWidth ;
235 $this->colTitles
[$i] = $this->fields
[$i]->name
;
236 $this->display_column
[$i] = true;
238 switch ($this->fields
[$i]->type
){
240 $this->colAlign
[$i] = 'R';
247 * @todo do not deactivate completely the display
248 * but show the field's name and [BLOB]
250 if (stristr($this->fields
[$i]->flags
, 'BINARY')) {
251 $this->display_column
[$i] = false;
252 unset($this->colTitles
[$i]);
254 $this->colAlign
[$i] = 'L';
257 $this->colAlign
[$i] = 'L';
261 // loop through the data, any column whose contents is bigger
262 // than the col size is resized
263 // TODO: force here a LIMIT to avoid reading all rows
264 while ($row = PMA_DBI_fetch_row($this->results
)) {
265 foreach ($colFits as $key => $val) {
266 $stringWidth = $this->getstringwidth($row[$key]) +
6 ;
267 if ($stringWidth > $this->sColWidth
) {
268 // any col where row is bigger than the start width is now discarded
269 unset($colFits[$key]);
271 // if text is not bigger than the current column width setting enlarge the column
272 if ($stringWidth > $val) {
273 $colFits[$key] = $stringWidth ;
279 $totAlreadyFitted = 0;
280 foreach ($colFits as $key => $val){
281 // set fitted columns to smallest size
282 $this->tablewidths
[$key] = $val;
283 // to work out how much (if any) space has been freed up
284 $totAlreadyFitted +
= $val;
287 $surplus = (sizeof($colFits) * $this->sColWidth
) - $totAlreadyFitted;
288 for ($i=0; $i < $this->numFields
; $i++
) {
289 if (!in_array($i, array_keys($colFits))) {
290 $this->tablewidths
[$i] = $this->sColWidth +
($surplus / ($this->numFields
- sizeof($colFits)));
292 if ($this->display_column
[$i] == false) {
293 $this->tablewidths
[$i] = 0;
297 ksort($this->tablewidths
);
300 PMA_DBI_free_result($this->results
);
304 $this->results
= PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED
);
306 $this->setY($this->tMargin
);
308 $this->morepagestable($this->FontSizePt
);
309 PMA_DBI_free_result($this->results
);
311 } // end of mysql_report function
313 } // end of PMA_PDF class
318 * @param string Text of comment
320 * @return bool Whether it suceeded
322 function PMA_exportComment($text)
328 * Outputs export footer
330 * @return bool Whether it suceeded
334 function PMA_exportFooter()
340 * Outputs export header
342 * @return bool Whether it suceeded
346 function PMA_exportHeader()
352 * Outputs database header
354 * @param string Database name
356 * @return bool Whether it suceeded
360 function PMA_exportDBHeader($db)
366 * Outputs database footer
368 * @param string Database name
370 * @return bool Whether it suceeded
374 function PMA_exportDBFooter($db)
380 * Outputs create database database
382 * @param string Database name
384 * @return bool Whether it suceeded
388 function PMA_exportDBCreate($db)
394 * Outputs the content of a table in PDF format
396 * @todo user-defined page orientation, paper size
397 * @param string the database name
398 * @param string the table name
399 * @param string the end of line sequence
400 * @param string the url to go back in case of error
401 * @param string SQL query for obtaining data
403 * @return bool Whether it suceeded
407 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
410 global $pdf_report_title;
412 $pdf = new PMA_PDF('L', 'pt', 'A3');
414 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
415 $pdf->AddFont('DejaVuSans', 'B', 'dejavusans-bold.php');
416 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
417 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserif-bold.php');
418 $pdf->SetFont(PMA_PDF_FONT
, '', 11.5);
419 $pdf->AliasNbPages();
420 $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
421 $pdf->mysql_report($sql_query, $attr);
423 // instead of $pdf->Output():
424 if ($pdf->state
< 3) {
427 if (!PMA_exportOutputHandler($pdf->buffer
)) {
432 } // end of the 'PMA_exportData()' function