6

Currently PDF files are saving in var directory.

Here is controller's execute function

 public function execute()
 { 
 $orderId = $this->getRequest()->getParam('id');
 if ($orderId) {
 $order = $this->orderRepository->get($orderId);
 if ($order) {
 $pdf = $this->orderPdfFactory->create()->getPdf([$order]);
 $date = $this->date->date('Y-m-d_H-i-s');
 return $this->fileFactory->create(
 'order' . $date . '.pdf',
 $pdf->render(),
 DirectoryList::VAR_DIR,
 'application/pdf'
 );
 }
 }
 return $this->resultRedirectFactory->create()->setPath('sales/*/view');
 }

DirectoryList::VAR_DIR,

Above code responsible for that. Now my question is that I want to save pdf files under var/pdf directory. How can I do this ?

Rakesh Jesadiya
42.5k19 gold badges135 silver badges186 bronze badges
asked Nov 9, 2016 at 10:10

2 Answers 2

5

You can do this in 2 ways:

1.The easiest way is to add in the file name the path, for example:

return $this->fileFactory->create(
 'custom/path/ . 'order' . $date . '.pdf',
 $pdf->render(),
 DirectoryList::VAR_DIR,
 'application/pdf'
);

2. Find a way to override the DirectoryList Class "Magento\Framework\App\Filesystem\DirectoryList" and add a constant with your custom path.

...
/**
* Code base root
*/
const ROOT = 'base';
const CUSTOM_PATH = 'var/pdf';
...
public static function getDefaultConfig()
{
 $result = [
 self::ROOT => [parent::PATH => ''],
 ...
 self::CUSTOM_PATH => [parent::PATH => 'var/pdf/'],
 ];
 return parent::getDefaultConfig() + $result;
}
...
answered Feb 4, 2017 at 10:23
2

Looking at other methods where a subfolder of varor media is used and given the fact, that DirectoryList::VAR_DIR returns the string value 'var' I think it's save to assume it's fine to concat it via Directory Separator (DS).

public function execute()
 { 
 $orderId = $this->getRequest()->getParam('id');
 if ($orderId) {
 $order = $this->orderRepository->get($orderId);
 if ($order) {
 $pdf = $this->orderPdfFactory->create()->getPdf([$order]);
 $date = $this->date->date('Y-m-d_H-i-s');
 return $this->fileFactory->create(
 'order' . $date . '.pdf',
 $pdf->render(),
 DirectoryList::VAR_DIR . DS . 'pdf',
 'application/pdf'
 );
 }
 }
 return $this->resultRedirectFactory->create()->setPath('sales/*/view');
 }
answered Feb 4, 2017 at 10:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.