У меня есть еще одна проблема с созданием PDF.
Я написал общую функцию, которая создаст таблицу.
// 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, "");
}
Я внес некоторые изменения с тех пор, как обновил верхние комментарии, но функция довольно прямолинейна (если вы можете сказать это о функциях, связанных с PDFLib: '-(), и обычно следует комментариям
Мне нужно, чтобы текст в одном из полей был выровнен по правому краю
Когда я вызываю эту функцию в другом классе и впоследствии отображаю ее на странице, поле, для которого я настроил выравнивание по правому краю, слегка подпрыгивает и выходит из вертикального выравнивания. Я абсолютно сбит с толку этим.
Вот код вызова функции
//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 ");
Результат выглядит следующим образом
любые идеи о том, как это исправить, будут приветствоваться
P.S. Я уверен, что просто упускаю что-то смешное, но это худшее, что можно найти :(