Untitled

 avatar
unknown
plain_text
13 days ago
837 B
3
Indexable
router.get('/download/:id', auth, async(req, res) => {
    try {
        const invoice = await Invoice.findOne({
            where: { id: req.params.id, user_id: req.user.id }
        });

        if (!invoice || !invoice.file) {
            return res.status(404).json({ error: 'Invoice PDF not found' });
        }

        // Set the response headers for file download
        res.setHeader('Content-Type', 'application/pdf');
        res.setHeader('Content-Disposition', `attachment; filename=invoice_${invoice.invoice_id}.pdf`);

        // Send the file data as the response
        res.send(invoice.file); // Send the buffer directly
    } catch (error) {
        console.error('Error downloading invoice PDF:', error);
        res.status(500).json({ error: 'Server error', details: error.message });
    }
});
Leave a Comment