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.
saveCopyzoom_out_map<?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/[Company]/[Module]/Model/Cms/Wysiwyg/Images/Storage.php and past above code into that file.
saveCopyzoom_out_map<?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.
AI-Powered Recommended Articles
How to Add Magento Ui Image Uploader on Store Configuration in Magento 2
Enable image uploads in the store configuration section of Magento 2 for a more versatile admin experience.
Magento 2 Add Image Uploader on Admin UI Form
Learn how to add an image uploader field on admin UI forms in Magento 2 for better media handling in backend.
How To Get Resize Image in Custom Module
Learn how to retrieve and resize images in your custom module in Magento 2.
How to Resize Product Images Programmatically in Magento 2
Learn how to resize product images programmatically in Magento 2 for faster loading times and better performance.
How to Resize Specific SKU Images in Magento 2 Using CSV
Learn how to resize specific product SKU images in Magento 2 using CSV to efficiently manage product images.
Magento 2 Add Image on Admin UI Grid
Learn how to add product images to the admin grid in Magento 2 to enhance product visibility in the backend.