1
1
import { NextApiRequest , NextApiResponse } from "next" ;
2
- import { PDFDocument , rgb } from "pdf-lib" ;
2
+ import { PDFDocument } from "pdf-lib" ;
3
3
import multer from "multer" ;
4
4
import fs from "fs" ;
5
5
import path from "path" ;
6
6
import { promisify } from "util" ;
7
7
import { NextResponse } from "next/server" ;
8
- import { writeFile } from "fs/promises" ;
8
+ import { writeFile , unlink } from "fs/promises" ;
9
+ import { v4 as uuidv4 } from "uuid" ;
10
+ import { cookies } from "next/headers" ; // For session tracking
9
11
10
12
const upload = multer ( { dest : "uploads/" } ) ;
11
-
12
13
const uploadMiddleware = promisify ( upload . array ( "files" ) ) ;
13
14
15
+ const COOKIE_NAME = 'session_id' ; // Name of the session cookie
16
+
17
+ function getSessionId ( req : Request ) : string {
18
+ const sessionId = cookies ( ) . get ( COOKIE_NAME ) ?. value ;
19
+
20
+ if ( ! sessionId ) {
21
+ const newSessionId = uuidv4 ( ) ;
22
+ cookies ( ) . set ( COOKIE_NAME , newSessionId ) ;
23
+ return newSessionId ;
24
+ }
25
+
26
+ return sessionId ;
27
+ }
28
+
14
29
export async function POST ( req : Request , res : NextApiResponse ) {
15
30
await uploadMiddleware ( req as any , res as any ) ;
16
31
@@ -23,9 +38,9 @@ export async function POST(req: Request, res: NextApiResponse) {
23
38
24
39
try {
25
40
const pdfDoc = await PDFDocument . create ( ) ;
41
+ const sessionId = getSessionId ( req ) ;
26
42
27
43
for ( const file of files ) {
28
- console . log ( file ) ;
29
44
const fileBlob = new Blob ( [ file ] ) ;
30
45
const imgBytes = Buffer . from ( await fileBlob . arrayBuffer ( ) ) ;
31
46
let img ;
@@ -35,7 +50,7 @@ export async function POST(req: Request, res: NextApiResponse) {
35
50
} else if ( file . type === "image/jpeg" || file . type === "image/jpg" ) {
36
51
img = await pdfDoc . embedJpg ( imgBytes ) ;
37
52
} else {
38
- continue ; // Skip unsupported file types
53
+ continue ;
39
54
}
40
55
const page = pdfDoc . addPage ( [ img . width , img . height ] ) ;
41
56
page . drawImage ( img , {
@@ -47,27 +62,32 @@ export async function POST(req: Request, res: NextApiResponse) {
47
62
}
48
63
}
49
64
50
- const pdfBytes = await pdfDoc . save ( ) ;
51
- const pdfPath = path . join ( process . cwd ( ) , "public" , "merged.pdf" ) ;
52
- await writeFile ( pdfPath , pdfBytes ) ;
65
+ const mergedPdfBytes = await pdfDoc . save ( ) ;
66
+ const mergedPdfPath = path . join ( process . cwd ( ) , "public" , `merged_${ sessionId } .pdf` ) ;
67
+ await writeFile ( mergedPdfPath , mergedPdfBytes ) ;
68
+
69
+ const watermarkedPdfPath = path . join ( process . cwd ( ) , "public" , `watermarked_${ sessionId } .pdf` ) ;
70
+ await applyWatermark ( mergedPdfBytes , watermarkedPdfPath ) ;
53
71
54
- return NextResponse . json (
55
- { url : `/${ path . basename ( pdfPath ) } ` } ,
56
- { status : 200 } ,
57
- ) ;
72
+ await unlink ( mergedPdfPath ) ; // Optionally delete the merged file
73
+
74
+ return NextResponse . json ( { url : `/${ path . basename ( watermarkedPdfPath ) } ` } , { status : 200 } ) ;
58
75
} catch ( error ) {
59
- return NextResponse . json (
60
- { error : "Failed to process PDF" } ,
61
- { status : 500 } ,
62
- ) ;
76
+ return NextResponse . json ( { error : "Failed to process PDF" } , { status : 500 } ) ;
63
77
}
64
78
}
65
79
66
80
export async function DELETE ( req : Request , res : NextApiResponse ) {
67
- const filePath = path . resolve ( "./public/merged.pdf" ) ;
68
81
try {
82
+ const sessionId = cookies ( ) . get ( COOKIE_NAME ) ?. value ;
83
+ if ( ! sessionId ) {
84
+ return NextResponse . json ( { error : "Session not found" } , { status : 400 } ) ;
85
+ }
86
+
87
+ const filePath = path . resolve ( `./public/watermarked_${ sessionId } .pdf` ) ;
88
+
69
89
if ( fs . existsSync ( filePath ) ) {
70
- fs . unlinkSync ( filePath ) ;
90
+ await unlink ( filePath ) ; // Delete the watermarked PDF file
71
91
return NextResponse . json (
72
92
{ message : "Deleted watermarked PDF file successfully" } ,
73
93
{ status : 200 } ,
@@ -80,11 +100,34 @@ export async function DELETE(req: Request, res: NextApiResponse) {
80
100
}
81
101
} catch ( error ) {
82
102
console . error ( "Error deleting PDF file:" , error ) ;
83
- return NextResponse . json (
84
- {
85
- error : "Failed to delete watermarked PDF file" ,
86
- } ,
87
- { status : 500 } ,
88
- ) ;
103
+ return NextResponse . json ( {
104
+ error : "Failed to delete watermarked PDF file" ,
105
+ } , { status : 500 } ) ;
89
106
}
90
107
}
108
+
109
+ async function applyWatermark ( pdfBytes : Uint8Array , outputPath : string ) {
110
+ const pdfDoc = await PDFDocument . load ( pdfBytes ) ;
111
+
112
+ const watermarkImage = await pdfDoc . embedJpg ( fs . readFileSync ( path . resolve ( "./public/watermark.jpg" ) ) ) ;
113
+ const pages = pdfDoc . getPages ( ) ;
114
+ const { width, height } = pages [ 0 ] ! . getSize ( ) ;
115
+
116
+ pages . forEach ( ( page ) => {
117
+ const x = width - 80 ;
118
+ const y = 50 ;
119
+ const watermarkWidth = 50 ;
120
+ const watermarkHeight = 50 ;
121
+
122
+ page . drawImage ( watermarkImage , {
123
+ x : x ,
124
+ y : y ,
125
+ width : watermarkWidth ,
126
+ height : watermarkHeight ,
127
+ opacity : 0.3 ,
128
+ } ) ;
129
+ } ) ;
130
+
131
+ const watermarkedPdfBytes = await pdfDoc . save ( ) ;
132
+ await writeFile ( outputPath , watermarkedPdfBytes ) ;
133
+ }
0 commit comments