GNU/Linux, Open Source, Cloud Computing, DevOps and more...

OpenCart PDF invoices with Zend_Pdf – Spanisland

No comments

OpenCart logo    Zend Framework logo

Client: Spanisland, S.L.

Spanisland OpenCart ecommerce website

This work consisted in integrating the Zend_Pdf class from Zend Framework into OpenCart ecommerce application to issue PDF invoices fully customized and tailored to client’s corporate image. I added a “PDF Invoice” button to each item of the order list inside OpenCart admin area and also to each individual order detail view. When clicked, an invoice was automatically generated and filled out with all the relevant order details (I have omitted these data due to confidentiality).

Spanisland PDF Invoice Template

The user just had to save the PDF file and send it to the client manually, because it was never implemented any functionality for automatically sending invoices to customers making the purchase. The invoice and order numbers, customer name and invoice date were added to the PDF file name in order to be fully descriptive and easier to be managed.

Below is a small fragment of myinvoice() function wich was added to OpenCart to generate invoices using Zend_Pdf:

/**
* Generates the PDF invoice
*
* @return   void
*/
public function myinvoice() {

    // Load Zend_Pdf class
    set_include_path ( get_include_path().":". DIR_SYSTEM . "external/" );
    require_once( DIR_SYSTEM . 'external/Zend/Pdf.php');

    $encoding = "UTF-8";
    $templateName = "factura_spanisland";
    $templatesDir = DIR_TEMPLATE . 'sale/';
    $templateVersion = $this->_getLastPdfTemplateVersion($templateName, $templatesDir);

    // Load PDF document from a file.
    $pdfTemplate = $templatesDir . $templateName . '_v'. $templateVersion . '.pdf';
    $pdf = Zend_Pdf::load($pdfTemplate);

    $page = $pdf->pages[0];

    // Apply style
    $style = $this->_getPdfTemplateStyle();
    $page->setStyle($style);

    // Define fonts
    $font1 = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);
    $font2 = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
    $font3 = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);

    $this->load->language('sale/order');

    /////////////////////////
    /// LOAD INVOICE DATA ///
    /////////////////////////

    $this->load->model('sale/order');

    $this->data['orders'] = array();

    $orders = array();

    if (isset($this->request->post['selected'])) {
        $orders = $this->request->post['selected'];
    } elseif (isset($this->request->get['order_id'])) {
        $orders[] = $this->request->get['order_id'];
    }

    foreach ($orders as $order_id) {
        $order_info = $this->model_sale_order->getOrder($order_id);
			
    if ($order_info) {
        if ($order_info['invoice_id']) {
            $invoice_id = $order_info['invoice_prefix'] . sprintf("%04d", $order_info['invoice_id']);
	} else {
            $invoice_id = $this->model_sale_order->generateInvoiceId($order_id);
	    $order_info = $this->model_sale_order->getOrder($order_id);
	    $invoice_id = $order_info['invoice_prefix'] . sprintf("%04d", $order_info['invoice_id']);
	}

	$product_data = array();

	$products = $this->model_sale_order->getOrderProducts($order_id);

...
...
...


 

About the author

Daniel López Azaña
Cloud Solutions Architect

Entrepreneur, a generator of ideas and restless mind. Passionate about new technologies, especially Linux systems and Open Source Software. I also like to write about Technology News, Cloud Computing, DevOps, System Security, Web Development and Programming, SEO, Science, Innovation, Entrepreneurship, etc.

DanielOpenCart PDF invoices with Zend_Pdf – Spanisland

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.