Skip to content

How to change of the packingslip pdf file name (Shipment) with the shipping ID generated from admin?

Use di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="MagentoShippingControllerAdminhtmlOrderShipmentPrintAction" type="CustomModuleControllerAdminhtmlShipmentPrintAction" />
</config>

Use PrintAction.php

<?php
namespace SingsysFilenameShipmentControllerAdminhtmlShipment;
use MagentoFrameworkAppResponseInterface;
use MagentoFrameworkAppFilesystemDirectoryList;

class PrintAction extends MagentoShippingControllerAdminhtmlOrderShipmentPrintAction
{

    public function execute()
    {
       
        $shipmentId = $this->getRequest()->getParam('shipment_id');
        if ($shipmentId) {
            $shipment = $this->_objectManager->create(MagentoSalesModelOrderShipment::class)->load($shipmentId);
            if ($shipment) {
                $pdf = $this->_objectManager->create(
                    MagentoSalesModelOrderPdfShipment::class
                )->getPdf(
                    [$shipment]
                );
                $date = $this->_objectManager->get(
                    MagentoFrameworkStdlibDateTimeDateTime::class
                )->date('Y-m-d_H-i-s');
                $fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];

                return $this->_fileFactory->create(
                    'packingslip' . $shipmentId . '.pdf',
                    $fileContent,
                    DirectoryList::VAR_DIR,
                    'application/pdf'
                );
            }
        } else {
            /** @var MagentoBackendModelViewResultForward $resultForward */
            $resultForward = $this->resultForwardFactory->create();
            return $resultForward->forward('noroute');
        }
    }
}

But i am unable to any changes.

Is it possible to change the filename of the Shiping PDF-files in Magento 2 to a format like “packingslip_100123.pdf”? We have tried with the lot of solution given by many developer’s solution but we aren’t able to change? I am using magento 2.4.4 version. Please help anyone. Thanks in advance.