Skip to content

Commit bedab4d

Browse files
committed
Improve File Cleanup
1 parent 7661570 commit bedab4d

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

pdf-service.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ app.post('/from-html', async (req, res) => {
5757
let pdfFilePath;
5858
try {
5959
pdfFilePath = await generatePdf(`file://${fullHtmlPath}`, req.query.media, options);
60-
61-
fs.unlinkSync(fullHtmlPath);
6260
} catch (err) {
6361
log('/from-html: error generating PDF', err);
6462
deliverJson(res, {msg: 'failure generating PDF', err}, 500);
6563

6664
return;
65+
} finally {
66+
fs.unlinkSync(fullHtmlPath);
6767
}
6868

6969
const compressedFilePath = `${pdfFilePath}_compressed.pdf`;
@@ -128,15 +128,7 @@ function deliverJson(res, resp, status = 200) {
128128
function deliverPdfFile(res, pdfFilePath) {
129129
log('deliverPdfFile: going to deliver PDF', pdfFilePath);
130130

131-
const reader = fs.createReadStream(pdfFilePath);
132-
133-
res.setHeader('Content-Length', fs.statSync(pdfFilePath).size);
134-
res.setHeader('Content-Type', 'application/pdf');
135-
res.setHeader('Content-Disposition', 'inline');
136-
reader.on('open', () => {
137-
reader.pipe(res);
138-
});
139-
reader.on('close', () => {
131+
res.download(pdfFilePath, 'document.pdf', () => {
140132
log('deliverPdfFile: going to remove file', pdfFilePath);
141133
fs.unlinkSync(pdfFilePath);
142134
});

0 commit comments

Comments
 (0)