title: PDF Invoice Generation in Magento
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>My code for creating the actual PDF invoice is below. This is the same code used in the default <code>pdfinvoicesAction</code> to create PDFs for the admin back-end (<code>app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php:459</code>). </p>
  
  <p><code>cron/Invoice.php</code> </p>
  
  <pre><code>&lt;?php
    /* $order is a valid Mage_Sales_Model_Order object */
  
    $invoices = $order-&gt;getInvoiceCollection();
    $pdfInvoice = Mage::getModel('sales/order_pdf_invoice');
    $pdf = $pdfInvoice-&gt;getPdf($invoices);
  
    $pdfFile = $pdf-&gt;render();
  ?&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p><code>app/core/Mage/Sales/Model/Order/Pdf/Abstract.php:221</code></p>
  
  <pre><code>    /* Payment */
      $paymentInfo = Mage::helper('payment')-&gt;getInfoBlock($order-&gt;getPayment())
          -&gt;setIsSecureMode(true)
          -&gt;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);
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>die(print_r($order-&gt;getPayment()-&gt;toArray()));
  </code></pre>
  
  <p>All the payment info is there. </p>
  
  <p>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.</p>
  
  <p>Edit: Just found out that while <code>Mage::helper('payment')-&gt;getInfoBlock($order-&gt;getPayment())-&gt;setIsSecureMode(true)-&gt;toHtml()</code> returns properly formatted HTML. <code>-&gt;toPdf</code> still returns nothing.</p>