1

How can I create a button that downloads a pdf file. Problem is that the pdf will get created on the fly (invoice pdf).

Currently I have a admin panel controller and within it there is a printAction

public function printAction() {

  $params = $this->getRequest()->getParams();
  $order = Mage::getModel("sales/order")->load($order_id);

  try {
    if(!$order->canInvoice())
    {
      Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));
    }

    $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();

    $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf(array($invoice));

    echo $pdf->render();
  } catch(Exception $e) {
    Mage::log($e);
  }
}

Currently I have a button like:

<a href="<?php echo Mage::helper('adminhtml')->getUrl('aaa_kitchenboard/index/print'); ?>?orderid=<?php echo $order->getId();?>">Print</a>

1 Answer 1

3

Take a look at app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php, you will need to send the correct header type

/**
 * Print invoices for selected orders
 */
public function pdfinvoicesAction(){
    $orderIds = $this->getRequest()->getPost('order_ids');
    $flag = false;
    if (!empty($orderIds)) {
        foreach ($orderIds as $orderId) {
            $invoices = Mage::getResourceModel('sales/order_invoice_collection')
                ->setOrderFilter($orderId)
                ->load();
            if ($invoices->getSize() > 0) {
                $flag = true;
                if (!isset($pdf)){
                    $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
                } else {
                    $pages = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
                    $pdf->pages = array_merge ($pdf->pages, $pages->pages);
                }
            }
        }
        if ($flag) {
            return $this->_prepareDownloadResponse(
                'invoice'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),
                'application/pdf'
            );
        } else {
            $this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
            $this->_redirect('*/*/');
        }
    }
    $this->_redirect('*/*/');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.