How to create a pdf in magento?

How to create a pdf in magento?

July 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to create a pdf in magento?

Creating a PDF of the product list in the Magento admin backend is a common requirement for many stores. Here’s how you can achieve this using Magento 2.

Step 1: Create a Custom Block

First, let’s create a custom block that will generate the product grid content we want to export as a PDF.

In your custom module folder (e.g., app/code/Vendor/Module), navigate to Block/Adminhtml/Web and create a new file called Web.php.

<?php

namespace Vendor\Module\Block\Adminhtml\Web;

class Web extends \Magento\Framework\View\Element\Template
{
    public function getPdfFile()
    {
        $collection = $this->getProductCollection();
        $gridContent = $this->getLayout()->createBlock('Magento\Backend\Block\Widget\Grid')
            ->setCollection($collection)
            ->renderViews();

        // Convert the grid content to a string
        $htmlContent = new \Zend_Pdf_Exception();
        $htmlContent->parseString($gridContent);

        return $htmlContent;
    }

    private function getProductCollection()
    {
        $collection = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Catalog\Model\ResourceModel\Product\Collection');
        return $collection;
    }
}

This block first retrieves a collection of all products and then renders it as a grid using the built-in Magento grid functionality. The getPdfFile method converts this HTML content into a format that can be used by the PDF generator.

Step 2: Create the Export Action

Next, we need to create an action in our admin controller that will handle the export request and trigger the download of the PDF file.

Navigate to app/code/Vendor/Module/controllers/Adminhtml/WebController.php and update your exportPdfAction() method:

public function exportPdfAction()
{
    $fileName = 'products.pdf';
    $content = $this->getLayout()->createBlock('Vendor\Module\Block\Adminhtml\Web\Web')->getPdfFile();
    $this->_prepareDownloadResponse($fileName, $content);
}

In this method, we are creating a new instance of our custom block and calling the getPdfFile method to generate the HTML content. We then use Magento’s built-in _prepareDownloadResponse method to prepare the PDF file for download.

Step 3: Update Your Menu Config

Finally, you need to update your menu configuration to include this new action in the admin backend navigation menu.

In app/code/Vendor/Module/etc/adminhtml/menu.xml, add the following:

<menu>
    <add id="Vendor_Module::web" title="Products" module="Vendor_Module"
         sortOrder="100" parent="Magento_Catalog::catalog_products_index"/>
</menu>

This configuration tells Magento to include a new menu item under the “Catalog” > “Products” section in the admin backend.

Conclusion

By following these steps, you should now have a fully functional PDF export feature for your product list in the Magento 2 admin backend. Remember to replace Vendor with your actual vendor name and Module with your module name in all the file paths.

For more advanced customization options and to learn how to handle larger datasets efficiently, consider exploring the PDF generation best practices on our blog at LaravelCompany.

Magento 2 Catalog Extensions Themes

Our network