Skip to content

Commit abd29c8

Browse files
fixed content dispos to include asian stuff
1 parent dd64900 commit abd29c8

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

src/pdf/pdf.controller.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
Req,
99
Query,
1010
} from '@nestjs/common';
11-
import { ApiBearerAuth, ApiQuery, ApiSecurity, ApiTags } from '@nestjs/swagger';
11+
import { ApiBearerAuth, ApiOkResponse, ApiProduces, ApiQuery, ApiSecurity, ApiTags } from '@nestjs/swagger';
1212
import { Response } from 'express';
1313
import { PdfService } from './pdf.service';
1414
import { SupabaseAuthGuard } from 'src/auth/guards/supabase.guard';
@@ -36,6 +36,24 @@ export class PdfController {
3636
type: 'string',
3737
description: 'End date (format: YYYY-MM-DD)',
3838
})
39+
@ApiOkResponse({
40+
description: 'PDF file',
41+
headers: {
42+
'Content-Disposition': {
43+
description: 'Indicates a file attachment with filename',
44+
schema: { type: 'string' },
45+
},
46+
},
47+
content: {
48+
'application/pdf': {
49+
schema: {
50+
type: 'string',
51+
format: 'binary',
52+
},
53+
},
54+
},
55+
})
56+
@ApiProduces('application/pdf')
3957
async getFile(
4058
@Res() res: Response,
4159
@Req() req,
@@ -59,16 +77,20 @@ export class PdfController {
5977
const user_id = req.user.id;
6078

6179
// Pass the dates to your service if needed:
62-
const pdfBuffer = await this.pdfService.createPdfBinary(
80+
const reportResponse = await this.pdfService.createPdfBinary(
6381
user_id,
6482
devEui,
6583
start,
6684
end,
6785
);
6886

87+
const pdfBuffer = reportResponse.pdf;
88+
const encodedFilename = encodeURIComponent(reportResponse.fileName);
89+
const asciiFilename = "report.pdf";
90+
console.log(reportResponse.fileName)
6991
res.set({
92+
"Content-Disposition": `attachment; filename="${asciiFilename}"; filename*=UTF-8''${encodedFilename}`,
7093
'Content-Type': 'application/pdf',
71-
'Content-Disposition': 'attachment; filename="report.pdf"',
7294
'Content-Length': pdfBuffer.length,
7395
});
7496

src/pdf/pdf.service.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class PdfService {
2020
private readonly profileService: ProfilesService
2121
) { }
2222

23-
public async createPdfBinary(user_id: string, devEui: string, start: Date, end: Date): Promise<Buffer> {
23+
public async createPdfBinary(user_id: string, devEui: string, start: Date, end: Date) {
2424
if (!user_id) throw new Error('User ID is required');
2525
if (!devEui) throw new Error('DevEui is required');
2626
let rawData = await this.fetchDataAndReportFromDB(devEui, user_id, start, end);
@@ -44,9 +44,14 @@ export class PdfService {
4444
{ name: 'notice', min: -17.99, max: -15.1, color: 'yellow' },
4545
{ name: 'normal', min: -18, max: -1000, color: 'white' }
4646
];
47-
return await buildColdChainReport(rawData, tableColorRange, reportUserData);
47+
const pdf = await buildColdChainReport(rawData, tableColorRange, reportUserData);
48+
return {
49+
pdf,
50+
fileName: `${location.name}-${device.name}-${moment(start).format('YYYYMMDD').toString()}-${moment(end).format('YYYYMMDD').toString()}.pdf`
51+
}
52+
4853
} else if (device.report_endpoint.includes('co2-report')) {
49-
return await buildCO2Report(
54+
const pdf = await buildCO2Report(
5055
rawData,
5156
device.dev_eui,
5257
profile.employer,
@@ -55,6 +60,10 @@ export class PdfService {
5560
device.name,
5661
`${moment(start).format('YYYY/MM/DD').toString()} - ${moment(end).format('YYYY/MM/DD').toString()}`,
5762
);
63+
return {
64+
pdf,
65+
fileName: `${location.name}-${device.name}-${moment(start).format('YYYYMMDD').toString()}-${moment(end).format('YYYYMMDD').toString()}.pdf`
66+
}
5867
} else {
5968
throw new Error('Report endpoint not setup for this device');
6069
}
@@ -76,6 +85,8 @@ export class PdfService {
7685
findAllParams,
7786
user_id
7887
);
88+
7989
return reportData;
90+
8091
}
8192
}

0 commit comments

Comments
 (0)