создайте кнопку, которая загружает PDF-файл, созданный на лету


Как я могу создать кнопку, которая загружает PDF-файл. Проблема в том, что pdf-файл будет создан на лету (pdf-файл счета-фактуры).

В настоящее время у меня есть контроллер панели администратора, и в нем есть функция печати

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);
  }
}

В настоящее время у меня есть кнопка, подобная:

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

1 answers

Взгляните на app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php, вам нужно будет отправить правильный тип заголовка

/**
 * 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('*/*/');
}
 3
Author: Renon Stewart, 2015-11-18 17:11:12