-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFReceiptGenerator.java
More file actions
66 lines (54 loc) · 2.54 KB
/
Copy pathPDFReceiptGenerator.java
File metadata and controls
66 lines (54 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.font.*;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import java.io.*;
public class PDFReceiptGenerator
{
public static void generateReceipt(int orderNumber, String customerName, String productDetails, double totalAmount) {
String fileName = "Receipt_" + orderNumber + ".pdf";
try (PDDocument document = new PDDocument()) {
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
try (PDPageContentStream contentStream = new PDPageContentStream(document, page))
{
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 18);
contentStream.beginText();
contentStream.setLeading(20f);
contentStream.newLineAtOffset(50, 750);
// Header
contentStream.showText(" Wee Ken Shin Bakers - Payment Receipt");
contentStream.newLine();
contentStream.newLine();
contentStream.setFont(PDType1Font.HELVETICA, 14);
// Order Details
contentStream.showText("Order Number: " + orderNumber);
contentStream.newLine();
contentStream.showText("Customer Name: " + customerName);
contentStream.newLine();
contentStream.showText("Products Ordered:");
contentStream.newLine();
// Product Details
String[] products = productDetails.split("\n");
for (String product : products) {
contentStream.showText("- " + product);
contentStream.newLine();
}
contentStream.newLine();
contentStream.showText("Total Amount: $" + totalAmount);
contentStream.newLine();
contentStream.showText("Payment Status: Paid");
contentStream.newLine();
// Footer
contentStream.newLine();
contentStream.showText("Thank you for choosing Wee Ken Shin Bakers!");
contentStream.newLine();
contentStream.endText();
}
// Save PDF
document.save(fileName);
System.out.println("Receipt generated successfully: " + fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
}