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.


Error: Your Requested widget " ai_widget-6" is not in the widget list.
  • [do_widget_area above-nav-left]
    • [do_widget_area above-nav-right]
      • [do_widget_area footer-1]
        • [do_widget id="wpp-4"]
      • [do_widget_area footer-2]
        • [do_widget id="recent-posts-4"]
      • [do_widget_area footer-3]
        • [do_widget id="recent-comments-3"]
      • [do_widget_area footer-4]
        • [do_widget id="archives-4"]
      • [do_widget_area logo-bar]
        • [do_widget id="oxywidgetwpml-3"]
      • [do_widget_area menu-bar]
        • [do_widget id="search-3"]
      • [do_widget_area sidebar]
        • [do_widget id="search-4"]
        • [do_widget id="ai_widget-2"]
        • [do_widget id="categories-5"]
        • [do_widget id="ai_widget-3"]
        • [do_widget id="ai_widget-4"]
        • [do_widget id="ai_widget-5"]
      • [do_widget_area sub-footer-1]
        • [do_widget id="text-4"]
      • [do_widget_area sub-footer-2]
        • [do_widget_area sub-footer-3]
          • [do_widget_area sub-footer-4]
            • [do_widget_area upper-footer-1]
              • [do_widget id="search-2"]
              • [do_widget id="recent-posts-2"]
              • [do_widget id="recent-comments-2"]
              • [do_widget id="archives-2"]
              • [do_widget id="categories-2"]
              • [do_widget id="meta-2"]
            • [do_widget_area upper-footer-2]
              • [do_widget_area upper-footer-3]
                • [do_widget_area upper-footer-4]
                  • [do_widget_area widgets_for_shortcodes]
                    • [do_widget id="search-5"]
                    • [do_widget id="ai_widget-6"]
                  • [do_widget_area wp_inactive_widgets]
                    • [do_widget id="wpp-2"]
                    • [do_widget id="text-1"]
                    • [do_widget id="recent-posts-3"]
                    • [do_widget id="categories-3"]
                    • [do_widget id="archives-3"]
                    • [do_widget id="icl_lang_sel_widget-3"]

                  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
                  Freelance AWS Cloud Solution Architect & Linux Sysadmin

                  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, AWS, DevOps, DevSecOps, 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.