Friday, August 12, 2011

PDFlib & PHP - (6) Tables

Adding a TOC in my last post brought me face-to-face with an issue with alignment/control. It would seem that a table would fix that. Trying to tack-in some table code to the existing document isn't working though, so I figure I'm going to have to start small.

The PDFlib "starter_table" is a bit beyond starter. So, I'm going to make the real starter table. I cut the middle out from the first setfont to the last end_page... and added the code that simply makes a table there. Progressively I want to add options and understand what they are (all about the OPTLIST).


A Really Very Simple Table



$p->begin_page_ext($doc_width, $doc_height, "");
if ($p->setfont($font, 24) == 0)
throw new Exception("Error@setfont: " . $p->get_errmsg());
###### TABLE TIME ###########
$table=0; $rowmax = 50;
# colmax used in colspan
$colmax = 5;
#llx = lower left x, whereas ury = upper right y
$llx= 50; $lly=50; $urx=550; $ury=800;
$row = 1; $col = 1;
$content = "I AM THE REAL STARTER TABLE!";
###### add_table optlist. add table cell. table no longer is zero.
# not setting the colwidth here results in warning.
$optlist = "fittextline={position=center font=" . $font .
" fontsize=14} colwidth=400 ";
$table = $p->add_table_cell($table, $col, $row, $content, $optlist);
###### fit_table optlist. time to place table.
# add a stroke so the table is visibly defined for us.
$optlist = "stroke={{line=other}}";
$result = $p->fit_table($table, $llx, $lly, $urx, $ury, $optlist);
if ($result == "_error") {
die("Couldn't place table: " . PDF_get_errmsg($p));
}
##############################
$p->end_page_ext("");

I want to make it layout a multiplication table where the cells are all exactly the same size and the numbers are all centered.


Okay. I have learned a few things. First of all, if your cell-text is large enough it doesn't matter if you have set the option colwidth to try and constraint the text, when adding like this. This will die/delete the PDF-in-the-making. Secondly, defining a rowheight is advisable.


Multiplication Table


This would just replace the above "TABLE TIME" commented bit.



###### TABLE TIME ###########
$table=0;
$num_columns = 12;
$num_rows = 12;
#llx = lower left x, whereas ury = upper right y
$llx= 50; $lly=50; $urx=550; $ury=800;
###### add_table optlist.
$optlist = "fittextline={position=center font=" . $font .
" fontsize=14} colwidth=50 rowheight=20";
// colspan=" . $num_columns;
# make table-grid of 12x12
$col_num = 0;
do{
$col_num++;
$row_num = 0;
do{
$row_num++;
# just fun with background color... 0.0 - 1
$r = $row_num/$num_rows;
$g = $col_num/$num_columns;
$b = 0.5;
$optlist .= " matchbox={fillcolor={rgb $r $g $b}}";
# end fun :\
$value = $col_num*$row_num;
$table = $p->add_table_cell($table, $col_num, $row_num, $value, $optlist);
} while($row_num < $num_rows);
} while ($col_num < $num_columns);
###### fit_table optlist. time to place table.
# header says: this is or isn't a header. if set to 1 the table instance
# will expect more row. since this is just to show a table on the page...
$optlist = "stroke={{line=other}}";
$result = $p->fit_table($table, $llx, $lly, $urx, $ury, $optlist);
if ($result == "_error") {
die("Couldn't place table: " . PDF_get_errmsg($p));
}
##############################

Paging the Multiplication Table


This is pastable over same lines from above example. It starts with the first three lines synching with the last three lines of the above. That is this is over/and after that to the end_page_ext. We've started the table already, and "fit" an added cell. NOTE: Comment out the old final end_page_ext as shown.

To see it work:


  1. Change $num_rows to greater than 37.


$result = $p->fit_table($table, $llx, $lly, $urx, $ury, $optlist);
if ($result == "_error") {
die("Couldn't place table: " . PDF_get_errmsg($p));
}
# states of result of fit_table...
# I'm not finding this in the documentation, yet.
$page_num = 1;
if($result == "_boxfull"){
if ($p->setfont($font, 12) == 0)
throw new Exception("Error@setfont: " . $p->get_errmsg());
$message = "Page $page_num";
$message_width = $p->stringwidth($message, $font, 12);
# cause to right-align in bottom corner.
$page_num_x = ($doc_width-$message_width)-24;
$page_num_y = $doc_height-($doc_height-24);
$p->show_xy($message, $page_num_x, $page_num_y);
$p->end_page_ext("");
do{
$p->begin_page_ext($doc_width, $doc_height, "");
$page_num++;
if ($p->setfont($font, 24) == 0)
throw new Exception("Error@setfont: " . $p->get_errmsg());
$result = $p->fit_table($table, $llx, $lly, $urx, $ury, $optlist);
if ($result == "_error") {
die("Couldn't place table: " . PDF_get_errmsg($p));
}
if ($p->setfont($font, 12) == 0)
throw new Exception("Error@setfont: " . $p->get_errmsg());
$message = "Page $page_num";
$message_width = $p->stringwidth($message, $font, 12);
# cause to right-align in bottom corner.
$page_num_x = ($doc_width-$message_width)-24;
$page_num_y = $doc_height-($doc_height-24);
$p->show_xy($message, $page_num_x, $page_num_y);
$p->end_page_ext("");
}while ($result == "_boxfull");
}else{
$p->end_page_ext("");
}
##############################
// $p->end_page_ext("");

No comments: