/** * Wrapper taking custom units and margins into account * @see \ZendPdf\Page::getWidth() for documentation */ public function getWidth() { $width = parent::getWidth(); $this->convertFromPoints($width); return $width; }
public function drawHtml(Page $page, $html, $x1, $y1, $x2 = null, $y2 = null, $lineHeight = 1.3, $charEncoding = '', $debug = false) { if (is_null($x2)) { $x2 = $page->getWidth(); } if (is_null($y2)) { $y2 = 0; } if ($debug === true) { var_dump(array('x1' => $x1, 'y1' => $y1, 'x2' => $x2, 'y2' => $y2)); } $x = $x1; $y = $y1; if (is_null($page->getFont())) { $page->setFont(Font::fontWithName(Font::FONT_HELVETICA), 10); } $defaultFont = $page->getFont(); $defaultfontSize = $page->getFontSize(); $maxFontSize = $defaultfontSize; $words = array(); $elements = $this->getParser()->parse($html); if ($debug === true) { var_dump($elements); } foreach ($elements as $element) { if ($element instanceof DataElement) { $font = $element->getFont($defaultFont); $fontSize = $element->getFontSize($defaultfontSize); $maxFontSize = $fontSize > $maxFontSize ? $fontSize : $maxFontSize; $rawWords = explode(' ', $element->getValue()); foreach ($rawWords as $rawWord) { $rawWord .= $rawWord ? ' ' : ''; $wordWith = $this->widthForStringUsingFontSize($rawWord, $font, $fontSize); if ($x + $wordWith > $x2) { $x = $x1; if ($y1 < $y2) { $y += $maxFontSize * $lineHeight; } else { $y -= $maxFontSize * $lineHeight; } $maxFontSize = $fontSize; } $words[] = new Word($rawWord, $x, $y, $font, $fontSize); $x += $wordWith; } } elseif ($element instanceof ControllElement) { if ($element->isBlockElement()) { if ($x != $x1) { $x = $x1; if ($y1 < $y2) { $y += $maxFontSize * $lineHeight; } else { $y -= $maxFontSize * $lineHeight; } $maxFontSize = $defaultfontSize; } if ($element instanceof StartElement) { if (!is_null($element->marginTop())) { if ($y1 < $y2) { $y += $element->marginTop(); } else { $y -= $element->marginTop(); } } if (!is_null($element->marginLeft())) { if (!is_null($element->getListSign())) { $words[] = new Word($element->getListSign(), $x, $y, $element->getFont($defaultFont), $element->getFontSize($defaultfontSize)); } $x1 += $element->marginLeft(); $x = $x1; } } if ($element instanceof StopElement) { if (!is_null($element->marginBottom())) { if ($y1 < $y2) { $y += $element->marginBottom(); } else { $y -= $element->marginBottom(); } } if (!is_null($element->marginLeft())) { $x1 -= $element->marginLeft(); $x = $x1; } } } } } if ($debug === true) { var_dump($words); } foreach ($words as $word) { /** @var Word $word */ $page->setFont($word->getFont(), $word->getFontSize()); $page->drawText($word->getText(), $word->getX(), $word->getY(), $charEncoding); } if ($debug === true) { die; } return array($x, $y); }