Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MassPrintShippingLabel.php
Go to the documentation of this file.
1 <?php
8 
18 use Magento\Sales\Model\ResourceModel\Order\Shipment\CollectionFactory;
19 
20 class MassPrintShippingLabel extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
21 {
27  const ADMIN_RESOURCE = 'Magento_Sales::shipment';
28 
32  protected $labelGenerator;
33 
37  protected $fileFactory;
38 
46  public function __construct(
47  Context $context,
51  CollectionFactory $collectionFactory
52  ) {
53  $this->collectionFactory = $collectionFactory;
54  $this->fileFactory = $fileFactory;
55  $this->labelGenerator = $labelGenerator;
56  parent::__construct($context, $filter);
57  }
58 
66  protected function massAction(AbstractCollection $collection)
67  {
68  $labelsContent = [];
69 
70  if ($collection->getSize()) {
72  foreach ($collection as $shipment) {
73  $labelContent = $shipment->getShippingLabel();
74  if ($labelContent) {
75  $labelsContent[] = $labelContent;
76  }
77  }
78  }
79 
80  if (!empty($labelsContent)) {
81  $outputPdf = $this->labelGenerator->combineLabelsPdf($labelsContent);
82  return $this->fileFactory->create(
83  'ShippingLabels.pdf',
84  $outputPdf->render(),
86  'application/pdf'
87  );
88  }
89 
90  $this->messageManager->addError(__('There are no shipping labels related to selected shipments.'));
91  return $this->resultRedirectFactory->create()->setPath('sales/shipment/');
92  }
93 }
__()
Definition: __.php:13
__construct(Context $context, Filter $filter, FileFactory $fileFactory, LabelGenerator $labelGenerator, CollectionFactory $collectionFactory)
foreach($order->getItems() as $orderItem) $shipment