pdf generation

This commit is contained in:
Kallesh B S 2025-08-12 15:27:32 +05:30
parent 3646a80800
commit f3cbadebd7
23 changed files with 3698 additions and 1496 deletions

2
.gitignore vendored
View File

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

View File

@ -3,6 +3,13 @@
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"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",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"pdf-lib": "^1.17.1",
"pdf-merger-js": "^5.1.2",
"pdfkit": "^0.17.1",
"pg": "^8.13.3",
"reflect-metadata": "^0.2.2",
"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.

Binary file not shown.

View File

@ -55,7 +55,8 @@ export class TransmitApplicationtoProcessDTO extends IntersectionType(
export class CarnetProcessingCenterDTO extends (IntersectionType(USERID_DTO, HEADERID_DTO)) { }
export class PrintCarnetDTO extends (IntersectionType(SPID_DTO, HEADERID_DTO, PRINTGL_DTO)){}
export class PrintCarnetDTO extends (IntersectionType(SPID_DTO, HEADERID_DTO, PRINTGL_DTO)) { }
export class PrintGLDTO extends (IntersectionType(SPID_DTO, HEADERID_DTO)) { }
export class CreateApplicationDTO extends IntersectionType(
SPID_DTO, CLIENTID_DTO, LOCATIONID_DTO, USERID_DTO, APPLICATIONNAME_DTO,

View File

@ -1,4 +1,4 @@
import { Body, Controller, Delete, Get, Param, Patch, Post, Put, UseGuards } from '@nestjs/common';
import { BadRequestException, Body, Controller, Delete, Get, Param, Patch, Post, Put, Res, StreamableFile, UseGuards } from '@nestjs/common';
import { CarnetApplicationService } from './carnet-application.service';
import { ApiTags } from '@nestjs/swagger';
@ -7,13 +7,19 @@ import {
AddCountriesDTO,
AddGenerallistItemsDTO,
CA_UpdateHolderDTO,
CarnetProcessingCenterDTO, CreateApplicationDTO, DeleteGenerallistItemsDTO, GetCarnetControlCenterDTO, PrintCarnetDTO, SaveCarnetApplicationDTO, TransmitApplicationtoProcessDTO,
CarnetProcessingCenterDTO, CreateApplicationDTO, DeleteGenerallistItemsDTO, GetCarnetControlCenterDTO, PrintCarnetDTO, PrintGLDTO, SaveCarnetApplicationDTO, TransmitApplicationtoProcessDTO,
UpdateExpGoodsAuthRepDTO,
UpdateShippingDetailsDTO
} from 'src/dto/property.dto';
import { Roles } from 'src/decorators/roles.decorator';
import { JwtAuthGuard } from 'src/guards/jwt-auth.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';
import { BadRequestException as BR } from 'src/exceptions/badRequest.exception';
import { InternalServerException } from 'src/exceptions/internalServerError.exception';
@ApiTags('Carnet Application - Oracle')
@ -152,8 +158,86 @@ export class CarnetApplicationController {
// [PRINT_PKG]
@Post('PrintCarnet')
PrintCarnet(@Body() body: PrintCarnetDTO) {
return this.carnetApplicationService.PrintCarnet(body);
@Roles('ca', 'sa', 'ua')
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`, `${replaceSlashWithDash(carnetData.carnetNo)}p2.pdf`];
// await generateFinalPDF([...filesArray, '2GeneralList.pdf', ...carnetData], 'carnet.pdf');
await generateFinalPDF([...filesArray, ...finalArray, '13LastSheetP3.pdf', '13LastSheetP4.pdf'], `${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); //server
// const filePath = join(process.cwd(), 'public/carnet-pdf', fileName); //local
console.log(filePath);
// console.log(filePath);
const stream = createReadStream(filePath);
// Clean up files after response finishes streaming
res.on('finish', async () => {
await deleteFilesAsync([...filesArray, ...finalArray.filter(x => x !== `${replaceSlashWithDash(carnetData.carnetNo)}p2.pdf`), fileName]);
});
return new StreamableFile(stream);
} catch (error) {
if (error instanceof BadRequestException || error instanceof BR) {
throw new BadRequestException("Download failed please check the details")
}
else {
throw new InternalServerException("Error while downloading");
}
}
}
@Post('PrintGL')
@Roles('ca', 'sa', 'ua')
async PrintGL(@Body() body: PrintGLDTO, @Res({ passthrough: true }) res: Response) {
try {
const { finalArray, carnetData }: any = await this.carnetApplicationService.PrintGL(body);
let filesArray = [`${replaceSlashWithDash(carnetData.carnetNo)}p2.pdf`];
// await generateFinalPDF([...filesArray, '2GeneralList.pdf', ...carnetData], 'carnet.pdf');
await generateFinalPDF([...filesArray, ...finalArray], `${replaceSlashWithDash(carnetData.carnetNo)}-GL.pdf`);
const fileName = `${replaceSlashWithDash(carnetData.carnetNo)}-GL.pdf`;
res.set({
'Content-Type': 'application/pdf',
'Content-Disposition': `attachment; filename="${fileName}"`,
});
const filePath = join(process.cwd(), 'dist/public/carnet-pdf', fileName); //server
// const filePath = join(process.cwd(), 'public/carnet-pdf', fileName); //local
// console.log(filePath);
// console.log(filePath);
const stream = createReadStream(filePath);
// Clean up files after response finishes streaming
res.on('finish', async () => {
await deleteFilesAsync([...filesArray, ...finalArray, fileName]);
});
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 * as oracledb from 'oracledb'
import { InternalServerException } from 'src/exceptions/internalServerError.exception';
import { closeOracleDbConnection, fetchCursor, handleError, setEmptyStringsToNull } from 'src/utils/helper';
import { closeOracleDbConnection, deleteFilesAsync, fetchCursor, fnCounterFoil, fnVochers, generateFinalPDF, generateGL, generateGL1, generateGreenCoverPDF, handleError, pdfCarnetData, pdfGLData, replaceSlashWithDash, setEmptyStringsToNull, tnVochers, tsCounterFoil, usCounterFoil } from 'src/utils/helper';
import {
CarnetProcessingCenterDTO, SaveCarnetApplicationDTO, TransmitApplicationtoProcessDTO,
@ -14,7 +14,9 @@ import {
CA_UpdateHolderDTO,
GetCarnetControlCenterDTO,
DeleteGenerallistItemsDTO,
PrintCarnetDTO
PrintCarnetDTO,
PrintGLDTO,
YON
} from 'src/dto/property.dto';
import { OracleService } from '../oracle.service';
import { BadRequestException } from 'src/exceptions/badRequest.exception';
@ -1114,7 +1116,8 @@ export class CarnetApplicationService {
}
// [PRINT_PKG]
async PrintCarnet(body: PrintCarnetDTO) {
async getPrintingData(body: PrintCarnetDTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
@ -1147,12 +1150,17 @@ export class CarnetApplicationService {
const fres: any = await fetchCursor(outBinds.P_CARNETDATACURSOR, 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) {
this.logger.warn(fres[0].ERRORMESG);
throw new BadRequestException(fres[0].ERRORMESG)
}
return { P_CARNETDATACURSOR: fres, P_GLDATACURSOR: fres1 };
return { fres: fres, fres1: fres1 }
} catch (error) {
handleError(error, CarnetApplicationService.name)
} finally {
@ -1160,4 +1168,186 @@ 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);
let p2Status: any = "";
if (body.P_PRINTGL === 'Y') {
p2Status = await generateGL(glData, carnetData);
// console.log("-------- p2 --------", p2Status);
// console.log(typeof p2Status.batch, p2Status.batch);
if (p2Status.batch % 2 === 0) {
await generateFinalPDF([...p2Status.fileArray, '2GeneralListVOID.pdf'], `${replaceSlashWithDash(carnetData.carnetNo)}p2.pdf`)
await deleteFilesAsync([...p2Status.fileArray])
}
else {
await generateFinalPDF([...p2Status.fileArray], `${replaceSlashWithDash(carnetData.carnetNo)}p2.pdf`)
await deleteFilesAsync([...p2Status.fileArray])
}
} else {
p2Status = await generateGL1(carnetData);
if (p2Status === 'p2 done') {
await generateFinalPDF([`${replaceSlashWithDash(carnetData.carnetNo)}p2.pdf`, '2GeneralListVOID.pdf'], `${replaceSlashWithDash(carnetData.carnetNo)}p2.pdf`)
}
}
const p3Status = await usCounterFoil(carnetData);
const p4Status = await fnCounterFoil(carnetData);
const p5Status = await tsCounterFoil(carnetData);
const p6Status = await fnVochers(carnetData, body);
const p7Status = await tnVochers(carnetData, body);
const finalArray = [
...p3Status,
...p4Status,
...p5Status,
...p6Status,
...p7Status
]
return { finalArray, carnetData }
} catch (error) {
throw new InternalServerException(error.message)
}
} catch (error) {
handleError(error, CarnetApplicationService.name)
}
}
async PrintGL(body: PrintGLDTO) {
let newBody: PrintCarnetDTO = { ...body, P_PRINTGL: YON.YES }
try {
const { fres, fres1 }: any = await this.getPrintingData(newBody);
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);
let p2Status: any = "";
p2Status = await generateGL(glData, carnetData);
// console.log("-------- p2 --------", p2Status);
// console.log(typeof p2Status.batch, p2Status.batch);
if (p2Status.batch % 2 === 0) {
await generateFinalPDF([...p2Status.fileArray, '2GeneralListVOID.pdf'], `${replaceSlashWithDash(carnetData.carnetNo)}p2.pdf`)
await deleteFilesAsync([...p2Status.fileArray])
}
else {
await generateFinalPDF([...p2Status.fileArray], `${replaceSlashWithDash(carnetData.carnetNo)}p2.pdf`)
await deleteFilesAsync([...p2Status.fileArray])
}
// 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