pdf generation

This commit is contained in:
Kallesh B S 2025-08-11 12:22:32 +05:30
parent 3646a80800
commit 6cb35273c2
21 changed files with 3260 additions and 1494 deletions

2
.gitignore vendored
View File

@ -3,6 +3,8 @@
/node_modules /node_modules
/build /build
# pdf
# Logs # Logs
logs logs
*.log *.log

View File

@ -3,6 +3,13 @@
"collection": "@nestjs/schematics", "collection": "@nestjs/schematics",
"sourceRoot": "src", "sourceRoot": "src",
"compilerOptions": { "compilerOptions": {
"deleteOutDir": true "deleteOutDir": true,
"assets": [
{
"include": "../public/**/**",
"outDir": "dist/public"
}
],
"watchAssets": true
} }
} }

3370
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -43,6 +43,9 @@
"oracledb": "^6.7.2", "oracledb": "^6.7.2",
"passport": "^0.7.0", "passport": "^0.7.0",
"passport-jwt": "^4.0.1", "passport-jwt": "^4.0.1",
"pdf-lib": "^1.17.1",
"pdf-merger-js": "^5.1.2",
"pdfkit": "^0.17.1",
"pg": "^8.13.3", "pg": "^8.13.3",
"reflect-metadata": "^0.2.2", "reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1", "rxjs": "^7.8.1",

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,4 +1,4 @@
import { Body, Controller, Delete, Get, Param, Patch, Post, Put, UseGuards } from '@nestjs/common'; import { Body, Controller, Delete, Get, Param, Patch, Post, Put, Res, StreamableFile, UseGuards } from '@nestjs/common';
import { CarnetApplicationService } from './carnet-application.service'; import { CarnetApplicationService } from './carnet-application.service';
import { ApiTags } from '@nestjs/swagger'; import { ApiTags } from '@nestjs/swagger';
@ -14,10 +14,14 @@ import {
import { Roles } from 'src/decorators/roles.decorator'; import { Roles } from 'src/decorators/roles.decorator';
import { JwtAuthGuard } from 'src/guards/jwt-auth.guard'; import { JwtAuthGuard } from 'src/guards/jwt-auth.guard';
import { RolesGuard } from 'src/guards/roles.guard'; import { RolesGuard } from 'src/guards/roles.guard';
import { Response } from 'express';
import { join } from 'path';
import { createReadStream } from 'fs';
import { deleteFilesAsync, generateFinalPDF, replaceSlashWithDash } from 'src/utils/helper';
@ApiTags('Carnet Application - Oracle') @ApiTags('Carnet Application - Oracle')
@UseGuards(JwtAuthGuard, RolesGuard) // @UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ca', 'sa') @Roles('ca', 'sa')
@Controller('oracle') @Controller('oracle')
export class CarnetApplicationController { export class CarnetApplicationController {
@ -152,8 +156,39 @@ export class CarnetApplicationController {
// [PRINT_PKG] // [PRINT_PKG]
@Post('PrintCarnet') @Post('PrintCarnet')
PrintCarnet(@Body() body: PrintCarnetDTO) { @Roles('ca', 'sa', 'ua')
return this.carnetApplicationService.PrintCarnet(body); async PrintCarnet(@Body() body: PrintCarnetDTO, @Res({ passthrough: true }) res: Response) {
try {
const { finalArray, carnetData }: any = await this.carnetApplicationService.PrintCarnet(body);
const filterCarnetData = finalArray.filter(item => item !== '2GeneralList.pdf')
let filesArray = [`${replaceSlashWithDash(carnetData.carnetNo)}p1.pdf`];
// await generateFinalPDF([...filesArray, '2GeneralList.pdf', ...carnetData], 'carnet.pdf');
await generateFinalPDF([...filesArray, '2GeneralList.pdf', ...finalArray], `${replaceSlashWithDash(carnetData.carnetNo)}carnet.pdf`);
const fileName = `${replaceSlashWithDash(carnetData.carnetNo)}carnet.pdf`;
res.set({
'Content-Type': 'application/pdf',
'Content-Disposition': `attachment; filename="${fileName}"`,
});
const filePath = join(process.cwd(), 'dist/public/carnet-pdf', fileName);
// console.log(filePath);
const stream = createReadStream(filePath);
// Clean up files after response finishes streaming
res.on('finish', async () => {
await deleteFilesAsync([...filesArray, ...filterCarnetData]);
});
return new StreamableFile(stream);
} catch (error) {
return error.message;
}
} }
} }

View File

@ -2,7 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';
import { OracleDBService } from 'src/db/db.service'; import { OracleDBService } from 'src/db/db.service';
import * as oracledb from 'oracledb' import * as oracledb from 'oracledb'
import { InternalServerException } from 'src/exceptions/internalServerError.exception'; import { InternalServerException } from 'src/exceptions/internalServerError.exception';
import { closeOracleDbConnection, fetchCursor, handleError, setEmptyStringsToNull } from 'src/utils/helper'; import { closeOracleDbConnection, fetchCursor, fnCounterFoil, fnVochers, generateGreenCoverPDF, handleError, pdfCarnetData, pdfGLData, setEmptyStringsToNull, tnVochers, tsCounterFoil, usCounterFoil } from 'src/utils/helper';
import { import {
CarnetProcessingCenterDTO, SaveCarnetApplicationDTO, TransmitApplicationtoProcessDTO, CarnetProcessingCenterDTO, SaveCarnetApplicationDTO, TransmitApplicationtoProcessDTO,
@ -1114,7 +1114,8 @@ export class CarnetApplicationService {
} }
// [PRINT_PKG] // [PRINT_PKG]
async PrintCarnet(body: PrintCarnetDTO) {
async getPrintingData(body: PrintCarnetDTO) {
let connection; let connection;
try { try {
connection = await this.oracleDBService.getConnection(); connection = await this.oracleDBService.getConnection();
@ -1147,12 +1148,17 @@ export class CarnetApplicationService {
const fres: any = await fetchCursor(outBinds.P_CARNETDATACURSOR, CarnetApplicationService.name); const fres: any = await fetchCursor(outBinds.P_CARNETDATACURSOR, CarnetApplicationService.name);
const fres1: any = await fetchCursor(outBinds.P_GLDATACURSOR, CarnetApplicationService.name); const fres1: any = await fetchCursor(outBinds.P_GLDATACURSOR, CarnetApplicationService.name);
if (fres.length === 0 || fres1.length === 0) {
throw new BadRequestException("Print failed please check the submitted details");
}
if (fres.length > 0 && fres[0].ERRORMESG) { if (fres.length > 0 && fres[0].ERRORMESG) {
this.logger.warn(fres[0].ERRORMESG); this.logger.warn(fres[0].ERRORMESG);
throw new BadRequestException(fres[0].ERRORMESG) throw new BadRequestException(fres[0].ERRORMESG)
} }
return { P_CARNETDATACURSOR: fres, P_GLDATACURSOR: fres1 }; return { fres: fres, fres1: fres1 }
} catch (error) { } catch (error) {
handleError(error, CarnetApplicationService.name) handleError(error, CarnetApplicationService.name)
} finally { } finally {
@ -1160,4 +1166,75 @@ export class CarnetApplicationService {
} }
} }
async PrintCarnet(body: PrintCarnetDTO) {
try {
const { fres, fres1 }: any = await this.getPrintingData(body);
if (fres.length === 0 || fres1.length === 0) {
throw new BadRequestException("Print failed please check the submitted details");
}
if (fres.length > 0 && fres[0].ERRORMESG) {
this.logger.warn(fres[0].ERRORMESG);
throw new BadRequestException(fres[0].ERRORMESG)
}
try {
const glData: pdfGLData[] = fres1.map(x => {
return {
itemNo: x.ITEMNO,
description: x.ITEMDESCRIPTION,
noOfPieces: x.NOOFPIECES,
weight: x.WEIGHT,
itemValue: x.ITEMVALUE,
countryOfOrigin: x.GOODSORIGINCOUNTRY
};
});
const carnetData: pdfCarnetData = {
carnetNo: fres[0].CARNETNO,
ISSUEDBY: fres[0].ISSUEDBY,
ISSUEDATE: fres[0].ISSUEDATE,
EXPDATE: fres[0].EXPDATE,
AUTHREP: fres[0].AUTHREP,
NOOFUSSETS: fres[0].NOOFUSSETS,
NOOFFOREIGNSETS: fres[0].NOOFFOREIGNSETS,
NOOFTRANSITSETS: fres[0].NOOFTRANSITSETS,
PRINTGOODSTOBEEXPORTED: fres[0].PRINTGOODSTOBEEXPORTED,
HOLDERDATATOPRINT: fres[0].HOLDERDATATOPRINT,
CONTINUATIONSHEETS: fres[0].CONTINUATIONSHEETS,
NEXTUSCFNO: fres[0].NEXTUSCFNO,
NEXTFOREIGNCFNO: fres[0].NEXTFOREIGNCFNO,
NEXTTRANSITCFNO: fres[0].NEXTTRANSITCFNO,
contPageNo: 0
}
const p1Status = await generateGreenCoverPDF(carnetData);
const p3Status = await usCounterFoil(carnetData);
const p4Status = await fnCounterFoil(carnetData);
const p5Status = await tsCounterFoil(carnetData);
const p6Status = await fnVochers(carnetData);
const p7Status = await tnVochers(carnetData);
const finalArray = [
...p3Status,
...p4Status,
...p5Status,
...p6Status,
...p7Status
]
return { finalArray, carnetData }
} catch (error) {
throw new InternalServerException(error.message)
}
} catch (error) {
handleError(error, CarnetApplicationService.name)
}
}
} }

File diff suppressed because it is too large Load Diff