2

Trying to create a download button for an Xlsx file. Using PHPSpreadsheet for this however when downloading the file it seems corrupted with a stack trace at the bottom of excel files:

Stack trace:
#0 [internal function]: Magento\Framework\App\ErrorHandler->handler(2, 'Cannot modify h...', '/var/www/buzzma...', 148, Array)
#1 /var/www/mage2/public_html/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php(148): setcookie('X-Magento-Vary', 'b5a9446b2918d35...', 1553779873, '/', '', true, true)
#2 /var/www/mage2/public_html/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php(101): Magento\Framework\Stdlib\Cookie\PhpCookieManager->setCookie('X-Magento-Vary', 'b5a9446b2918d35...', Array)
#3 /var/www/mage2/public_html/vendor/magento/framework/App/Response/Http.php(108): Magento\Framework\Stdlib\Cookie\PhpCookieManager->setSensitiveCookie('X-Magento-Vary', 'b5a9446b2918d35...', Object(Magento\Framework\Stdlib\Cookie\SensitiveCookieMetadata))
#4 /var/www/mage2/public_html/generated/code/Magento/Framework/App/Response/Http/Interceptor.php(37): Magento\Framework\App\Response\Http->sendVary()
#5 /var/www/mage2/public_html/vendor/magento/module-page-cache/Model/App/Response/HttpPlugin.php(25): Magento\Framework\App\Response\Http\Interceptor->sendVary()
#6 /var/www/mage2/public_html/vendor/magento/framework/Interception/Interceptor.php(121): Magento\PageCache\Model\App\Response\HttpPlugin->beforeSendResponse(Object(Magento\Framework\App\Response\Http\Interceptor))
#7 /var/www/mage2/public_html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\App\Response\Http\Interceptor->Magento\Framework\Interception\{closure}()
#8 /var/www/mage2/public_html/generated/code/Magento/Framework/App/Response/Http/Interceptor.php(117): Magento\Framework\App\Response\Http\Interceptor->___callPlugins('sendResponse', Array, Array)
#9 /var/www/mage2/public_html/vendor/magento/framework/App/Http.php(186): Magento\Framework\App\Response\Http\Interceptor->sendResponse()
#10 /var/www/mage2/public_html/vendor/magento/framework/App/Http.php(157): Magento\Framework\App\Http->handleDeveloperMode(Object(Magento\Framework\App\Bootstrap), Object(Exception))
#11 /var/www/mage2/public_html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\Http->catchException(Object(Magento\Framework\App\Bootstrap), Object(Exception))
#12 /var/www/mage2/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\App\Http\Interceptor->___callParent('catchException', Array)
#13 /var/www/mage2/public_html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\App\Http\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Bootstrap), Object(Exception))
#14 /var/www/mage2/public_html/generated/code/Magento/Framework/App/Http/Interceptor.php(39): Magento\Framework\App\Http\Interceptor->___callPlugins('catchException', Array, Array)
#15 /var/www/mage2/public_html/vendor/magento/framework/App/Bootstrap.php(264): Magento\Framework\App\Http\Interceptor->catchException(Object(Magento\Framework\App\Bootstrap), Object(Exception))
#16 /var/www/mage2/public_html/pub/index.php(40): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#17 {main}

Exiting with exit; after $objWriter->save("php://output"); seems to work fine but seems messy.

This is an example of my code within my controller:

public function buildXls($list) {
    $fileName = $this->getFileName($list) . ".xlsx";
    $rootPath = $this->directoryList->getRoot();

    $objPHPExcel = new spreadsheet;
    $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', "TEST");

    /*Original Method*/
    /*
    $objWriter = io_factory::createWriter($objPHPExcel, 'Xlsx');
    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=" . $fileName);
    header("Cache-Control: max-age=0");
    $objWriter->save("php://output");
    //FIX
   exit;        
   */
    /*Magento Method?*/
    $fileDirectory = \Magento\Framework\App\Filesystem\DirectoryList::MEDIA;

    $filePath =  $this->directoryList->getPath($fileDirectory) . "/" . $fileName;
    $objWriter = io_factory::createWriter($objPHPExcel, 'Xlsx');
    $objWriter->save($filePath);

    $resultRaw = $this->fileFactory->create(
        $fileName,
        @file_get_contents($filePath)
    );

    return $resultRaw;
    /*Magento Method End*/
}

Using the Magento method or original method used without exiting however all show as corrupt I believe because that stack trace that shows within files. How can I download this file that has been generated properly preferably the "Magento way"?

1

Tadaa:

use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Framework\App\Filesystem\DirectoryList;

public function __construct(
    FileFactory $_fileFactory,
    DateTime $dateTime
) {
    $this->_fileFactory = $_fileFactory;
    $this->dateTime = $dateTime;
}


return $this->_fileFactory->create(
        sprintf('myreport%s.xlsx', $this->dateTime->date('Y-m-d_H-i-s')), //file name during download
        [
            'type' => 'filename',
            'value' => 'spreadsheet_export/spreadsheets/' . $localName, //relative path to your locally saved spreadsheet with  $writer->save($fullpath);
            'rm' => true  // can delete file after use
        ],
        DirectoryList::MEDIA,
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    );

Check out

vendor/magento/module-offline-shipping/Controller/Adminhtml/System/Config/ExportTablerates.php
vendor/magento/module-sales/Controller/Adminhtml/Order/Pdfinvoices.php   

I have taken this out from a much larger class so add all the necessary controller paraphernalia obviously

| improve this answer | |

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.