diff --git a/server/controllers/invoiceController.js b/server/controllers/invoiceController.js index c5d9d70..faf6d79 100644 --- a/server/controllers/invoiceController.js +++ b/server/controllers/invoiceController.js @@ -71,8 +71,35 @@ exports.getInvoicePrint = async (req, res) => { const mode = req.query.mode === 'thermal' ? 'thermal' : 'a4'; const { renderInvoiceHtml } = require('../services/invoicePrintHtml'); const html = renderInvoiceHtml(invoice, { mode }); + + // Small on-screen toolbar (hidden in print), so the print tab isn't a + // dead-end page. Reuses the token passed for this tab to link the PDF. + const token = req.tokenFromQuery || String((req.headers && req.headers.authorization) || '').replace(/^Bearer\s+/i, ''); + const pdfFormat = mode === 'thermal' ? 'thermal80' : 'a4'; + const pdfHref = `/api/v1/invoices/${req.params.id}/pdf?format=${pdfFormat}&download=1&token=${encodeURIComponent(token)}`; + const title = String(invoice.invoiceNumber || 'Invoice').replace(/[<>&"]/g, ''); + + const chrome = ` + + +`; + res.setHeader('Content-Type', 'text/html; charset=utf-8'); - return res.send(html); + return res.send(html.replace('', `${chrome}`)); } catch (err) { console.error('Error rendering invoice HTML:', err); return sendResponse(res, 500, false, err.message || 'Server error'); diff --git a/server/middleware/auth.js b/server/middleware/auth.js index aaeb463..75c933f 100644 --- a/server/middleware/auth.js +++ b/server/middleware/auth.js @@ -223,6 +223,7 @@ const attachUserIfPresent = (req, _res, next) => { */ const attachTokenFromQuery = (req, res, next) => { if (!req.headers.authorization && req.query && typeof req.query.token === 'string') { + req.tokenFromQuery = req.query.token; req.headers.authorization = `Bearer ${req.query.token}`; delete req.query.token; }