<?php
include("common_db.php");
include("functions.php");
dbconnect($host, $username, $password);
$strings = loadStrings($lang, 'VRPDF');
include("pdf/phppdflib.class.php");
//if(isset($_REQUEST['paper_size'])) { //disabled
// $papersize = $_REQUEST['paper_size'];
//} else {
$papersize = 'a4'; //default to a4
//}
$paperheight = 841; //210 mm (A4) == 595.275 points
$paperwidth = 595; //297 mm (A4) == 841.889 points
$sidemargin = 70; //margin to the left and right
$upmargin = 70; //margin to the top and bottom
$tablemargin = 3; //margin between table columns
if(isset($_REQUEST['report_id'])) {
$report_id = intval($_REQUEST['report_id']);
} else {
//todo: panic
die('panic');
}
//Get the report from the database
mysql_select_db('HypatiaDB');
$report_details = mysql_fetch_array(mysql_query("SELECT * FROM reports WHERE id = '$report_id'"));
$pdf = new pdffile();
$pdf->set_default('margin', 0);
$page = $pdf->new_page($papersize);
$pagearray[] = $page;
//draw_text takes, unfortunately enough, args as (left, bottom, text, page, params)
$pdf->draw_text($sidemargin, $paperheight - ($upmargin), $report_details['title'], $page, array('height' => 24, 'margin' => 0, 'font' => 'Times-Roman'));
//$pdf->draw_text(0, 0, $report_details['title'], $page);
if($pdf->pop_error($num, $msg)) {
error_log("pdf:$num:$mdg");
}
//now we need to draw the table
$sql = buildReportSQL($report_id);
mysql_select_db($report_details['db']);
$results = mysql_query($sql);
//There's no way to actually tell how wide a table column should be
//So, we loop through the result array and find the longest column,
//and then set the length to that.
for($i = 0; $i < mysql_num_fields($results); $i++) {
$len = $pdf->strlen(mysql_field_name($results, $i));
$row_lengths[$i] = $len;
}
while($row = mysql_fetch_row($results)) {
$rows[] = $row;
for($i = 0; $i < count($row); $i++) {
$len = $pdf->strlen($row[$i]);
if($len > $row_lengths[$i]) {
$row_lengths[$i] = $len;
}
}
}
//Now we move on to drawing
//The current y-position of the table draw is stored in the variable
//$position. This variable is passed to the appropriate functions,
//which return the new value for $position (based on how much y-space
//the row/header takes up)
//Like draw_text, position is the distance from the bottom of the page.
$position = $paperheight - ($upmargin + 24 + 5); //24 - header, 5 - margin
$position = drawTableHeader($page, $results, $row_lengths, $position);
$banded = false;
$pages = 1;
for($o = 0; $o < count($rows); $o++) {
$position = drawTableRow($page, $rows[$o], $row_lengths, $position, $banded);
$banded = !$banded;
//check if we need a new page
if($position < $upmargin) {
$page = $pdf->new_page($papersize);
$position = $paperheight - $upmargin;
$pages++;
$pagearray[] = $page;
}
}
function drawTableHeader($page, $results, $lengths, $position) {
global $pdf, $sidemargin, $tablemargin;
$margin = $sidemargin;
$position -= 12; //row height
for($i = 0; $i < mysql_num_fields($results); $i++) {
$pdf->draw_text($margin, $position, mysql_field_name($results, $i), $page);
$margin += $lengths[$i] + $tablemargin;
}
$position -= 1; //space for descenders
//draw a line
$x = array($sidemargin, $margin);
$y = array($position, $position);
$pdf->draw_line($x, $y, $page);
return $position - 2; //row height is 12 + 3 margin (the 12 + 1 is subtracted above)
}
function drawTableRow($page, $row, $lengths, $position, $banded) {
global $pdf, $sidemargin, $tablemargin;
$margin = $sidemargin;
$total_margin = $margin;
foreach($lengths as $l)
$total_margin += ($l + $tablemargin);
if($banded) {
//top, left, bottom, right, page, params
if(!$pdf->draw_line(array($margin, $total_margin), array($position - 7.5, $position - 7.5), $page, array('width' => 13, 'strokecolor' => array('red' => 0.9, 'green' => 0.9, 'blue' => 0.93)/*, 'fillcolor' => array('red' => 1, 'green' => 0, 'blue' => 0)*/))) {
$pdf->pop_error($num, $msg);
error_log("pdf:$num:$msg");
}
}
for($i = 0; $i < count($row); $i++) {
$pdf->draw_text($margin, $position - 12, $row[$i], $page);
$margin += $lengths[$i] + $tablemargin;
}
return $position - 13; //row height is 12 + 1 spacing
}
function drawFooter($page, $total, $current) {
global $pdf, $report_details, $paperwidth, $strings;
$str = sprintf($strings['VRP_FOOTER'], $report_details['title'], $current, $total);
$len = $pdf->strlen($str);
$left = ($paperwidth / 2) - ($len / 2);
$pdf->draw_text($left, 5, $str, $page, array('height' => 10));
}
//Now, we draw the footer and so forth
$cpage = 1;
foreach($pagearray as $page) {
drawFooter($page, $pages, $cpage);
$cpage++;
}
$output = $pdf->generate();
header("Content-Type: application/pdf");
header("Content-Length: " . strlen($output));
echo $output;
?>