In this post, I’ll guide you how to allow PDF file on CMS Page, CMS Block and Product page Wysiwyg Editor.
By default, Magento does not provide such functionality. So, you need to create a custom module or use your any existing module and create few files on that which are mentioned below.
There are two steps:
Add pdf to allowed extensions type. With that modification only, the file will upload but with an exception because Magento tries to resize the pdf.
Override the upload method to resize images only.
First, you need to create di.xml file at app/code/Vendor/Modulename/etc/di.xml and add the following code into that file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Cms\Model\Wysiwyg\Images\Storage"> <arguments> <argument name="extensions" xsi:type="array"> <item name="allowed" xsi:type="array"> <item name="pdf" xsi:type="number">1</item> </item> </argument> </arguments> </type> <preference for="Magento\Cms\Model\Wysiwyg\Images\Storage" type="[Company]\[Module]\Model\Cms\Wysiwyg\Images\Storage" /> </config> |
Second, you need to create Storage.php file at app/code/Vendor/Modulename/Model/Cms/Wysiwyg/Images/Storage.php and add the following code into that file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php namespace Vendor\Modulename\Model\Cms\Wysiwyg\Images; class Storage extends \Magento\Cms\Model\Wysiwyg\Images\Storage { public function uploadFile($targetPath, $type = null) { /** @var \Magento\MediaStorage\Model\File\Uploader $uploader */ $uploader = $this->_uploaderFactory->create(['fileId' => 'image']); $allowed = $this->getAllowedExtensions($type); if ($allowed) { $uploader->setAllowedExtensions($allowed); } $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(false); $result = $uploader->save($targetPath); if (!$result) { throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t upload the file right now.')); } // Change Start if (strtolower($uploader->getFileExtension()) !== 'pdf') { // Create Thumbnail $this->resizeFile($targetPath . '/' . $uploader->getUploadedFileName(), true); } $result['cookie'] = [ 'name' => $this->getSession()->getName(), 'value' => $this->getSession()->getSessionId(), 'lifetime' => $this->getSession()->getCookieLifetime(), 'path' => $this->getSession()->getCookiePath(), 'domain' => $this->getSession()->getCookieDomain(), ]; return $result; } } |
1 |
php bin/magento cache:clean |
I hope this post helped you to find what you were looking for.
If you want get query string params in controller file,...
Create di.xml and add the below code Magemonkey/Redirect/etc/frontend/di.xml [crayon-62867191ab40e454994816/] Create...
You can try below code to change local date to...
Step 1: First you need to add registration.php file in...
Step1 : Override message.js in current theme file on the...