0

How to open an invoice pdf in a new tab in the Magento backend?

How to see the preview of the invoice print in a new tab(target _blank) via the Magento invoice grid

I have added the new column "Actual View" in Sales-> Invoice grid in Magento backend like below. enter image description here

I have customized this column, when you click this Actual view link, it will download the invoice that was sent to the customer.

But I want to open this invoice in a separate browser tab instead of downloading it.

Please refer the below code

sales_order_invoice_grid.xml

<!-- Add the column "custom_column" (Custom Column) to the sales invoice grid. --> <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_invoice_columns">
        <actionsColumn name="action" class="Vendorname\InvoiceGridCustomization\Ui\Component\Listing\Column\ViewAction">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="viewUrlPath" xsi:type="string">sales/order_invoice/print</item>
                    <item name="urlEntityParamName" xsi:type="string">invoice_id</item>
                </item>
            </argument>
            <settings>
                <label translate="true">Actual view</label>
                <indexField>increment_id</indexField>
            </settings>
        </actionsColumn>
    </columns> </listing>

Vendorname\InvoiceGridCustomization\Ui\Component\Listing\Column\ViewAction.php

<?php

namespace Vendor\Modulename\Ui\Component\Listing\Column;

use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;

/**
 * Class ViewAction
 */
class ViewAction extends Column
{
    /**f
     * @var UrlInterface
     */
    protected $urlBuilder;

    /**
     * Constructor
     *
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param UrlInterface $urlBuilder
     * @param array $components
     * @param array $data
     */
    public function __construct(
        ContextInterface   $context,
        UiComponentFactory $uiComponentFactory,
        UrlInterface       $urlBuilder,
        array              $components = [],
        array              $data = []
    )
    {
        $this->urlBuilder = $urlBuilder;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                if (isset($item['entity_id'])) {
                    $viewUrlPath = $this->getData('config/viewUrlPath') ?: '#';
                    $urlEntityParamName = $this->getData('config/urlEntityParamName') ?: 'entity_id';


                    $url = $this->urlBuilder->getUrl(
                        $viewUrlPath,
                        [
                            $urlEntityParamName => $item['entity_id']
                        ]
                    );
                    $item[$this->getData('name')]['edit'] = [
                        'href' => $url,
                        'target' => '_blank',
                        'label' => __('Print'),

                    ];
                }
            }
        }

        return $dataSource;
    }
}

1 Answer 1

2

To indicate to the browser that the file should be viewed in the browser, the HTTP response should include these headers:

Content-Type: application/pdf
Content-Disposition: inline; filename="filename.pdf"

The quotes around the filename are required if the filename contains special characters such as filename[1].pdf which may otherwise break the browser's ability to handle the response.

Also note that individual settings on your browser can have an override effect on this behavior. By default, when you open a pdf file in firefox, it will provide you with a popup to either save the pdf file or to open it directly and there is also a check box which says do this action automatically from now on.

Due to this, pdfs will be downloaded rather than opened in a new tab, even if all the required headers are in the response. I would double check your browser's settings to verify your preferred behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.