1

I have a custom module which should create a pdf file each time a user clicks a button. Ideally the PDF would be created in MyVendor_MyModule::pdfFiles/mypfd123.pdf but it can be created anywhere else if it can't be done inside my module folder.

after creating that pdf, I would need to get its url in order to open a new tab showing it.

I already know how to create the file, I just don't know how to tell magento that I want to place it in my module folder. Also I don't know how to get its absolute URL.

2

Doing things correctly, it's better to not create a new folder in the Magento Module architecture, pdfFiles doesn't exist. I suggest you create it in the web folder instead.

To do that :

  1. create your pdf folder inside web module folder : app/code/Vendor/Modulename/view/frontend/web/pdfFiles.

  2. create function in your block to get the pdf path:

    app/code/Vendor/Modulename/Block/Blockname.php

    protected $_moduleReader;
    
    public function __construct(
        ....
        \Magento\Framework\Module\Dir\Reader $moduleReader
    ) {
        ....
        $this->_moduleReader = $moduleReader;
    }
    
    public function getPdfDirectory(){
        $path = $this->_moduleReader->getModuleDir(
            \Magento\Framework\Module\Dir::MODULE_VIEW_DIR,
            'Vendor_Modulename'
        );
        return $path."/frontend/web/pdfFiles/" ;
    }
    
  3. Then you get that pdf path like this :

    $block->getPdfDirectory() // /var/www/magentoProject/app/code/Vendor/Modulename/view/frontend/web/pdfFiles/
    

    Good luck.

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.