In my ExpressJS application, I have a method for generating a PDF file and sending it to the client. However, there are cases where I need to retrieve an existing local PDF file and return it as the response.
I'm unsure how to handle this scenario.
import express from 'express'
import PDFDocument from 'pdfkit'
const router = express.Router()
router.get('/pdf/:id?',
async (req, res) => {
const { id } = req.params
if (id) {
// Need assistance with reading and outputting a local PDF file here
} else {
const doc = new PDFDocument()
res.setHeader('Content-disposition', 'inline; filename="output.pdf"')
res.setHeader('Content-type', 'application/pdf')
doc
.rect(60, 50, 200, 120)
.fontSize(8)
.text('some text', 64, 54)
.stroke()
doc.pipe(res)
doc.end()
}
}
)
export default router