Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Pdfinvoices.php
Go to the documentation of this file.
1 <?php
7 
17 use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;
19 
23 class Pdfinvoices extends \Magento\Sales\Controller\Adminhtml\Order\PdfDocumentsMassAction
24 {
28  const ADMIN_RESOURCE = 'Magento_Sales::invoice';
29 
33  protected $fileFactory;
34 
38  protected $dateTime;
39 
43  protected $pdfInvoice;
44 
48  protected $collectionFactory;
49 
58  public function __construct(
59  Context $context,
65  ) {
66  $this->fileFactory = $fileFactory;
67  $this->dateTime = $dateTime;
68  $this->pdfInvoice = $pdfInvoice;
69  $this->collectionFactory = $collectionFactory;
70  parent::__construct($context, $filter);
71  }
72 
81  {
82  $invoicesCollection = $this->collectionFactory->create()->setOrderFilter(['in' => $collection->getAllIds()]);
83  if (!$invoicesCollection->getSize()) {
84  $this->messageManager->addErrorMessage(__('There are no printable documents related to selected orders.'));
85  return $this->resultRedirectFactory->create()->setPath($this->getComponentRefererUrl());
86  }
87  $pdf = $this->pdfInvoice->getPdf($invoicesCollection->getItems());
88  $fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
89 
90  return $this->fileFactory->create(
91  sprintf('invoice%s.pdf', $this->dateTime->date('Y-m-d_H-i-s')),
92  $fileContent,
93  DirectoryList::VAR_DIR,
94  'application/pdf'
95  );
96  }
97 }
__()
Definition: __.php:13
massAction(AbstractCollection $collection)
Definition: Pdfinvoices.php:80
__construct(Context $context, Filter $filter, CollectionFactory $collectionFactory, DateTime $dateTime, FileFactory $fileFactory, Invoice $pdfInvoice)
Definition: Pdfinvoices.php:58