Beispiel #1
0
 public function draw()
 {
     $currentX = $this->startingX;
     $currentY = $this->startingY;
     $remainingHeight = $this->height;
     foreach ($this->group as $textItem) {
         $multicell = new OOPHPDF_MultiCell($this->pdf);
         $multicell->setWidth($this->width)->setLn(2);
         foreach ($textItem as $textPropName => $textPropVal) {
             $funcName = str_replace(' ', '', ucwords(trim($textPropName)));
             if (method_exists($multicell, 'set' . $funcName)) {
                 call_user_func_array(array($multicell, 'set' . $funcName), array($textPropVal));
             }
         }
         if ($multicell->getHeightAuto() > $remainingHeight) {
             $multicell->setHeight($remainingHeight);
         }
         $multicell->drawAtPosition($currentX, $currentY);
         $currentX = $this->startingX;
         $currentY = $this->pdf->getY() + $this->spacing;
         $remainingHeight = $this->startingY + $this->height - $currentY;
         if ($remainingHeight <= 0) {
             return $this;
         }
     }
     return $this;
 }
 public function drawAtPosition($x, $y)
 {
     parent::drawAtPosition($x, $y);
     $startingX = $x;
     $startingY = $y;
     $width = $this->getWidth();
     $height = $this->getHeight();
     $imageStartX = $startingX + $this->imagePaddingX;
     $imageStartY = $startingY + $this->imagePaddingY;
     $paddedWidth = $width - $this->imagePaddingX * 2.0;
     $paddedHeight = $height - $this->imagePaddingY * 2.0;
     // We want to fill the MultiCell in one direction and center in the other direction, without distorting the aspect ratio.
     // first, try scaling to height of cell
     $image = new OOPHPDF_Image($this->pdf, $this->imageFilename, $imageStartX, $imageStartY, null, $paddedHeight);
     if ($image->getScaledWidth() > $paddedWidth) {
         // scaled image is wider than cell, so scale to width instead
         $image = new OOPHPDF_Image($this->pdf, $this->imageFilename, $imageStartX, $imageStartY, $paddedWidth, null);
     }
     // center scaled image
     $image->setX($imageStartX + ($paddedWidth - $image->getScaledWidth()) / 2.0);
     $image->setY($imageStartY + ($paddedHeight - $image->getScaledHeight()) / 2.0);
     $image->draw();
     return $this;
 }
   licensed under Creative Commons Attribution-Share Alike 4.0 International
   https://commons.wikimedia.org/wiki/File:Cute-kittens-12929201-1600-1200.jpg
*/
require __DIR__ . '/vendor/autoload.php';
// create a new PDF document
$pdf = new TCPDF('P', 'in', 'LETTER');
// set margins
$pdf->SetMargins(0.5, 1, 0.5);
$pdf->setFooterMargin(0.5);
$pdf->setHeaderMargin(0.5);
// set auto page breaks
$pdf->SetAutoPageBreak(true, 1);
// start a new page
$pdf->AddPage();
// create the left box
$leftBox = new OOPHPDF_MultiCell($pdf);
$leftBox->setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.")->setFontSize(12)->setFontStyle('B')->setFillColorArray(array(255, 200, 200))->setLn(0)->setWidth(3.75)->setHeightToAuto();
// create the right box containing an image
$rightBox = new OOPHPDF_ImageMultiCell($pdf, 'kitten.jpg', 0, 0.2);
$rightBox->setFontSize(12)->setFontStyle('I')->setFillColorArray(array(200, 200, 255))->setLn(0)->setWidth(3.75)->setHeightToAuto();
// create the first row and auto size it
$row1 = new OOPHPDF_Row($pdf, array($leftBox, $rightBox));
$row1->setHeightToAuto();
// create the second row and auto size it
$row2 = new OOPHPDF_Row($pdf, array($rightBox, $leftBox));
$row2->setHeightToAuto();
// create the table and add the rows to it
$table = new OOPHPDF_Table($pdf, array($row1, $row2));
// draw the table
$table->drawAtPosition(0.5, 1);
// output the document
<?php

require __DIR__ . '/vendor/autoload.php';
// create a new PDF document
$pdf = new TCPDF('P', 'in', 'LETTER');
// set margins
$pdf->SetMargins(0.5, 1, 0.5);
$pdf->setFooterMargin(0.5);
$pdf->setHeaderMargin(0.5);
// set auto page breaks
$pdf->SetAutoPageBreak(true, 1);
// start a new page
$pdf->AddPage();
// create the left box
$leftBox = new OOPHPDF_MultiCell($pdf);
$leftBox->setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.")->setFontSize(24)->setFontStyle('B')->setFillColorArray(array(255, 200, 200))->setLn(0)->setWidth(3.75)->setHeightToAuto();
// create the right box
$rightBox = new OOPHPDF_MultiCell($pdf);
$rightBox->setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")->setFontSize(16)->setFontStyle('I')->setFillColorArray(array(200, 200, 255))->setLn(0)->setWidth(3.75)->setHeightToAuto();
// find the maximum height of both boxes
$maxHeight = max($leftBox->getHeight(), $rightBox->getHeight());
// make both boxes use the max height
$leftBox->setHeight($maxHeight);
$rightBox->setHeight($maxHeight);
// draw both boxes
$leftBox->drawAtPosition(0.5, 1);
$rightBox->drawAtPosition(4.25, 1);
// output the document
$pdf->Output('document.pdf', 'I');