Php Web Development With Laminas Pdf Link Direct
// Table rows $page->setFont($fontNormal, 10); foreach ($items as $item) { $page->drawText($item['desc'], 60, $y - 10); $page->drawText($item['qty'], 350, $y - 10); $page->drawText('$' . number_format($item['price'], 2), 420, $y - 10); $page->drawText('$' . number_format($item['total'], 2), 500, $y - 10); $y -= 25; if ($y < 100) { // Add new page logic here for long invoices } }
// 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)
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: php web development with laminas pdf
// You can also reuse the same image on multiple pages $page2->drawImage($image, 50, 750, 150, 800); Let's create a realistic invoice PDF generator.
// Customer info $page->setFont($fontBold, 12); $page->drawText('Bill To:', 50, $y); $page->setFont($fontNormal, 11); $page->drawText($customer, 50, $y - 20); $y -= 60; pages as $page) { $page->
return $pdf; }
composer require laminas/laminas-pdf If you don't have Composer, get it first . Laminas requires PHP 7.4 or 8.0+. // rotate back }
// Load an existing PDF $pdf = PdfDocument::load('/path/to/document.pdf'); // Add a watermark to every page foreach ($pdf->pages as $page) { $page->setFont(Font::fontWithName(Font::FONT_HELVETICA), 60); $page->setFillColor(new Rgb(0.8, 0.8, 0.8)); $page->rotate(250, 400, deg2rad(45)); $page->drawText('CONFIDENTIAL', 200, 400); $page->rotate(250, 400, deg2rad(-45)); // rotate back }