8

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

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

  • Nice one.Works fine on 2.1.10 – vbak Dec 22 '17 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 '18 at 14:15
  • You can find it in the readme – Mr. Lewis Jan 19 '18 at 14:33
  • I didnt get installation manual – Learner Jan 26 '18 at 9:24
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>
2

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>
  • 1
    Make sure that you should create a module and override the di.xml there – Varun Jyothi Jul 11 '16 at 9:41
  • 1
    That's not working for me... :( (cache cleared) – Karol Fiturski Jul 28 '16 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. – Anton Evers Mar 15 '17 at 11:17
2

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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