2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Produce a PDF report (export) from a query
12 if (isset($plugin_list)) {
13 $plugin_list['pdf'] = array(
16 'mime_type' => 'application/pdf',
19 array('type' => 'message_only', 'name' => 'explanation', 'text' => 'strPDFReportExplanation'),
20 array('type' => 'text', 'name' => 'report_title', 'text' => 'strPDFReportTitle'),
21 array('type' => 'hidden', 'name' => 'data'),
23 'options_text' => 'strOptions',
30 * @todo Make this configuratble (at least Sans/Serif).
32 define('PMA_PDF_FONT', 'DejaVuSans');
33 require_once './libraries/tcpdf/tcpdf.php';
35 // Adapted from a LGPL script by Philip Clarke
37 class PMA_PDF
extends TCPDF
43 // overloading of a tcpdf function:
44 function _beginpage($orientation)
47 // solved the problem of overwriting a page, if it already exists
48 if (!isset($this->pages
[$this->page
])) {
49 $this->pages
[$this->page
] = '';
52 $this->x
= $this->lMargin
;
53 $this->y
= $this->tMargin
;
55 $this->FontFamily
= '';
59 $orientation = $this->DefOrientation
;
61 $orientation = strtoupper($orientation{0});
62 if ($orientation != $this->DefOrientation
) {
63 $this->OrientationChanges
[$this->page
] = true;
66 if ($orientation != $this->CurOrientation
) {
68 if ($orientation == 'P') {
69 $this->wPt
= $this->fwPt
;
70 $this->hPt
= $this->fhPt
;
74 $this->wPt
= $this->fhPt
;
75 $this->hPt
= $this->fwPt
;
79 $this->PageBreakTrigger
= $this->h
- $this->bMargin
;
80 $this->CurOrientation
= $orientation;
88 // Check if header for this page already exists
89 if (!isset($this->headerset
[$this->page
])) {
91 foreach ($this->tablewidths
as $width) {
94 $this->SetY(($this->tMargin
) - ($this->FontSizePt
/$this->k
)*2);
95 $this->cellFontSize
= $this->FontSizePt
;
96 $this->SetFont(PMA_PDF_FONT
, '', ($this->titleFontSize ?
$this->titleFontSize
: $this->FontSizePt
));
97 $this->Cell(0, $this->FontSizePt
, $this->titleText
, 0, 1, 'C');
98 $l = ($this->lMargin
);
99 $this->SetFont(PMA_PDF_FONT
, '', $this->cellFontSize
);
100 foreach ($this->colTitles
as $col => $txt) {
101 $this->SetXY($l, ($this->tMargin
));
102 $this->MultiCell($this->tablewidths
[$col], $this->FontSizePt
, $txt);
103 $l +
= $this->tablewidths
[$col] ;
104 $maxY = ($maxY < $this->getY()) ?
$this->getY() : $maxY ;
106 $this->SetXY($this->lMargin
, $this->tMargin
);
107 $this->setFillColor(200, 200, 200);
108 $l = ($this->lMargin
);
109 foreach ($this->colTitles
as $col => $txt) {
110 $this->SetXY($l, $this->tMargin
);
111 $this->cell($this->tablewidths
[$col], $maxY-($this->tMargin
), '', 1, 0, 'L', 1);
112 $this->SetXY($l, $this->tMargin
);
113 $this->MultiCell($this->tablewidths
[$col], $this->FontSizePt
, $txt, 0, 'C');
114 $l +
= $this->tablewidths
[$col];
116 $this->setFillColor(255, 255, 255);
118 $this->headerset
[$this->page
] = 1;
126 // Check if footer for this page already exists
127 if (!isset($this->footerset
[$this->page
])) {
130 $this->Cell(0, 10, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}', 'T', 0, 'C');
133 $this->footerset
[$this->page
] = 1;
137 function morepagestable($lineheight=8)
139 // some things to set and 'remember'
141 $startheight = $h = $this->GetY();
142 $startpage = $currpage = $this->page
;
144 // calculate the whole width
146 foreach ($this->tablewidths
as $width) {
147 $fullwidth +
= $width;
150 // Now let's start to write the table
152 $tmpheight = array();
155 while ($data = PMA_DBI_fetch_row($this->results
)) {
156 $this->page
= $currpage;
157 // write the horizontal borders
158 $this->Line($l, $h, $fullwidth+
$l, $h);
159 // write the content and remember the height of the highest col
160 foreach ($data as $col => $txt) {
161 $this->page
= $currpage;
162 $this->SetXY($l, $h);
163 if ($this->tablewidths
[$col] > 0) {
164 $this->MultiCell($this->tablewidths
[$col], $lineheight, $txt, 0, $this->colAlign
[$col]);
165 $l +
= $this->tablewidths
[$col];
168 if (!isset($tmpheight[$row.'-'.$this->page
])) {
169 $tmpheight[$row.'-'.$this->page
] = 0;
171 if ($tmpheight[$row.'-'.$this->page
] < $this->GetY()) {
172 $tmpheight[$row.'-'.$this->page
] = $this->GetY();
174 if ($this->page
> $maxpage) {
175 $maxpage = $this->page
;
180 // get the height we were in the last used page
181 $h = $tmpheight[$row.'-'.$maxpage];
182 // set the "pointer" to the left margin
184 // set the $currpage to the last page
185 $currpage = $maxpage;
190 // we start adding a horizontal line on the last page
191 $this->page
= $maxpage;
192 $this->Line($l, $h, $fullwidth+
$l, $h);
193 // now we start at the top of the document and walk down
194 for ($i = $startpage; $i <= $maxpage; $i++
) {
197 $t = ($i == $startpage) ?
$startheight : $this->tMargin
;
198 $lh = ($i == $maxpage) ?
$h : $this->h
-$this->bMargin
;
199 $this->Line($l, $t, $l, $lh);
200 foreach ($this->tablewidths
as $width) {
202 $this->Line($l, $t, $l, $lh);
205 // set it to the last page, if not it'll cause some problems
206 $this->page
= $maxpage;
210 function mysql_report($query, $attr = array())
212 foreach ($attr as $key => $val){
217 * Pass 1 for column widths
219 $this->results
= PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED
);
220 $this->numFields
= PMA_DBI_num_fields($this->results
);
221 $this->fields
= PMA_DBI_get_fields_meta($this->results
);
223 // if column widths not set
224 if (!isset($this->tablewidths
)){
226 // sColWidth = starting col width (an average size width)
227 $availableWidth = $this->w
- $this->lMargin
- $this->rMargin
;
228 $this->sColWidth
= $availableWidth / $this->numFields
;
229 $totalTitleWidth = 0;
231 // loop through results header and set initial col widths/ titles/ alignment
232 // if a col title is less than the starting col width, reduce that column size
233 for ($i = 0; $i < $this->numFields
; $i++
){
234 $stringWidth = $this->getstringwidth($this->fields
[$i]->name
) +
6 ;
235 // save the real title's width
236 $titleWidth[$i] = $stringWidth;
237 $totalTitleWidth +
= $stringWidth;
239 // set any column titles less than the start width to the column title width
240 if ($stringWidth < $this->sColWidth
){
241 $colFits[$i] = $stringWidth ;
243 $this->colTitles
[$i] = $this->fields
[$i]->name
;
244 $this->display_column
[$i] = true;
246 switch ($this->fields
[$i]->type
){
248 $this->colAlign
[$i] = 'R';
255 * @todo do not deactivate completely the display
256 * but show the field's name and [BLOB]
258 if (stristr($this->fields
[$i]->flags
, 'BINARY')) {
259 $this->display_column
[$i] = false;
260 unset($this->colTitles
[$i]);
262 $this->colAlign
[$i] = 'L';
265 $this->colAlign
[$i] = 'L';
269 // title width verification
270 if ($totalTitleWidth > $availableWidth) {
271 $adjustingMode = true;
273 $adjustingMode = false;
274 // we have enough space for all the titles at their
275 // original width so use the true title's width
276 foreach ($titleWidth as $key => $val) {
277 $colFits[$key] = $val;
281 // loop through the data, any column whose contents is bigger
282 // than the col size is resized
284 * @todo force here a LIMIT to avoid reading all rows
286 while ($row = PMA_DBI_fetch_row($this->results
)) {
287 foreach ($colFits as $key => $val) {
288 $stringWidth = $this->getstringwidth($row[$key]) +
6 ;
289 if ($adjustingMode && ($stringWidth > $this->sColWidth
)) {
290 // any column whose data's width is bigger than the start width is now discarded
291 unset($colFits[$key]);
293 // if data's width is bigger than the current column width,
294 // enlarge the column (but avoid enlarging it if the
295 // data's width is very big)
296 if ($stringWidth > $val && $stringWidth < ($this->sColWidth
* 3)) {
297 $colFits[$key] = $stringWidth ;
303 $totAlreadyFitted = 0;
304 foreach ($colFits as $key => $val){
305 // set fitted columns to smallest size
306 $this->tablewidths
[$key] = $val;
307 // to work out how much (if any) space has been freed up
308 $totAlreadyFitted +
= $val;
311 if ($adjustingMode) {
312 $surplus = (sizeof($colFits) * $this->sColWidth
) - $totAlreadyFitted;
313 $surplusToAdd = $surplus / ($this->numFields
- sizeof($colFits));
318 for ($i=0; $i < $this->numFields
; $i++
) {
319 if (!in_array($i, array_keys($colFits))) {
320 $this->tablewidths
[$i] = $this->sColWidth +
$surplusToAdd;
322 if ($this->display_column
[$i] == false) {
323 $this->tablewidths
[$i] = 0;
327 ksort($this->tablewidths
);
329 PMA_DBI_free_result($this->results
);
333 $this->results
= PMA_DBI_query($query, null, PMA_DBI_QUERY_UNBUFFERED
);
335 $this->setY($this->tMargin
);
337 $this->morepagestable($this->FontSizePt
);
338 PMA_DBI_free_result($this->results
);
340 } // end of mysql_report function
342 } // end of PMA_PDF class
347 * @param string Text of comment
349 * @return bool Whether it suceeded
351 function PMA_exportComment($text)
357 * Outputs export footer
359 * @return bool Whether it suceeded
363 function PMA_exportFooter()
369 * Outputs export header
371 * @return bool Whether it suceeded
375 function PMA_exportHeader()
381 * Outputs database header
383 * @param string Database name
385 * @return bool Whether it suceeded
389 function PMA_exportDBHeader($db)
395 * Outputs database footer
397 * @param string Database name
399 * @return bool Whether it suceeded
403 function PMA_exportDBFooter($db)
409 * Outputs create database database
411 * @param string Database name
413 * @return bool Whether it suceeded
417 function PMA_exportDBCreate($db)
423 * Outputs the content of a table in PDF format
425 * @todo user-defined page orientation, paper size
426 * @param string the database name
427 * @param string the table name
428 * @param string the end of line sequence
429 * @param string the url to go back in case of error
430 * @param string SQL query for obtaining data
432 * @return bool Whether it suceeded
436 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
439 global $pdf_report_title;
441 $pdf = new PMA_PDF('L', 'pt', 'A3');
443 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
444 $pdf->AddFont('DejaVuSans', 'B', 'dejavusans-bold.php');
445 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
446 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserif-bold.php');
447 $pdf->SetFont(PMA_PDF_FONT
, '', 11.5);
448 $pdf->AliasNbPages();
449 $attr=array('titleFontSize' => 18, 'titleText' => $pdf_report_title);
450 $pdf->mysql_report($sql_query, $attr);
452 // instead of $pdf->Output():
453 if ($pdf->state
< 3) {
456 if (!PMA_exportOutputHandler($pdf->buffer
)) {
461 } // end of the 'PMA_exportData()' function