Printing Order Labels in Magento: A Step-by-Step Guide
Are you looking to print order labels directly from your Magento admin panel? You’ve come to the right place! In this post, we’ll guide you through the process of converting the ‘Print Labels’ action on the orders page to open a PDF file instead of an HTML page.
Understanding the Current Setup
Let’s first examine the current code structure. The add_action method in the AquiveMedia_Orderlabel_Model_Observer class is responsible for adding the ‘Print Labels’ action to the order grid:
class AquiveMedia_Orderlabel_Model_Observer {
public function add_action($observer) {
$block = $observer->getEvent()->getBlock();
if ($block instanceof Mage_Adminhtml_Block_Widget_Grid_Massaction) {
if ($block->getParentBlock() instanceof Mage_Adminhtml_Block_Sales_Order_Grid) {
$block->addItem('print_labels', array(
'label' => Mage::helper('sales')->__('Print Labels'),
'url' => $block->getUrl('orderlabel/adminhtml_orderlabel/massprint')
)
);
}
}
}
}
The action is added with a specific URL (massprint) that points to an HTML page. Our goal is to redirect this to generate and display a PDF file.
Redirecting to a PDF File
To achieve this, we need to modify the Mage_Orderlabel_Model_Observer class that handles the ‘Print Labels’ action. Open your terminal or command prompt and navigate to:
your_magento_root/app/code/local/AquiveMedia/Orderlabel/Model/Observer.php
Find the massprintAction method in this file. It currently calls a function to process an HTML template for printing labels. To convert it to generate a PDF, we’ll use Magento’s built-in PDF generation features.
Replace the contents of the massprintAction method with the following code:
public function massprintAction()
{
$orderIds = $this->getRequest()->getParam('order_ids');
if (!is_array($orderIds)) {
$this->_redirect('adminhtml/sales_order/');
}
// Create a new PDF document
$labelPDF = Mage::getModel('core/pdf_document')->create();
foreach ($orderIds as $orderId) {
// Load the order
$order = Mage::getModel('sales/order')->load($orderId);
// Add order data to the PDF (you can customize this part)
$labelPDF->addPage(Mage::helper('sales')->__('Order #%d - %s', $order->getIncrementId(), $order->getShippingName()));
}
// Generate and display the PDF
if ($labelPDF) {
$fileName = 'labels.pdf';
Mage::getModel('core/file_transfer_objects_storage')->saveFile(Mage::helper('sales')->__($fileName), $labelPDF->render());
$this->_prepareDownloadResponse(
$fileName,
'application/pdf'
);
}
}
This code loads the selected orders, generates a PDF document, adds order information to each page of the document, and saves it as a downloadable file.
Conclusion
With this modification, clicking the ‘Print Labels’ action in your Magento admin panel should now generate an invoice.pdf file, just like the ‘Print Invoice’ function does. This streamlines your printing process and makes it more efficient.
Remember to clear your cache after making these changes:
php bin/magento cache:clean
If you found this tutorial helpful, consider reading our other Magento optimization guides for more tips on improving performance and user experience in your e-commerce store.
Happy printing!