With Laminas Pdf - Php Web Development
my-pdf-app/ ├── vendor/ ├── public/ │ └── generate.php └── composer.json Let's create a simple PDF with "Hello, Laminas PDF!".
// Header $page->setFont($fontBold, 24); $page->drawText('INVOICE', 50, $y); $page->setFont($fontNormal, 10); $page->drawText("No: $invoiceNumber", 450, $y); $page->drawText("Date: $date", 450, $y - 15); $y -= 60;
return $pdf; }
$fontBold = Font::fontWithName(Font::FONT_HELVETICA_BOLD); $fontNormal = Font::fontWithName(Font::FONT_HELVETICA); $y = 800;
// Load image $image = \Laminas\Pdf\Image::imageWithPath('/path/to/logo.jpg'); // Draw at position (x, y) with scaling $page->drawImage($image, 50, 750, 150, 800); // x1,y1 (bottom-left) to x2,y2 (top-right) php web development with laminas pdf
// 4. Draw text at coordinates (x=100, y=700) $page->drawText('Hello, Laminas PDF!', 100, 700, 'UTF-8');
composer require laminas/laminas-pdf If you don't have Composer, get it first . Laminas requires PHP 7.4 or 8.0+. Laminas requires PHP 7
function drawCenteredText($page, $text, $y, $fontSize) { $width = strlen($text) * $fontSize * 0.6; // rough estimate $pageWidth = $page->getWidth(); $x = ($pageWidth - $width) / 2; $page->drawText($text, $x, $y, 'UTF-8'); } For exact text dimensions, use $font->widthForStringUsingFontSize($text, $fontSize) . Lines $page->setLineColor(new Rgb(0, 0, 0)); $page->setLineWidth(2); $page->drawLine(50, 500, 562, 500); // horizontal line across letter page Rectangles // Outline only $page->drawRectangle(100, 400, 200, 450); // Filled rectangle $page->setFillColor(new Rgb(0.8, 0.9, 1)); $page->drawRectangle(100, 350, 200, 400, Page::SHAPE_DRAW_FILLED); Circles and Ellipses // Circle (center x, center y, radius) $page->drawCircle(306, 500, 50); // Ellipse (x, y, x-radius, y-radius) $page->drawEllipse(306, 400, 60, 30); Polygons $vertices = [ [200, 300], [250, 350], [200, 400], [150, 350] ]; $page->drawPolygon($vertices); Rotation and Scaling (via coordinate transformation) $page->saveGS(); // save graphics state $page->rotate(306, 500, deg2rad(45)); // rotate 45° around (306,500) $page->drawText('Rotated text', 306, 500); $page->restoreGS(); // restore Loading External TrueType/OpenType Fonts For Unicode, custom fonts, or proper UTF-8 support, embed a TrueType font:

