I have yet another issue with creating a PDF.
I wrote a generic function which will create a table.
// to set up a table one needs to supply:
// the font detailss for
// the header and the table body (font, fontsize, textPosition)
//
// the multidimensional array to hold info about the header
// headerText, columnWidth
// for advanced users
// extra seetings can be added to each column as a key=>value
// pair in the 'additional' array
//
// table body requires a multidimensional array that will hold
// an array of field values for every row
//
// coordinates for the lower left and upper right corners
// lowerLeftX, lowerLeftY and upperRightX, upperRightY
protected function displayTable(
$headerFont=array('fontName'=>'', 'fontEncoding'=>'', 'fontSize'=>''),
$headers=array( array('headerText'=>'', 'columnWidth'=>'', 'textPosition'=>'',
'additional'=>array()),
array('headerText'=>'', 'columnWidth'=>'', 'textPosition'=>'',
'additional'=>array())),
$fieldsFont=array('fontName'=>'', 'fontEncoding'=>'', 'fontSize'=>''),
$fields=array(array(
array('value'=>'fieldValue', 'textPosition'=>''),
array('value'=>'fieldValue', 'textPosition'=>'')
)),
$lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY,
$tableOptions = "header=1 rowheightdefault=auto"){
$table = 0;
$headFont = 0;
//generate headers
$row = 1;
$headFont = pdf_load_font($this->pdf, $headerFont['fontName'], $headerFont['fontEncoding'], "");
if ($headFont == 0) {
die("Error: " . pdf_get_errmsg($this->pdf));
}
$col = 0;
foreach ($headers as $header){
$col++;
$optionList = "fittextline={position=".$header['textPosition']."
font=" .$headFont. "
fontsize=".$headerFont['fontSize']."}
colwidth=".$header['columnWidth'];
if ((isset($header['additional'])) && (!empty($header['additional']))){
foreach ($header['additional'] as $key=>$value){
$optionList.=" ".$key."=".$value;
}
}
$table = pdf_add_table_cell($this->pdf,$table, $col, $row, $header['headerText'], $optionList);
if ($table == 0) {
die("Error: " . pdf_get_errmsg($this->pdf));
}
}
//the rest of the fields
$bodyFont = pdf_load_font($this->pdf, $fieldsFont['fontName'], $fieldsFont['fontEncoding'], "");
if ($bodyFont == 0) {
die("Error: " . pdf_get_errmsg($this->pdf));
}
foreach ($fields as $line){
$row ++;
$col = 0;
foreach($line as $field){
$col++;
$optionList = "fittextline={position=".$field['textPosition']."
font=" .$bodyFont. "
fontsize=".$fieldsFont['fontSize']."} ";
$table = pdf_add_table_cell($this->pdf,$table, $col, $row, $field['value'], $optionList);
}
if ($table == 0) {
die("Error: " . pdf_get_errmsg($this->pdf));
}
}
// Place the table instance
$result = pdf_fit_table($this->pdf, $table, $lowerLeftX, $lowerLeftY,
$upperRightX, $upperRightY, $tableOptions);
if ($result == "_error") {
die("Couldn't place table: " . pdf_get_errmsg($pdf));
}
pdf_delete_table($this->pdf, $table, "");
}
I have made some changes since I updated the top comments but the function is rather straight forward (if you can say that about PDFLib related functions :'-( ) and generally does follow the comments
I need a text in one of the fields to be aligned to the right
When I call that function in a different class and subsequently display it on the page, the field that I set up an alignment to the right jumps slightly up and goes out of vertical alignment. I am absolutely baffled by that.
Here's the code for calling the function
//set up a table
parent::displayTable(
$headerFont=array('fontName'=>'ZEISSFrutigerNextW1G-BoldIt', 'fontEncoding'=>'unicode',
'fontSize'=>'9'),
$headers=array( array('headerText'=>'Artikelnummeb', 'columnWidth'=>60
, 'textPosition'=>'left', 'additional'=>array('margin'=>4)),
array('headerText'=>'Artikelbezeichnung', 'columnWidth'=>230
, 'textPosition'=>'left', 'additional'=>array('margin'=>4)),
array('headerText'=>'EAN', 'columnWidth'=>70
, 'textPosition'=>'left', 'additional'=>array('margin'=>2)),
array('headerText'=>'Preis (brutto)', 'columnWidth'=>65
, 'textPosition'=>'left', 'additional'=>array('margin'=>0))),
$fieldsFont=array('fontName'=>'ZEISSFrutigerNextW1G-Light', 'fontEncoding'=>'unicode',
'fontSize'=>'9'),
$fields=array( array(array('value'=>'01234567890123', 'textPosition'=>'left'),
array('value'=>'fieldValue', 'textPosition'=>'left'),
array('value'=>'8888888888888', 'textPosition'=>'left'),
array('value'=>'99999.99 Eur', 'textPosition'=>'right')),
array(array('value'=>'01234567890123', 'textPosition'=>'left'),
array('value'=>'fieldValue', 'textPosition'=>'left'),
array('value'=>'8888888888888', 'textPosition'=>'left'),
array('value'=>'99999.99 Eur', 'textPosition'=>'right')),
array(array('value'=>'01234567890123', 'textPosition'=>'left'),
array('value'=>'fieldValue', 'textPosition'=>'left'),
array('value'=>'8888888888888', 'textPosition'=>'left'),
array('value'=>'99999.99 Eur', 'textPosition'=>'right')),
array(array('value'=>'01234567890123', 'textPosition'=>'left'),
array('value'=>'fieldValue', 'textPosition'=>'left'),
array('value'=>'8888888888888', 'textPosition'=>'left'),
array('value'=>'99999.99 Eur', 'textPosition'=>'right'))),
111, 50, 551, 400,
$tableOptions = "header=1 rowheightdefault=auto ");
The result is as follows
any ideas on ways to fix this will be welcomed
P.S. I'm sure I'm just missing something laughable, but those are the worst ones to find :(
the position
option has one or two values. From the PDFlib API Reference, chapter 6.1, table 6.1:
The keywords left, center, right (in x direction) or bottom, center, top (in y direction) can be used as equivalents for the values 0, 50, and 100. If only one keyword has been specified, the corresponding keyword for the other direction will be added.
In your code, you set:
$optionList = "fittextline={position=".$field['textPosition']."
font=" .$bodyFont. "
fontsize=".$fieldsFont['fontSize']."} ";
which means, you apply only a single value. When you apply right
(means 100) you get the same value as position={right 100}
which means upper right corner.
In your case, I would recommend to extend the code to:
$optionList = "fittextline={position={".$field['textPosition']." bottom}
font=" .$bodyFont. "
fontsize=".$fieldsFont['fontSize']."} ";
so you get right bottom
similar to the left bottom
as the default.