0

I need to provide a download link in my footer where customers get the revocation form.

I have the following in my phtml filebut it is not working. The PDF document is in pub/media/pdf/Muster-Widerrufsformular.pdf

<li class="nav item"><a href="<?php echo $this->getMediaUrl('pdf/Muster-Widerrufsformular.pdf');?>"><?php echo __('Revocation Form') ?></a></li>

I also tried

<a href="{{media url="pdf/Muster-Widerrufsformular.pdf"}}" target="_blank"></a>    

can anyone help? Thanks!

0

You can get media URL by objectManager method directly in your phtml file.

    <?php 
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $mediaUrl = $objectManager->get('Magento\Store\Model\StoreManagerInterface')
                        ->getStore()
                        ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    ?>

<li class="nav item"><a href="<?php echo $mediaUrl.'pdf/Muster-Widerrufsformular.pdf';?>"><?php echo __('Revocation Form') ?></a></li>

or you can Get By Dependency Injection method

protected $_storeManager;

public function __construct( 
    ...
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    ...
) {
    ...
    $this->_storeManager = $storeManager;
    ...
}

public function getMediaUrl()
{
    $mediaUrl = $this->_storeManager
                     ->getStore()
                     ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    return $mediaUrl;
}

and then you can call this method in phtml file like the below way.

<li class="nav item"><a href="<?php echo $block->getMediaUrl().'pdf/Muster-Widerrufsformular.pdf';?>"><?php echo __('Revocation Form') ?></a></li>
| improve this answer | |
  • It is unfortunately not working, it causes a 404. The generated URL is media/pdf/Muster-Widerrufsformular.pdf, the file path is: pub/media/pdf/Muster-Widerrufsformular.pdf – Thomas Schott Apr 16 at 6:36
  • Have you used the object manager method from my answer? – Kishor Thummar Apr 16 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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