add id to pdf file name

pull/11/head
Dan Gowans 2025-04-29 14:57:07 -04:00
parent 0dcdfaaa8a
commit 2c506711fb
2 changed files with 30 additions and 11 deletions

View File

@ -20,7 +20,13 @@ export async function handler(request, response, next) {
const reportData = await getReportData(printConfig, request.query); const reportData = await getReportData(printConfig, request.query);
const reportPath = path.join('views', 'print', 'pdf', `${printName}.ejs`); const reportPath = path.join('views', 'print', 'pdf', `${printName}.ejs`);
function pdfCallbackFunction(pdf) { function pdfCallbackFunction(pdf) {
response.setHeader('Content-Disposition', `${attachmentOrInline}; filename=${camelcase(printConfig?.title ?? 'export')}.pdf`); let exportFileNameId = '';
if ((printConfig?.params.length ?? 0) > 0) {
// eslint-disable-next-line @typescript-eslint/no-base-to-string, @typescript-eslint/restrict-template-expressions
exportFileNameId = `-${request.query[printConfig?.params[0] ?? '']}`;
}
const exportFileName = `${camelcase(printConfig?.title ?? 'export')}${exportFileNameId}.pdf`;
response.setHeader('Content-Disposition', `${attachmentOrInline}; filename=${exportFileName}`);
response.setHeader('Content-Type', 'application/pdf'); response.setHeader('Content-Type', 'application/pdf');
response.send(pdf); response.send(pdf);
} }
@ -32,8 +38,8 @@ export async function handler(request, response, next) {
} }
const pdf = await convertHTMLToPDF(ejsData, { const pdf = await convertHTMLToPDF(ejsData, {
format: 'letter', format: 'letter',
printBackground: true, preferCSSPageSize: true,
preferCSSPageSize: true printBackground: true
}, { }, {
usePackagePuppeteer: true usePackagePuppeteer: true
}); });

View File

@ -53,9 +53,18 @@ export async function handler(
const reportPath = path.join('views', 'print', 'pdf', `${printName}.ejs`) const reportPath = path.join('views', 'print', 'pdf', `${printName}.ejs`)
function pdfCallbackFunction(pdf: Buffer): void { function pdfCallbackFunction(pdf: Buffer): void {
let exportFileNameId = ''
if ((printConfig?.params.length ?? 0) > 0) {
// eslint-disable-next-line @typescript-eslint/no-base-to-string, @typescript-eslint/restrict-template-expressions
exportFileNameId = `-${request.query[printConfig?.params[0] ?? '']}`
}
const exportFileName = `${camelcase(printConfig?.title ?? 'export')}${exportFileNameId}.pdf`
response.setHeader( response.setHeader(
'Content-Disposition', 'Content-Disposition',
`${attachmentOrInline}; filename=${camelcase(printConfig?.title ?? 'export')}.pdf` `${attachmentOrInline}; filename=${exportFileName}`
) )
response.setHeader('Content-Type', 'application/pdf') response.setHeader('Content-Type', 'application/pdf')
@ -73,13 +82,17 @@ export async function handler(
return return
} }
const pdf = await convertHTMLToPDF(ejsData, { const pdf = await convertHTMLToPDF(
format: 'letter', ejsData,
printBackground: true, {
preferCSSPageSize: true format: 'letter',
}, { preferCSSPageSize: true,
usePackagePuppeteer: true printBackground: true
}) },
{
usePackagePuppeteer: true
}
)
pdfCallbackFunction(Buffer.from(pdf)) pdfCallbackFunction(Buffer.from(pdf))
} }