Where can I edit the code to enable the pdf's or how can I do it on Magento 2 for uploading pdf attachments to products?
10 Answers
I have made a simple module to allow additional file types in the WYSIWYG editor. This module is available on GitHub https://github.com/experius/Magento-2-Module-Experius-WysiwygDownloads
-
-
@Mr. Lewis will it work for .stl file format ? and how to install this on magento 2 ? any manual ? Commented Jan 19, 2018 at 14:15
-
-
-
i stumbled upon the same problem und came up with an out-of-the-box solution, that only requires ftp/sftp-access:
- upload an pdf-icon (something like ) into a folder (named "pdf" for example)
- upload all your pdf files via ftp/sftp into the same folder (under /[webroot]/pub/media/wysiwyg/pdf (if you named the folder "pdf") )
- if you want to link to a pdf, embed the icon and put a link that directs to the pdf in question around the icon. that's it.
addition: you have to enable that pdf are displayed in the wysywig editor. create a simple module with an di.xml that adds "pdf" to the allowed file extensions and looks like this:
<?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>
<item name="image_allowed" xsi:type="array">
<item name="pdf" xsi:type="number">1</item>
</item>
</argument>
</arguments>
</type>
</config>
Create a di.xml in a custom module and paste the below code in the di.xml. This will allow the user to upload pdf in admin WYSIWYG editor.
<type name="Magento\Cms\Model\Wysiwyg\Images\Storage">
<arguments>
<argument name="resizeParameters" xsi:type="array">
<item name="height" xsi:type="number">75</item>
<item name="width" xsi:type="number">100</item>
</argument>
<argument name="extensions" xsi:type="array">
<item name="allowed" xsi:type="array">
<item name="jpg" xsi:type="string">image/jpg</item>
<item name="jpeg" xsi:type="string">image/jpeg</item>
<item name="png" xsi:type="string">image/png</item>
<item name="gif" xsi:type="string">image/gif</item>
<item name="pdf" xsi:type="string">application/pdf</item>
</item>
<item name="image_allowed" xsi:type="array">
<item name="jpg" xsi:type="string">image/jpg</item>
<item name="jpeg" xsi:type="string">image/jpeg</item>
<item name="png" xsi:type="string">image/png</item>
<item name="gif" xsi:type="string">image/gif</item>
</item>
<item name="media_allowed" xsi:type="array">
<item name="flv" xsi:type="string">video/x-flv</item>
<item name="avi" xsi:type="string">video/x-msvideo</item>
<item name="mov" xsi:type="string">video/x-sgi-movie</item>
<item name="rm" xsi:type="string">application/vnd.rn-realmedia</item>
<item name="wmv" xsi:type="string">video/x-ms-wmv</item>
<item name="pdf" xsi:type="string">application/pdf</item>
</item>
</argument>
<argument name="dirs" xsi:type="array">
<item name="exclude" xsi:type="array">
<item name="captcha" xsi:type="array">
<item name="regexp" xsi:type="boolean">true</item>
<item name="name" xsi:type="string">pub[/\\]+media[/\\]+captcha[/\\]*$</item>
</item>
<item name="catalog/product" xsi:type="array">
<item name="regexp" xsi:type="boolean">true</item>
<item name="name" xsi:type="string">pub[/\\]+media[/\\]+catalog[/\\]+product[/\\]*$</item>
</item>
<item name="customer" xsi:type="array">
<item name="regexp" xsi:type="boolean">true</item>
<item name="name" xsi:type="string">pub[/\\]+media[/\\]+customer[/\\]*$</item>
</item>
<item name="downloadable" xsi:type="array">
<item name="regexp" xsi:type="boolean">true</item>
<item name="name" xsi:type="string">pub[/\\]+media[/\\]+downloadable[/\\]*$</item>
</item>
<item name="import" xsi:type="array">
<item name="regexp" xsi:type="boolean">true</item>
<item name="name" xsi:type="string">pub[/\\]+media[/\\]+import[/\\]*$</item>
</item>
<item name="theme" xsi:type="array">
<item name="regexp" xsi:type="boolean">true</item>
<item name="name" xsi:type="string">pub[/\\]+media[/\\]+theme[/\\]*$</item>
</item>
<item name="theme_customization" xsi:type="array">
<item name="regexp" xsi:type="boolean">true</item>
<item name="name" xsi:type="string">pub[/\\]+media[/\\]+theme_customization[/\\]*$</item>
</item>
<item name="tmp" xsi:type="array">
<item name="regexp" xsi:type="boolean">true</item>
<item name="name" xsi:type="string">pub[/\\]+media[/\\]+tmp[/\\]*$</item>
</item>
</item>
<item name="include" xsi:type="array"/>
</argument>
</arguments>
</type>
After that override Magento\Cms\Model\Wysiwyg\Images\Storage file and add below code to avoid the error which shows after the image upload.
if (strtolower($uploader->getFileExtension()) !== 'pdf') {
$this->resizeFile($targetPath . '/' . $uploader->getUploadedFileName(), true);
}
Add the if condition code to avoid the resize file for PDF files. Below is the function where we need to add the above code.
public function uploadFile($targetPath, $type = null)
{
if (!$this->isPathAllowed($targetPath, $this->getConditionsForExcludeDirs())) {
throw new \Magento\Framework\Exception\LocalizedException(
__('We can\'t upload the file to current folder right now. Please try another folder.')
);
}
/** @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);
if (!$uploader->checkMimeType($this->getAllowedMimeTypes($type))) {
throw new \Magento\Framework\Exception\LocalizedException(__('File validation failed.'));
}
$result = $uploader->save($targetPath);
if (!$result) {
throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t upload the file right now.'));
}
// create thumbnail
if (strtolower($uploader->getFileExtension()) !== 'pdf') {
$this->resizeFile($targetPath . '/' . $uploader->getUploadedFileName(), true);
}
return $result;
}
Thanks
You can do it by adding "pdf" in the allowed extension section under vendor/magento/module-cms/etc/di.xml
like below:
<item name="allowed" xsi:type="array">
<item name="jpg" xsi:type="number">1</item>
<item name="jpeg" xsi:type="number">1</item>
<item name="png" xsi:type="number">1</item>
<item name="gif" xsi:type="number">1</item>
<item name="pdf" xsi:type="number">1</item>
</item>
-
2Make sure that you should create a module and override the di.xml there Commented Jul 11, 2016 at 9:41
-
1
-
1Also make sure that your php.ini's upload_max_filesize value is high enough. You will see no errors in Magento if this value is too low. PDF's can be larger in size than images generally.– user2450Commented Mar 15, 2017 at 11:17
I've got Magento 2.1.x and I had to use a custom module to overwrite the settings in the module cms and enable pdf upload. Here how you get it done.
I found this solution, you can upload any file type to Product Page.
- Hide attachments for Guest
- Download counts
See https://www.mageplaza.com/blog/how-upload-product-attachments-magento-2.html
for creating module you should use like this creators [http://schogini.biz/magento_module_builder/]
<?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>
</config>
You should create some basic module and on di xml you should put this code. For me is working .
You should never modify the Magento core code because when we upgrade our code will not upgrade with it. Extensions that rely on the same class will no longer work and patches will not have the desired result.
Also...if you want to add pdf to "Allowable File Types" in Magento you can use this reference that provides step-by-step instructions by adding a simple configuration file that includes the file type along with the default types AND a module to turn this feature on/off.
-
3This is a good reason why link only answers don't work - that bottom link no longer works.– SomeoneCommented Aug 20, 2020 at 14:06
-
1
The following customization enables PDF upload in WYSIWYG editor (on Magento 2.4.2):
app/code/YourVendor/YourModule/etc/di.xml
<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="string">application/pdf</item>
</item>
<item name="media_allowed" xsi:type="array">
<item name="pdf" xsi:type="string">application/pdf</item>
</item>
</argument>
</arguments>
</type>
Upon a successful upload, you'll get the following warning that can be ignored:
Unsupported image format. File: /var/www/magento2/pub/media//your_document.pdf