pdfInvoice = $pdfInvoice; $this->pdfShipment = $pdfShipment; $this->pdfCreditmemo = $pdfCreditmemo; $this->fileFactory = $fileFactory; $this->dateTime = $dateTime; $this->shipmentCollectionFactory = $shipmentCollectionFactory; $this->invoiceCollectionFactory = $invoiceCollectionFactory; $this->creditmemoCollectionFactory = $creditmemoCollectionFactory; $this->collectionFactory = $orderCollectionFactory; parent::__construct($context, $filter); } /** * Print all documents for selected orders * * @param AbstractCollection $collection * @return ResponseInterface|\Magento\Backend\Model\View\Result\Redirect * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @throws \Exception */ protected function massAction(AbstractCollection $collection) { $orderIds = $collection->getAllIds(); $shipments = $this->shipmentCollectionFactory->create()->setOrderFilter(['in' => $orderIds]); $invoices = $this->invoiceCollectionFactory->create()->setOrderFilter(['in' => $orderIds]); $creditmemos = $this->creditmemoCollectionFactory->create()->setOrderFilter(['in' => $orderIds]); $documents = []; if ($invoices->getSize()) { $documents[] = $this->pdfInvoice->getPdf($invoices); } if ($shipments->getSize()) { $documents[] = $this->pdfShipment->getPdf($shipments); } if ($creditmemos->getSize()) { $documents[] = $this->pdfCreditmemo->getPdf($creditmemos); } if (empty($documents)) { $this->messageManager->addErrorMessage(__('There are no printable documents related to selected orders.')); return $this->resultRedirectFactory->create()->setPath($this->getComponentRefererUrl()); } $pdf = array_shift($documents); foreach ($documents as $document) { $pdf->pages = array_merge($pdf->pages, $document->pages); } $fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true]; return $this->fileFactory->create( sprintf('docs%s.pdf', $this->dateTime->date('Y-m-d_H-i-s')), $fileContent, DirectoryList::VAR_DIR, 'application/pdf' ); } }