Php Web Development With Laminas Pdf Online
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:
// 1. Create new document $pdf = new PdfDocument();
// Step 1: Create font definition $font = \Laminas\Pdf\Font::fontWithPath('/path/to/DejaVuSans.ttf'); // Step 2: Register with the page $page->setFont($font, 12); php web development with laminas pdf
While it has a steeper learning curve than HTML-to-PDF converters, the precision and performance make it invaluable for enterprise applications. Start with the core fonts and drawing primitives, then graduate to custom TrueType fonts and complex vector graphics.
// CMYK $page->setFillColor(new Cmyk(0, 1, 1, 0)); // Cyan=0, Magenta=1, Yellow=1, Black=0 -> Red Since drawText() only places the string at given X/Y, implement alignment manually: Start with the core fonts and drawing primitives,
// 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 } }
function generateInvoice($invoiceNumber, $date, $customer, $items, $total) { $pdf = new PdfDocument(); $page = $pdf->newPage(Page::SIZE_A4); $pdf->pages[] = $page; number_format($item['total'], 2), 500, $y - 10); $y -=
// 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 }