I have to upload PDF files from admin gallery and insert them to CMS-page, I can see extensions available for product attributes PDF upload but I need to use them an CMS-page through gallery. Kindly advice how to achieve.
1 Answer 1
Please create the following module:
app/code/Vendor/WysiwygDownloads/registration.php:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_WysiwygDownloads',
__DIR__
);
app/code/Vendor/WysiwygDownloads/etc/module.xml:
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_WysiwygDownloads" />
</config>
app/code/Vendor/WysiwygDownloads/etc/adminhtml/di.xml :
<?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">
<plugin disabled="false" name="img_storage_custom_plugin" sortOrder="10" type="Vendor\WysiwygDownloads\Plugin\Magento\Cms\Model\Wysiwyg\Images\Storage"/>
<arguments>
<argument name="extensions" xsi:type="array">
<item name="allowed" xsi:type="array">
<item name="pdf" xsi:type="string">application/pdf</item>
</item>
<item name="image_allowed" xsi:type="array">
<item name="pdf" xsi:type="string">application/pdf</item>
</item>
</argument>
</arguments>
</type>
<preference for="Magento\Framework\Image\Adapter\Gd2" type="Vendor\WysiwygDownloads\Image\Adapter\Gd2" />
</config>
app/code/Vendor/WysiwygDownloads/Image/Adapter/Gd2.php:
<?php
namespace Vendor\WysiwygDownloads\Image\Adapter;
use Magento\Framework\Image\Adapter\Gd2 as OriginalGd2;
use Exception;
use OverflowException;
use Magento\Framework\Filesystem;
use Psr\Log\LoggerInterface;
use Vendor\WysiwygDownloads\Plugin\Magento\Cms\Model\Wysiwyg\Images\Storage;
use Magento\Framework\Exception\FileSystemException;
class Gd2 extends OriginalGd2
{
public function __construct(
Filesystem $filesystem,
LoggerInterface $logger,
array $data = []
) {
parent::__construct($filesystem, $logger, $data);
}
/**
* Open image for processing
*
* @param string $filename
* @return void
* @throws OverflowException|FileSystemException
*/
public function open($filename)
{
$pathInfo = pathinfo($filename);
if (!key_exists('extension', $pathInfo) || !in_array($pathInfo['extension'], Storage::FILE_TYPES)) {
parent::open($filename);
}
}
/**
* Save image to specific path.
* If some folders of path does not exist they will be created
*
* @param null|string $destination
* @param null|string $newName
* @return void
* @throws Exception If destination path is not writable
*/
public function save($destination = null, $newName = null)
{
$fileName = $this->_prepareDestination($destination, $newName);
$pathInfo = pathinfo($fileName);
if (!key_exists('extension', $pathInfo) || !in_array($pathInfo['extension'], Storage::FILE_TYPES)) {
parent::save($destination, $newName);
}
}
}
app/code/Vendor/WysiwygDownloads/Plugin/Magento/Cms/Model/Wysiwyg/Images/Storage.php:
<?php
namespace Vendor\WysiwygDownloads\Plugin\Magento\Cms\Model\Wysiwyg\Images;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Module\Dir\Reader;
use Magento\Framework\Filesystem;
use Magento\Cms\Model\Wysiwyg\Images\Storage as OriginalStorage;
use Magento\Framework\Module\Dir;
class Storage {
const FILE_TYPES = ['pdf'];
protected $type;
/**
* @var Reader
*/
private Reader $moduleReader;
/**
* @var Filesystem
*/
private Filesystem $filesystem;
/**
* Storage constructor.
*
* @param Reader $moduleReader
* @param Filesystem $filesystem
*/
public function __construct(
Reader $moduleReader,
Filesystem $filesystem
){
$this->moduleReader = $moduleReader;
$this->filesystem = $filesystem;
}
/**
* @param OriginalStorage $subject
* @param $type
* @return void
*/
public function beforeGetAllowedExtensions(OriginalStorage $subject, $type)
{
$this->type = $type;
}
/**
* @param OriginalStorage $subject
* @param $result
* @return array
*/
public function afterGetAllowedExtensions(OriginalStorage $subject, $result)
{
$fileTypes = self::FILE_TYPES;
return array_merge($result, $fileTypes);
}
/**
* @param OriginalStorage $subject
* @param $source
* @param $keepRatio
* @return array
*/
public function beforeResizeFile(OriginalStorage $subject, $source, $keepRatio = true)
{
$sourceInfo = explode('.', $source);
$fileExtension = end($sourceInfo);
if (strtolower($fileExtension) === 'pdf') {
$mediaPath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath() . 'pdf-icon.png';
if (!file_exists($mediaPath)) {
copy(
$this->moduleReader->getModuleDir(
Dir::MODULE_VIEW_DIR,
'Vendor_WysiwygDownloads'
) . '/adminhtml/web/images/pdf-icon.png',
$mediaPath
);
}
$source = $mediaPath;
}
return [$source, $keepRatio];
}
}
and of course the image: app/code/Vendor/WysiwygDownloads/view/adminhtml/web/images Upload this image:
You can add more file types in the FILE_TYPES const (gif, xml, zip etc), but you will need to update also the di.xml file.
This is a simplified version of this module: https://github.com/experius/Magento-2-Module-Experius-WysiwygDownloads. Credit goes to them!
Also the simplified version is running just fine on Magento 2.4.6 .
How to use ? enter image description here
Good luck !
-
the module is great, i modified a little in the allowed extensions! But working niceUnpassableWizard– UnpassableWizard2025年09月18日 10:07:57 +00:00Commented Sep 18 at 10:07
Explore related questions
See similar questions with these tags.