Magento 2 Allow PDF file in Wysiwyg Editor

In this post we’ll guide you how to Allow PDF file on CMS Page, CMS Block and Product page Wysiwyg Editor.

We have add this post just because lot’s of peoples finding a way for add or insert a PDF file in Wysiwyg Editor on Magento 2.

By default Magento does not provide functionality for that. So, you need to create a custom module or use your any exsisting module and create few files on that which are mentioned on below.

Make magento allow pdf files upload on back office. There’s two steps :

  • Add pdf to allowed extensions type. With that modification only, the files would 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/[Company]/[Module]/etc/di.xml and past above code into that file.
<?xml version="1.0"?> <config xmlns:xsi="" 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/[Company]/[Module]/Model/Cms/Wysiwyg/Images/Storage.php and past above code into that file.

<?php namespace [Company]\[Module]\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; } }
php bin/magento cache:clean

Once you are done with that, it’s pretty straightforward. Add the pdf just like you would do with any image link.

That’s it.

I hope this post helped you to find what you were looking for. Bookmark it for your future reference. Do comment below if you have any other questions on that.

P.S. Do share this note with your team.

Review other articles maybe it'll help you too.

Recent Articles
Chrome Extension
Copyright © 2024 All rights reserved.
Ads OFF toggle_off