PrintAction.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Sales\Controller\Adminhtml\Shipment\AbstractShipment;
  8. use Magento\Backend\App\Action\Context;
  9. use Magento\Framework\App\ResponseInterface;
  10. use Magento\Framework\App\Filesystem\DirectoryList;
  11. use Magento\Framework\App\Response\Http\FileFactory;
  12. use Magento\Backend\Model\View\Result\ForwardFactory;
  13. abstract class PrintAction extends \Magento\Backend\App\Action
  14. {
  15. /**
  16. * Authorization level of a basic admin session
  17. *
  18. * @see _isAllowed()
  19. */
  20. const ADMIN_RESOURCE = 'Magento_Sales::shipment';
  21. /**
  22. * @var FileFactory
  23. */
  24. protected $_fileFactory;
  25. /**
  26. * @var ForwardFactory
  27. */
  28. protected $resultForwardFactory;
  29. /**
  30. * @param Context $context
  31. * @param FileFactory $fileFactory
  32. * @param ForwardFactory $resultForwardFactory
  33. */
  34. public function __construct(
  35. Context $context,
  36. FileFactory $fileFactory,
  37. ForwardFactory $resultForwardFactory
  38. ) {
  39. $this->_fileFactory = $fileFactory;
  40. $this->resultForwardFactory = $resultForwardFactory;
  41. parent::__construct($context);
  42. }
  43. /**
  44. * @return ResponseInterface|\Magento\Backend\Model\View\Result\Forward
  45. * @throws \Exception
  46. */
  47. public function execute()
  48. {
  49. $shipmentId = $this->getRequest()->getParam('shipment_id');
  50. if ($shipmentId) {
  51. $shipment = $this->_objectManager->create(\Magento\Sales\Model\Order\Shipment::class)->load($shipmentId);
  52. if ($shipment) {
  53. $pdf = $this->_objectManager->create(
  54. \Magento\Sales\Model\Order\Pdf\Shipment::class
  55. )->getPdf(
  56. [$shipment]
  57. );
  58. $date = $this->_objectManager->get(
  59. \Magento\Framework\Stdlib\DateTime\DateTime::class
  60. )->date('Y-m-d_H-i-s');
  61. $fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
  62. return $this->_fileFactory->create(
  63. 'packingslip' . $date . '.pdf',
  64. $fileContent,
  65. DirectoryList::VAR_DIR,
  66. 'application/pdf'
  67. );
  68. }
  69. } else {
  70. /** @var \Magento\Backend\Model\View\Result\Forward $resultForward */
  71. $resultForward = $this->resultForwardFactory->create();
  72. return $resultForward->forward('noroute');
  73. }
  74. }
  75. }