Creating a Download Link for a PDF File in a Magento2 Custom Module
In this guide, we’ll walk you through the process of creating a download link for a PDF file located within your custom Magento2 module’s web directory.
Understanding the Current Setup
You’ve already created a simple custom module with a routing configuration. The current setup looks like this:
- Module name:
Magento_Hello - FrontName:
hello
The routing file (app\code\Magento\Hello\etc\frontend\routes.xml) is configured as follows:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="hello" frontName="hello">
<module name="Magento_Hello"/>
</route>
</router>
</config>
The PDF File Location
You have a PDF file named test.pdf located at the following path within your module:
app\code\Magento\Hello\view\frontend\web\pdf\test.pdf
Our goal is to create a download link for this PDF file on one of the pages generated by your custom module.
Creating the Download Link
To create a download link, you’ll need to add some code in your Blade file (assuming you’re using Magento2’s built-in Blade templating engine). Let’s assume you have a page called hello-world.phtml located at:
app/design/frontend/{Vendor}/{Theme}/Magento_Hello/templates/hello-world.phtml
Inside this file, you can add the following line to create a download link for your PDF file:
<a href="{{ view.url('Magento_Hello/test/pdf/test.pdf') }}" target="_blank">Download Test PDF</a>
In this code:
{{ view.url('Magento_Hello/test/pdf/test.pdf') }}generates the URL for the PDF file.- The
target="_blank"attribute opens the link in a new tab.
Adding the Download Functionality
To make the above link work, you need to create a controller action that handles the request and forces the download of the PDF file. Create a new controller under your custom module:
app\code\Magento\Hello\Controller\Index\PdfController.php
In this file, add the following code:
<?php
namespace Magento\Hello\Controller\Index;
class PdfController extends \Magento\Framework\App\Action\Action
{
public function execute()
{
$filePath = BP . '/pub/media/pdf/test.pdf';
$content = file_get_contents($filePath);
$this->getResponse()->setHeader('Content-Type', 'application/octet-stream');
$this->getResponse()->setHeader('Content-Disposition', 'attachment; filename="test.pdf"');
$this->getResponse()->setContent($content);
}
}
In this controller:
BPis a constant representing the Magento root directory.- The controller reads the content of the PDF file using
file_get_contents(). - It sets the response headers to force the download.
Updating the Routing Configuration
Finally, update your routes.xml file to map the new controller action:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="hello" frontName="hello">
<module name="Magento_Hello"/>
</route>
<!-- Add the new route for PDF download -->
<route id="pdf-download" frontName="hello" path="/download/pdf">
<module name="Magento_Hello"/>
<action method="execute"/>
</route>
</router>
</config>
In this update:
- A new route
pdf-downloadis added with the path/download/pdf. - This route maps to your new controller action.
Conclusion
By following these steps, you should now have a download link for your PDF file within your custom Magento2 module. The link will open in a new tab and allow users to download the test.pdf file located in the specified directory.
Remember to clear your cache after making changes to see the updates:
bin/magento cache:flush
Happy coding, and feel free to explore more about Magento2 module development on our blog.