protected function _getWrappedText($string, Zend_Pdf_Style $style, $max_width) { $wrappedText = ''; $lines = explode("\n", $string); foreach ($lines as $line) { $words = explode(' ', $line); $word_count = count($words); $i = 0; $wrappedLine = ''; while ($i < $word_count) { /* if adding a new word isn't wider than $max_width, we add the word */ if ($this->widthForStringUsingFontSize($wrappedLine . ' ' . $words[$i], $style->getFont(), $style->getFontSize()) < $max_width) { if (!empty($wrappedLine)) { $wrappedLine .= ' '; } $wrappedLine .= $words[$i]; } else { $wrappedText .= $wrappedLine . "\n"; $wrappedLine = $words[$i]; } $i++; } $wrappedText .= $wrappedLine . "\n"; } return $wrappedText; }
/** * Set the style to use for future drawing operations on this page * * @param Zend_Pdf_Style $style * @return Zend_Pdf_Page */ public function setStyle(Zend_Pdf_Style $style) { $this->_style = $style; $this->_addProcSet('Text'); $this->_addProcSet('PDF'); if ($style->getFont() !== null) { $this->setFont($style->getFont(), $style->getFontSize()); } $this->_contents .= $style->instructions($this->_pageDictionary->Resources); return $this; }
/** * Draw table or part of table by coordinates with or without header * * @param array[][] $table Table values * @param int $x x coordinate * @param int $y y coordinate * @param int $width Table width * @param int $max_y Minimal y coordinate * @param array[] $header Table header * @param int $start_row start row of table * * @return int End row * * @api */ function drawTable($table, $x, $y, $width, $max_y = 0, $header = NULL, $start_row = 0, $params = false, $note = '') { if (!$table) { return NULL; } if ($start_row > count($table) - 1) { if ($this->currentPosition > $y - $this->MARGIN['bottom']) { $this->currentPosition = $y - $this->MARGIN['bottom']; } return NULL; } $max_widths = $this->getTableColumnsMaxWidths($table, $params); $avg_widths = $this->getTableColumnsAverageWidths($table, $params); // parent::drawText(implode(',',$max_widths), $this -> MARGIN['left'], $this -> MARGIN['bottom'], 'UTF-8'); //вычисляем ширину столбцов, в зависимости от того, какую таблицу рисуем выбираем нужную формулу if ($params) { $awidth = array_sum($max_widths) - 0; } else { $awidth = array_sum($avg_widths) - $avg_widths[0] - 5; $widths = array($max_widths[0]); } // Подгоняем ширину под заданную, через процентные соотношения foreach ($avg_widths as $i => $a_w) { if ($params) { $widths[] = $a_w / $awidth * $width; } elseif ($i != 0) { $widths[] = $a_w / $awidth * ($width - $widths[0] - 10); } } // запомним наш $x и текущий $y $coords = array($x, $y); // write header ( if exists ) if ($header) { //создаем сностки для таблици с параметрами. В зависимости от количества столбцов и длинны названия параметра, заменяем его и записываем в сноски $snoski = ''; $s_number = 1; $widthSnoski = $width; $this->pageFormat == 'A4' ? $countChar = 30 : ($countChar = 10); foreach ($header as $i => $column) { if (count($header) > 7 && strlen($column) >= $countChar && $i != 0 || $column == 'Типоразмер') { if ($widthSnoski < $this->widthForStringUsingFontsize($s_number . '* - ' . $column . '; ', Model_Static_Fonts::get("Arial Narrow"), $this->fontSizeFormatSnoski) + $this->widthForStringUsingFontsize($snoski, Model_Static_Fonts::get("Arial Narrow"), $this->fontSizeFormatSnoski)) { $snoski .= chr('0x0D') . chr('0x0A'); $widthSnoski *= 2; } $snoski .= $s_number . '* - ' . $column . '; '; $header[$i] = $s_number . '*'; $s_number++; } elseif ($this->pageFormat == 'A5') { $header[$i] = str_replace('(', chr('0x0D') . chr('0x0A') . '(', $column); } } $snoski .= ' ' . $note; $this->saveGS(); $style = new Zend_Pdf_Style(); $style->setFillColor(new Zend_Pdf_Color_Html("white")); $style->setFont(Model_Static_Fonts::get("Arial Narrow"), $this->fontSizeFormatHeader); // Bold $this->setStyle($style); // calculate height of header $height = 0; foreach ($header as $i => $column) { $style->setLineWidth($widths[$i]); $height = max($height, $this->getTextBlockHeight(trim($column), $style, 0, true)); } // if we can't write 2 line - exit if ($y - $height - $max_y < 16) { $this->restoreGS(); return 0; } // else - draw header background $this->drawHorizontalLine($coords[0] + 5, $coords[0] + $width, $this->pageFormat == 'A4' ? $y - $height / 2 + 5.5 : $y - $height / 2 + 3.5, $height, new Zend_Pdf_Color_Html("#0095da")); // here - write header // foreach ($header as $i => $col) { $style->setLineWidth($widths[$i]); $width_text = $this->widthForStringUsingFontsize($col, $style->getFont(), $style->getFontSize()); if ($widths[$i] > $width_text && $i != 0) { $iLeft = $x + ($widths[$i] - $width_text) / 2; } else { $iLeft = $x + 8; } $y = min($y, $this->drawTextBlock(str_replace(array(''), array(''), trim($col)), $iLeft + 1 * ($i == 0), $coords[1] - 3, $style, 0, true)); $x += $widths[$i]; } $this->restoreGS(); } // make offset(3) if ($this->pageFormat == 'A4') { $y -= 2; } elseif ($this->pageFormat == 'A5') { $y -= 0; } // write table data $coords[0] += 5; $width -= 5; for ($c_row = $start_row; $c_row < count($table); $c_row++) { $rowset = $table[$c_row]; $x = $coords[0]; $line_y = $y; // for table with headers odd lines have bg if ($c_row % 2 != 0 && $header) { $style = new Zend_Pdf_Style(); $style->setLineWidth($widths[$i] + 10); $style->setFont(Model_Static_Fonts::get("Arial Narrow" . ($i == 0 && !$header ? ' Bold' : '')), $this->fontSizeFormat); $height = 0; foreach ($rowset as $i => $col) { $style->setLineWidth($widths[$i]); $height = max($height, $this->getTextBlockHeight($col, $style)); } if ($this->pageFormat == 'A4') { $this->drawHorizontalLine($x, $x + $width, $y + (3 - intval(($height - 2) / 6 - 1.5) * 2), $height - 4, new Zend_Pdf_Color_Html("#e7e7e7")); } elseif ($this->pageFormat == 'A5') { $this->drawHorizontalLine($x, $x + $width, $y + (2 - intval(($height - 2) / 6 - 1) * 2), $height - 4, new Zend_Pdf_Color_Html("#e7e7e7")); } } foreach ($rowset as $i => $col) { $style = new Zend_Pdf_Style(); if ($params) { $style->setFont(Model_Static_Fonts::get("Arial Narrow" . ($i == 0 && !$header ? ' Bold' : '')), $this->fontSizeFormat); } else { $style->setFont(Model_Static_Fonts::get("Arial Narrow" . ($i == 0 && !$header ? ' Bold' : '')), $this->fontSizeFormatDiscription); } $style->setLineWidth($widths[$i]); // пишем клетку, и запоминаем макс высоту ( для многострочности ) // выравнивание по центру значения таблицы с параметрами $width_text = $this->widthForStringUsingFontsize(trim($col), $style->getFont(), $style->getFontSize()); if ($widths[$i] > $width_text && $params && $i != 0) { $iLeft = $x + ($widths[$i] - $width_text) / 2; $line_y = min($line_y, $this->drawTextBlock(str_replace(' ', ' ', trim($col)), $iLeft, $y, $style, -2)); } elseif ($params && $i == 0) { $iLeft = $x + 10; $line_y = min($line_y, $this->drawTextBlock(str_replace(' ', ' ', trim($col)), $iLeft, $y, $style, -2)); } else { $iLeft = $x; $line_y = min($line_y, $this->drawTextBlock(str_replace(' ', ' ', trim($col)), $iLeft, $y + 4, $style)); } $x += $widths[$i]; } $heightSnoski = 0; $y = $line_y; if ($header) { $style = new Zend_Pdf_Style(); $style->setFont(Model_Static_Fonts::get("Arial Narrow"), $this->fontSizeFormatSnoski); $style->setLineWidth($width); $heightSnoski = $this->getTextBlockHeight($snoski, $style, 0, true); } // проверим, не вышли ли бы за границы высоты, но не в последнем элементе if ($y - 11 - $heightSnoski < 8 && count($table) == 1) { if ($header) { $this->drawTextBlock($snoski, 5, $y, $style); //$page -> setCurrentPosition($page -> getCurrentPosition()); } if ($this->currentPosition > $y - $this->MARGIN['bottom']) { $this->currentPosition = $y - $this->MARGIN['bottom']; } return NULL; } if ($y - $max_y - $heightSnoski < 8 && $c_row != count($table) - 1) { // if ($header) { $this->drawTextBlock($snoski, 5, $y, $style); //$page -> setCurrentPosition($page -> getCurrentPosition()); } if ($this->currentPosition > $y - $this->MARGIN['bottom']) { $this->currentPosition = $y - $this->MARGIN['bottom']; } return $c_row; } } //$this -> drawTextBlock($y, -20, $y + 10, $style); if ($this->currentPosition > $y - $this->MARGIN['bottom']) { $this->currentPosition = $y - $this->MARGIN['bottom']; } // пишем сноски в конце блока с парамаетрами if ($header) { $style = new Zend_Pdf_Style(); $style->setFont(Model_Static_Fonts::get("Arial Narrow"), $this->fontSizeFormatSnoski); $style->setLineWidth($width); $this->drawTextBlock($snoski, 5, $y, $style); // $this -> drawTextBlock( $note, 5, $this -> getCurrentPosition() + 25, $style); //$this -> setCurrentPosition($this -> getCurrentPosition()); } return NULL; }