2017 © Pedro Peláez
 

library php-pdf-invoice

Generate PDF files for your application's invoices with PHP

image

quickshiftin/php-pdf-invoice

Generate PDF files for your application's invoices with PHP

  • Sunday, May 20, 2018
  • by quickshiftin
  • Repository
  • 2 Watchers
  • 3 Stars
  • 56 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 11 Versions
  • 8 % Grown

The README.md

PHP PDF Invoice

This project uses a PDF invoice generator extracted from Magento for general use via Composer. You can easily integrate your existing domain model and start generating PDF invoices that look like this:, (*1)

Example Invoice PDF, (*2)

Features

  • Generate Invoice PDF documents
  • Integrate your domain model easily by implementing Order and OrderItem interfaces
  • Composer distribution
  • Change background and font colors, font types, line color and provide custom logo
  • Automatically create multiple pages based on number of line items

Install via Composer

./composer.phar require quickshiftin/php-pdf-invoice, (*3)

Usage

Integration boilerplate - connecting your existing domain model to the generator

To integrate your existing application's orders, simply provide two classes that implement Quickshiftin\Pdf\Invoice\Spec\Order and Quickshiftin\Pdf\Invoice\Spec\OrderItem, (*4)

OrderItem interface implementation

namespace MyApp;
use Quickshiftin\Pdf\Invoice\Spec\OrderItem;

// Implement the Order Item methods below
class MyOrderItem interface implements OrderItem
{
    /**
     * The name or description of the product
     * @return string
     */
    public function getName();

    /**
     * The 'SKU' or unique identifier for your product
     * @return string
     */
    public function getSku();

    /**
     * The quantity sold
     * @return int
     */
    public function getQuantity();

    /**
     * The price per unit
     * @return float
     */
    public function getPricePerUnit();

    /**
     * The price including tax
     * @return flaot
     */
    public function getPrice();

    /**
     * The sales tax amount in dollars
     * @return float
     */
    public function getSalesTaxAmount();
}

Order interface implementation

use Quickshiftin\Pdf\Invoice\Spec\Order;

// Implement the order methods below
class MyOrder implements Order
{
    /**
     * Get the sub-total, eclusive of shipping and tax.
     * @return float
     */
    public function getPriceBeforeShippingNoTax();

    /**
     * Get the shipping charge if any
     * @return float
     */
    public function getCustomerShipCharge();

    /**
     * Get the sales tax amount, eg .08 for 8%
     * @return float
     */
    public function getSalesTaxAmount();

    /**
     * Get the total cost including shipping and tax
     * @return float
     */
    public function getTotalCost();

    /**
     * Get the full billing address for the customer
     * @return string
     */
    public function getFullBillingAddress();

    /**
     * Get the payment method, EG COD, Visa, PayPal etc
     * @return string
     */
    public function getPaymentMethod();

    /**
     * Get the full shipping address for the order
     * @return string
     */
    public function getFullShippingAddress();

    /**
     * Get the name of the shipping method, EG UPS, FedEx, etc
     * @return string
     */
    public function getShippingMethodName();

    /**
     * Get an array of OrderItem objects
     * @note This should return an array of instances of a class where you implement Quickshiftin\Pdf\Invoice\Spec\OrderItem
     * @return array
     */
    public function getOrderItems();

    /**
     * Get the id of the order
     * @return int|string
     */
    public function getOrderId();

    /**
     * Get the date of the sale
     * @return DateTime
     */
    public function getSaleDate();
}

Implementation suggestion

Since these are interfaces, you can create a new class that wraps your existing OrderItem objects. If there are no name collisions you could also consider implementing directly on your existing OrderItem class., (*5)

Building & Styling your Invoice PDFs

This system uses Zend_Pdf (from ZF1) under the hood. The package provides Quickshiftin\Pdf\Invoice\Factory which is a wrapper for instantiating classes from Zend_Pdf. You'll use these objects to customize the appearance of your PDFs., (*6)

We also assume you have an instance of an order object which implements Quickshiftin\Pdf\Invoice\Spec\Order as described above that is stored in a variable called $myOrder., (*7)

use Quickshiftin\Pdf\Invoice\Invoice as PdfInvoice;
use Quickshiftin\Pdf\Invoice\Factory as InvoiceFactory;

$oInvoiceFactory = new InvoiceFactory();
$oInvoicePdf     = new PdfInvoice();

// Configure fonts - just put ttf font files somewhere your project can access them
$oInvoicePdf->setRegularFontPath(__DIR__ . '/../assets/Arial.ttf');
$oInvoicePdf->setBoldFontPath(__DIR__ . '/../assets/Arial Bold.ttf');
$oInvoicePdf->setItalicFontPath(__DIR__ . '/../assets/Arial Italic.ttf');

// Set Colors
$red    = '#d53f27';
$yellow = '#e8e653';

// Title section of invoice
// Background color for title section of invoice, the default is white
$oInvoicePdf->setTitleBgFillColor($oInvoiceFactory->createColorHtml($yellow));
$oInvoicePdf->setTitleFontColor($oInvoiceFactory->createColorHtml('black'));

// Header sections of invoice
$oInvoicePdf->setHeaderBgFillColor($oInvoiceFactory->createColorHtml($red));
$oInvoicePdf->setBodyHeaderFontColor($oInvoiceFactory->createColorHtml('white'));

// Body section of invoice
$oInvoicePdf->setBodyFontColor($oInvoiceFactory->createColorHtml('black'));

// Line color of invoice
$oInvoicePdf->setLineColor($oInvoiceFactory->createColorGrayscale(0));

// Configure logo
$oInvoicePdf->setLogoPath(__DIR__ . '/../assets/fake-logo.jpg');

// Build the PDF
// $oPdf is an instance of Zend_Pdf
$oPdf = $oInvoicePdf->getPdf($myOrder);

// A string rendition, you could echo this to the browser with headers to implement a download
$pdf = $oPdf->render();

// You can also simply save it to a file
file_put_contents('/tmp/test.pdf', $pdf);

Notes

You can look at the test directory for usage insights. Issues and PRs welcome!, (*8)

The Versions

20/05 2018

1.10.0

1.10.0.0

Generate PDF files for your application's invoices with PHP

  Sources   Download

OSL-3.0

The Requires

 

20/05 2018

dev-master

9999999-dev

Generate PDF files for your application's invoices with PHP

  Sources   Download

OSL 3.0 OSL-3.0

The Requires

 

18/10 2017

1.9.0

1.9.0.0

Generate PDF files for your application's invoices with PHP

  Sources   Download

OSL 3.0

The Requires

 

18/10 2017

1.8.0

1.8.0.0

Generate PDF files for your application's invoices with PHP

  Sources   Download

OSL 3.0

The Requires

 

18/08 2017

1.7.0

1.7.0.0

Generate PDF files for your application's invoices with PHP

  Sources   Download

OSL 3.0

The Requires

 

18/08 2017

1.6.0

1.6.0.0

Generate PDF files for your application's invoices with PHP

  Sources   Download

OSL 3.0

The Requires

 

18/08 2017

1.5.0

1.5.0.0

Generate PDF files for your application's invoices with PHP

  Sources   Download

OSL 3.0

The Requires

 

18/08 2017

1.4.0

1.4.0.0

Generate PDF files for your application's invoices with PHP

  Sources   Download

OSL 3.0

The Requires

 

18/08 2017

1.3.0

1.3.0.0

Generate PDF files for your application's invoices with PHP

  Sources   Download

OSL 3.0

The Requires

 

18/08 2017

1.2.0

1.2.0.0

Generate PDF files for your application's invoices with PHP

  Sources   Download

OSL 3.0

The Requires

 

17/08 2017

v1.0

1.0.0.0

Generate PDF files for your application's invoices with PHP

  Sources   Download

OSL 3.0

The Requires