Skip to content

Commit b358932

Browse files
rogyartiagosampaio
authored andcommitted
Remove PDF invoice after generation
1 parent fe11b39 commit b358932

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

app/code/Magento/Sales/Controller/Adminhtml/Invoice/AbstractInvoice/PrintAction.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ public function execute()
5858
$date = $this->_objectManager->get(
5959
\Magento\Framework\Stdlib\DateTime\DateTime::class
6060
)->date('Y-m-d_H-i-s');
61+
$fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
62+
6163
return $this->_fileFactory->create(
6264
'invoice' . $date . '.pdf',
63-
$pdf->render(),
65+
$fileContent,
6466
DirectoryList::VAR_DIR,
6567
'application/pdf'
6668
);

lib/internal/Magento/Framework/App/Response/Http/FileFactory.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ public function create(
7676
->setHeader('Pragma', 'public', true)
7777
->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
7878
->setHeader('Content-type', $contentType, true)
79-
->setHeader('Content-Length', $contentLength === null ? strlen($content) : $contentLength, true)
79+
->setHeader(
80+
'Content-Length',
81+
$contentLength === null ? strlen($this->getFileContent($content)) : $contentLength,
82+
true
83+
)
8084
->setHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"', true)
8185
->setHeader('Last-Modified', date('r'), true);
8286

@@ -88,7 +92,8 @@ public function create(
8892
echo $stream->read(1024);
8993
}
9094
} else {
91-
$dir->writeFile($fileName, $content);
95+
$dir->writeFile($fileName, $this->getFileContent($content));
96+
$file = $fileName;
9297
$stream = $dir->openFile($fileName, 'r');
9398
while (!$stream->eof()) {
9499
echo $stream->read(1024);
@@ -102,4 +107,19 @@ public function create(
102107
}
103108
return $this->_response;
104109
}
110+
111+
/**
112+
* Returns file content for writing.
113+
*
114+
* @param string|array $content
115+
* @return string
116+
*/
117+
private function getFileContent($content)
118+
{
119+
if (isset($content['type']) && $content['type'] == 'string') {
120+
return $content['value'];
121+
}
122+
123+
return $content;
124+
}
105125
}

0 commit comments

Comments
 (0)