Skip to content

Commit

Permalink
Remove PDF invoice after generation
Browse files Browse the repository at this point in the history
  • Loading branch information
rogyar authored and tiagosampaio committed Jul 30, 2018
1 parent fe11b39 commit b358932
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ public function execute()
$date = $this->_objectManager->get(
\Magento\Framework\Stdlib\DateTime\DateTime::class
)->date('Y-m-d_H-i-s');
$fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];

return $this->_fileFactory->create(
'invoice' . $date . '.pdf',
$pdf->render(),
$fileContent,
DirectoryList::VAR_DIR,
'application/pdf'
);
Expand Down
24 changes: 22 additions & 2 deletions lib/internal/Magento/Framework/App/Response/Http/FileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public function create(
->setHeader('Pragma', 'public', true)
->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
->setHeader('Content-type', $contentType, true)
->setHeader('Content-Length', $contentLength === null ? strlen($content) : $contentLength, true)
->setHeader(
'Content-Length',
$contentLength === null ? strlen($this->getFileContent($content)) : $contentLength,
true
)
->setHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"', true)
->setHeader('Last-Modified', date('r'), true);

Expand All @@ -88,7 +92,8 @@ public function create(
echo $stream->read(1024);
}
} else {
$dir->writeFile($fileName, $content);
$dir->writeFile($fileName, $this->getFileContent($content));
$file = $fileName;
$stream = $dir->openFile($fileName, 'r');
while (!$stream->eof()) {
echo $stream->read(1024);
Expand All @@ -102,4 +107,19 @@ public function create(
}
return $this->_response;
}

/**
* Returns file content for writing.
*
* @param string|array $content
* @return string
*/
private function getFileContent($content)
{
if (isset($content['type']) && $content['type'] == 'string') {
return $content['value'];
}

return $content;
}
}

0 comments on commit b358932

Please sign in to comment.