fileFactory = $fileFactory; $this->collectionFactory = $collectionFactory; $this->shipmentCollectionFactory = $shipmentCollectionFactory; $this->labelGenerator = $labelGenerator; parent::__construct($context, $filter); } /** * Batch print shipping labels for whole shipments. * Push pdf document with shipping labels to user browser * * @param AbstractCollection $collection * @return ResponseInterface|ResultInterface */ protected function massAction(AbstractCollection $collection) { $labelsContent = []; $shipments = $this->shipmentCollectionFactory->create()->setOrderFilter(['in' => $collection->getAllIds()]); if ($shipments->getSize()) { /** @var \Magento\Sales\Model\Order\Shipment $shipment */ foreach ($shipments as $shipment) { $labelContent = $shipment->getShippingLabel(); if ($labelContent) { $labelsContent[] = $labelContent; } } } if (!empty($labelsContent)) { $outputPdf = $this->labelGenerator->combineLabelsPdf($labelsContent); return $this->fileFactory->create( 'ShippingLabels.pdf', $outputPdf->render(), DirectoryList::VAR_DIR, 'application/pdf' ); } $this->messageManager->addError(__('There are no shipping labels related to selected orders.')); return $this->resultRedirectFactory->create()->setPath('sales/order/'); } }