0

i have my function that generate my PDF, this works fine (the function is in my block).

After that, i want to show the window with this (this is in my controller)

ob_start();
header("Content-Length: " . filesize ( $fullUrl ) );
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=".basename($fullUrl));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
ob_clean();
flush();
readfile($fullUrl);

but readfile doesn't do anything. Why ?

1

Please try below code

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $fileFactory = $objectManager->create('\Magento\Framework\App\Response\Http\FileFactory');
    $fileName = 'example'.\date('m-d-Y_hia').'.txt';
    $pdfObject = @file_get_contents($fullUrl);
    $fileFactory->create(
               $fileName,
               $pdfObject,
               \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR,
               'application/pdf'
            );
| improve this answer | |
  • My file already exists, could you please give explanation ? – Morgan Tartreau Jan 23 '19 at 9:00
  • Above code downloads pdf from file you provided. Do you want to preview pdf or you want download it. – Satish Gumudavelly Jan 23 '19 at 10:06
  • Yeah, downloads would be great. But could you please add the code to preview the pdf? Could be useful – Morgan Tartreau Jan 23 '19 at 10:54

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.