Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF Invoice Generation in Magento

I am trying to check for new orders in Magento and if they exist, send a PDF invoice to the site admins. Everything is great except for the PDF.

When trying to externally create PDF invoices, all billing information is missing from the rendered PDF. Creating the invoice is fairly straight-forward, but finding the reason for the missing billing information has been impossible for me. Here is what I have learned.

My code for creating the actual PDF invoice is below. This is the same code used in the default pdfinvoicesAction to create PDFs for the admin back-end (app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php:459).

cron/Invoice.php

<?php
  /* $order is a valid Mage_Sales_Model_Order object */

  $invoices = $order->getInvoiceCollection();
  $pdfInvoice = Mage::getModel('sales/order_pdf_invoice');
  $pdf = $pdfInvoice->getPdf($invoices);

  $pdfFile = $pdf->render();
?>

This creates a valid PDF, containing all order information minus billing. Isolating the cause for this, I found that in the following (default) file, order billing information is an empty string - when I retrieve the invoice via the magento back-end this returns a formatted string containing all billing information.

app/core/Mage/Sales/Model/Order/Pdf/Abstract.php:221

    /* Payment */
    $paymentInfo = Mage::helper('payment')->getInfoBlock($order->getPayment())
        ->setIsSecureMode(true)
        ->toPdf();

    // $paymentInfo is an empty string when rendering a PDF externally,
    // and formatted as expected when rendering a PDF via the admin panel

    $payment = explode('{{pdf_row_separator}}', $paymentInfo);

So that's whats happening. I have no idea how or why. The real kicker? In my cron job script, if I run the following:

die(print_r($order->getPayment()->toArray()));

All the payment info is there.

I've asked this in the development forum of the Magento website with no luck. I'm really hoping someone could help shed light on this issue, as I've exhausted my debugging efforts. Thank you so much.

Edit: Just found out that while Mage::helper('payment')->getInfoBlock($order->getPayment())->setIsSecureMode(true)->toHtml() returns properly formatted HTML. ->toPdf still returns nothing.

like image 989
Mahdi.Montgomery Avatar asked Jan 12 '12 01:01

Mahdi.Montgomery


People also ask

How can I get invoice in Magento 2?

Invoices in Magento 2 is a record of the payment for an order. An admin can create multiple invoices for a single order. However, when you want to implement a functionality per order only, based on invoice data, you'll need to get invoice data from order id in Magento 2.


1 Answers

Well I hope this helps someone else. Sometimes when you ask a detailed question, it gets you thinking more clearly. It was a very simple solution, with no clear error message indicating what the problem was!

Magento uses two folders for template files adminhtml and frontend. By default, there is only a .phtml for the payment data helper pdf action on the back-end. My script was running on the front-end, not finding this file, and outputting an empty string.

So in short, if your PDF invoices in Magento <= 1.6.1 are missing billing information when rendered on the front-end, take this file:

app/design/adminhtml/default/default/template/paygate/info/pdf.phtml

Then copy/paste it to:

app/design/frontend/base/default/template/paygate/info/pdf.phtml (Or if you prefer, your custom template directory)

In hindsight, I probably should be using the back-end layout for my cron scripts.

Edit: Today I learned about the var/log/system.log file in Magento... (Re-editing for clarity) The /var/log/system.log file was clearly telling me what the problem was, I just failed to read it.

like image 102
Mahdi.Montgomery Avatar answered Oct 05 '22 02:10

Mahdi.Montgomery