Skip to content

Magento 2: Upload custom generated file to pub/media

I have generated a custom html file and instead of downloading I want to upload it to the pub/media programmatically.

<?php
use MagentoFrameworkAppResponseInterface;
use MagentoFrameworkAppFilesystemDirectoryList;
use MagentoMediaStorageModelFileUploaderFactory;

class PrintAction extends MagentoBackendAppAction
{
    const ADMIN_RESOURCE = 'Magento_Sales::sales_order';

    /**
     * @var MagentoFrameworkAppResponseHttpFileFactory
     */
    protected $fileFactory;

    /**
     * @var MagentoFrameworkControllerResultRedirectFactory
     */
    protected $resultRedirectFactory;

    /**
     * @var MagentoSalesApiOrderRepositoryInterface
     */
    protected $orderRepository;

    /**
     * @var MagentoFrameworkStdlibDateTimeDateTime
     */
    protected $date;

    /**
     * @var MagemechanicWorkorderModelPdfOrderFactory
     */
    protected $orderPdfFactory;

    /**
     * @param MagentoBackendAppActionContext                $context
     * @param MagentoFrameworkAppResponseHttpFileFactory   $fileFactory
     * @param MagentoSalesApiOrderRepositoryInterface        $orderRepository
     * @param MagentoFrameworkStdlibDateTimeDateTime        $date
     */
    public function __construct(
        MagentoBackendAppActionContext $context,
        MagentoFrameworkAppResponseHttpFileFactory $fileFactory,
        MagentoSalesApiOrderRepositoryInterface $orderRepository,
        MagemechanicWorkorderModelPdfOrderFactory $orderPdfFactory,
        MagentoFrameworkStdlibDateTimeDateTime $date,
        UploaderFactory $uploaderFactory,
        MagentoFrameworkFilesystem $filesystem 
    ) {
        parent::__construct($context);
        $this->fileFactory = $fileFactory;
        $this->resultRedirectFactory = $context->getResultRedirectFactory();
        $this->orderRepository = $orderRepository;
        $this->orderPdfFactory = $orderPdfFactory;
        $this->date = $date;
        $this->_url = $context->getUrl();
        $this->uploaderFactory = $uploaderFactory;
        $this->filesystem = $filesystem;
    }

    /**
     * @return MagentoBackendModelViewResultRedirect|ResponseInterface|MagentoFrameworkControllerResultRedirect|MagentoFrameworkControllerResultInterface
     * @throws Exception
     */
    public function execute()
    {
        $orderId = $this->getRequest()->getParam('order_id');
        if ($orderId) {
            $order = $this->orderRepository->get($orderId);

            if ($order->getEntityId()) {
                $pdf = $this->orderPdfFactory->create()->getPdf([$order]);
                $date = $this->date->date('Y-m-d_H-i-s');
                $createdAt = $order->getCreatedAt();
                $objectManager = MagentoFrameworkAppObjectManager::getInstance();
                $addressCollection = $objectManager->create('MagentoSalesModelResourceModelOrderAddressCollectionFactory');
                $orderBillingId = $order->getBillingAddressId();
                $orderShippingId = $order->getShippingAddressId();

                $payment = $order->getPayment();
                $method = $payment->getMethodInstance();
                $methodTitle = $method->getTitle();
                $shippingMethod = $order->getShippingMethod();

                foreach ($order->getAllItems() as $item) {
                    $productName = $item->getName();
                    $qty = $item->getQty();
                    $sku = $item->getSku();
                }                

                $html = '';

                $html .= '<section class="admin__page-section order-view-account-information">
                <div class="admin__page-section-title">
                    <span class="title">Order &amp; Account Information</span>
                </div>
                <div class="admin__page-section-content">
                    <div class="admin__page-section-item order-information">
                        <div class="admin__page-section-item-content">
                            <table class="admin__table-secondary order-information-table">
                                <tbody>
                                    <tr>
                                        <th>Order Date</th>
                                        <td>'. $createdAt . '</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    </div>
                </section>
                <div class="admin__page-section-item order-billing-address">
                <div class="admin__page-section-item-title">
                    <span class="title">Billing Address</span>
                </div>
                    <address class="admin__page-section-item-content">'.
                $order->getBillingAddress()->getFirstname(). '</br>'.
                $order->getBillingAddress()->getLastName(). '</br>'.
                implode(" ", $order->getBillingAddress()->getStreet()) . '</br>'.
                $order->getBillingAddress()->getCity(). ' '.
                $order->getBillingAddress()->getRegion(). ' '.
                $order->getBillingAddress()->getCountryId(). '</br>'.
                $order->getBillingAddress()->getTelephone(). '</br>'
                    .'
                    </address>
                </div></br>
                <div class="admin__page-section-item order-billing-address">
                <div class="admin__page-section-item-title">
                    <span class="title">Shipping Address</span>
                </div>
                    <address class="admin__page-section-item-content">'.
                    $order->getShippingAddress()->getFirstname(). '</br>'.
                    $order->getShippingAddress()->getLastName(). '</br>'.
                    implode(" ", $order->getShippingAddress()->getStreet()). '</br>'.
                    $order->getShippingAddress()->getCity(). ' '.
                    $order->getShippingAddress()->getRegion(). ' '. 
                    $order->getShippingAddress()->getCountryId().'</br>'. 
                    $order->getShippingAddress()->getTelephone() . '</br>'
                    .'
                    </address>
                </div></br>';

                $html .= '<div class="admin__page-section-item order-payment-method">
                <div class="admin__page-section-item-title">
                    <span class="title">Payment Information</span>
                </div>
                    <div class="admin__page-section-item-content">
                        <div class="order-payment-method-title">'. $methodTitle .'</div>
                        </div>
                    </div>
                </div>';

                $html .= '<div class="admin__page-section-item order-shipping-method">
                <div class="admin__page-section-item-title">
                    <span class="title">Shipping & Handling Information</span>
                </div>
                    <div class="admin__page-section-item-content">
                    '. $shippingMethod .'
                </div> </br>';

                $html .= '<section class="admin__page-section">
                <div class="admin__page-section-title">
                    <span class="title">Items Ordered</span>
                </div>
                <div class="admin__table-wrapper">
                    <table class="data-table admin__table-primary edit-order-table">
                        <thead>
                            <tr class="headings">
                                <th class="col-product"><span>Product</span></th>
                                <th class="col-ordered-qty"><span>Qty</span></th>
                            </tr>
                        </thead>
                        <tbody class="even">
                            <tr>
                                <td class="col-product">
                                    <div class="product-title">
                                        '. $productName .' </br>
                                        '. $sku .'
                                    </div>
                                </td>
                                <td class="col-ordered-qty">
                                    <table class="qty-table">
                                        <tbody>
                                            <tr>
                                                <td>'.$qty.'</td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </section>';

            // echo $html;

                $fileContent = ['type' => 'string', 'value' => $html, 'rm' => true];
                return $this->fileFactory->create(
                    __('order') . '_' . $date . '.html',
                    $fileContent,
                    DirectoryList::VAR_DIR,
                    'application/pdf'
                );

  


            }

        }
        // return $this->resultRedirectFactory->create()->setPath('sales/*/view');
    }
}



Any help would be appreciated.