Home >Backend Development >PHP Tutorial >phpmaster | Generating Invoices with Zend-Pdf

phpmaster | Generating Invoices with Zend-Pdf

Lisa Kudrow
Lisa KudrowOriginal
2025-03-02 08:40:10304browse

This article demonstrates how to use Zend_Pdf to dynamically generate PDF invoices. Key benefits include creating new PDFs or modifying existing ones, ideal for invoices with both static (company info, logo) and dynamic (customer, transaction details) data. Zend_Pdf handles fonts, colors, and content addition, although text positioning may require some adjustment. Generated invoices can be saved to a file or sent directly to the browser for download. Beyond basic PDF creation, Zend_Pdf offers features like image inclusion, font styling, link insertion, and error handling. However, complex elements like tables necessitate manual cell placement calculations.

Getting Started: Template-Based Approach

Creating a PDF from scratch is possible, but using a pre-designed template (e.g., from Word or LibreOffice, exported as PDF) is more efficient. This approach minimizes coding and generation time. The template contains static elements (company details, logo etc.), while dynamic data is added programmatically. Below is an example template (replace with your own).

phpmaster | Generating Invoices with Zend-Pdf

Setting up Zend Framework:

If you're new to Zend Framework, download it from https://www.php.cn/link/997440836d4c2346926d8aca03690f78. Unpack it on your server and use Composer for dependency management: composer require zendframework/zendpdf. (Note: older examples used a manual autoloader; Composer is the recommended modern approach).

Loading the PDF Template:

Zend_Pdf loads existing PDFs using Zend_Pdf::load("/path/to/invoice-template.pdf");. The resulting object provides access to individual pages via the pages array (e.g., $page = $invoice->pages[0];).

Fonts and Colors:

Before adding text, define the font. Zend_Pdf supports 14 standard fonts (e.g., Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_BOLD);) or custom TrueType fonts via Zend_Pdf_Font::fontWithPath("/path/to/myfont.ttf");. Set the font and size using $page->setFont($font, 12);.

Colors are specified using Zend_Pdf_Color_* objects (grayscale, RGB, CMYK, or HTML notation). Set the fill color with $page->setFillColor($color);.

Adding Content:

$page->drawText($text, $x, $y); adds text. Coordinates ($x, $y) are in points (72 points = 1 inch), bottom-left origin. Precise positioning may require experimentation.

phpmaster | Generating Invoices with Zend-Pdf

Example data (replace with your database retrieval):

$customerName = "Angelina Jolie";
$invoiceId = "DF-00025786423";
$items = array(
    array("Golden Globe Polish", 1, 25.50, 25.50),
    array("Trophy Shelf", 2, 180.00, 360.00),
    array("DIY Tattoo Kit", 1, 149.99, 149.99)
);
$subtotal = 535.49;
$discount = 10;
$amountDue = 481.94;

Text placement example (adjust coordinates as needed):

$page->drawText($customerName, 110, 641);
$page->drawText($invoiceId, 420, 642);
// ... other text additions ...

Rendering the Invoice:

Save the PDF using $invoice->save($pathToFile); or send it directly to the browser for download:

header("Content-Type: application/x-pdf");
header("Content-Disposition: attachment; filename=invoice-". date("Y-m-d-H-i") . ".pdf");
echo $invoice->render();

Conclusion:

This guide provides a foundational understanding of Zend_Pdf for invoice generation. Explore the library's extensive capabilities for more advanced features. Remember to adapt coordinates to match your template. The complete code example is available on GitHub (link to be provided if available).

The above is the detailed content of phpmaster | Generating Invoices with Zend-Pdf. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn