12

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 10

14

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

5
  • Nice one.Works fine on 2.1.10
    – vbak
    Dec 22, 2017 at 9:05
  • @Mr. Lewis will it work for .stl file format ? and how to install this on magento 2 ? any manual ?
    – ephemeral
    Jan 19, 2018 at 14:15
  • You can find it in the readme
    – Mr. Lewis
    Jan 19, 2018 at 14:33
  • I didnt get installation manual
    – Learner
    Jan 26, 2018 at 9:24
  • still works on 2.3.2
    – Julime
    Nov 12, 2019 at 8:51
4

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 adobe pdf icon) 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>
1
4

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

3

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>
3
  • 2
    Make sure that you should create a module and override the di.xml there Jul 11, 2016 at 9:41
  • 1
    That's not working for me... :( (cache cleared)
    – Karol F
    Jul 28, 2016 at 11:30
  • 1
    Also 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.
    – user2450
    Mar 15, 2017 at 11:17
1

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.

0

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

0

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 .

0

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.

Dont Modify Core Code

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.

Add pdf as Allowable File Type in Magento

2
  • 3
    This is a good reason why link only answers don't work - that bottom link no longer works.
    – Lewis
    Aug 20, 2020 at 14:06
  • 1
    You should never link to the answer. Jan 7, 2022 at 16:02
0

Use this code in di.xml working fine

application/zip application/pdf

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.