| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.common.util;
|
1 |
package com.spice.profitmandi.common.util;
|
| 2 |
|
2 |
|
| - |
|
3 |
import java.io.ByteArrayInputStream;
|
| - |
|
4 |
import java.io.ByteArrayOutputStream;
|
| - |
|
5 |
import java.io.IOException;
|
| 3 |
import java.io.OutputStream;
|
6 |
import java.io.OutputStream;
|
| - |
|
7 |
import java.net.URL;
|
| 4 |
import java.util.List;
|
8 |
import java.util.List;
|
| 5 |
import java.util.Locale;
|
9 |
import java.util.Locale;
|
| 6 |
import java.util.Set;
|
10 |
import java.util.Set;
|
| 7 |
|
11 |
|
| 8 |
import org.apache.logging.log4j.Logger;
|
- |
|
| 9 |
import org.apache.logging.log4j.LogManager;
|
12 |
import org.apache.logging.log4j.LogManager;
|
| - |
|
13 |
import org.apache.logging.log4j.Logger;
|
| 10 |
import org.springframework.util.StringUtils;
|
14 |
import org.springframework.util.StringUtils;
|
| 11 |
|
15 |
|
| 12 |
import com.ibm.icu.text.RuleBasedNumberFormat;
|
16 |
import com.ibm.icu.text.RuleBasedNumberFormat;
|
| 13 |
import com.itextpdf.text.Chunk;
|
17 |
import com.itextpdf.text.Chunk;
|
| 14 |
import com.itextpdf.text.Document;
|
18 |
import com.itextpdf.text.Document;
|
| 15 |
import com.itextpdf.text.DocumentException;
|
19 |
import com.itextpdf.text.DocumentException;
|
| 16 |
import com.itextpdf.text.Element;
|
20 |
import com.itextpdf.text.Element;
|
| 17 |
import com.itextpdf.text.Font;
|
21 |
import com.itextpdf.text.Font;
|
| - |
|
22 |
import com.itextpdf.text.Font.FontFamily;
|
| - |
|
23 |
import com.itextpdf.text.Image;
|
| 18 |
import com.itextpdf.text.Paragraph;
|
24 |
import com.itextpdf.text.Paragraph;
|
| - |
|
25 |
import com.itextpdf.text.Phrase;
|
| 19 |
import com.itextpdf.text.Rectangle;
|
26 |
import com.itextpdf.text.Rectangle;
|
| - |
|
27 |
import com.itextpdf.text.pdf.ColumnText;
|
| - |
|
28 |
import com.itextpdf.text.pdf.PdfContentByte;
|
| - |
|
29 |
import com.itextpdf.text.pdf.PdfDocument;
|
| - |
|
30 |
import com.itextpdf.text.pdf.PdfGState;
|
| 20 |
import com.itextpdf.text.pdf.PdfPCell;
|
31 |
import com.itextpdf.text.pdf.PdfPCell;
|
| 21 |
import com.itextpdf.text.pdf.PdfPTable;
|
32 |
import com.itextpdf.text.pdf.PdfPTable;
|
| - |
|
33 |
import com.itextpdf.text.pdf.PdfReader;
|
| - |
|
34 |
import com.itextpdf.text.pdf.PdfStamper;
|
| 22 |
import com.itextpdf.text.pdf.PdfWriter;
|
35 |
import com.itextpdf.text.pdf.PdfWriter;
|
| 23 |
import com.spice.profitmandi.common.model.CreditNotePdfModel;
|
36 |
import com.spice.profitmandi.common.model.CreditNotePdfModel;
|
| 24 |
import com.spice.profitmandi.common.model.CustomCustomer;
|
37 |
import com.spice.profitmandi.common.model.CustomCustomer;
|
| 25 |
import com.spice.profitmandi.common.model.CustomInsurancePolicy;
|
38 |
import com.spice.profitmandi.common.model.CustomInsurancePolicy;
|
| 26 |
import com.spice.profitmandi.common.model.CustomOrderItem;
|
39 |
import com.spice.profitmandi.common.model.CustomOrderItem;
|
| Line 52... |
Line 65... |
| 52 |
private static float[] stateWidthsCrNote = new float[] { 2.1f, 0.7f, .3f, 0.6f, .7f, .5f, .6f, .5f, .6f, .8f };
|
65 |
private static float[] stateWidthsCrNote = new float[] { 2.1f, 0.7f, .3f, 0.6f, .7f, .5f, .6f, .5f, .6f, .8f };
|
| 53 |
|
66 |
|
| 54 |
private static final Locale indianLocale = Locale.getDefault();
|
67 |
private static final Locale indianLocale = Locale.getDefault();
|
| 55 |
|
68 |
|
| 56 |
private static final Logger LOGGER = LogManager.getLogger(PdfUtils.class);
|
69 |
private static final Logger LOGGER = LogManager.getLogger(PdfUtils.class);
|
| - |
|
70 |
|
| - |
|
71 |
private static URL cancelledImgUrl = null;
|
| - |
|
72 |
static {
|
| - |
|
73 |
cancelledImgUrl = PdfUtils.class.getResource("cancelled.png");
|
| - |
|
74 |
}
|
| 57 |
|
75 |
|
| 58 |
public static void generateAndWrite(PdfModel pdfModel, OutputStream outputStream) {
|
76 |
public static void generateAndWrite(PdfModel pdfModel, OutputStream outputStream) {
|
| 59 |
Document document = new Document();
|
77 |
Document document = new Document();
|
| 60 |
document.setMargins(0, 0, 25, 0);
|
78 |
document.setMargins(0, 0, 25, 0);
|
| 61 |
try {
|
79 |
try {
|
| Line 69... |
Line 87... |
| 69 |
} else {
|
87 |
} else {
|
| 70 |
customerAddressStateCode = Utils.getStateCode(customer.getAddress().getState());
|
88 |
customerAddressStateCode = Utils.getStateCode(customer.getAddress().getState());
|
| 71 |
retailerAddressStateCode = Utils.getStateCode(retailer.getAddress().getState());
|
89 |
retailerAddressStateCode = Utils.getStateCode(retailer.getAddress().getState());
|
| 72 |
}
|
90 |
}
|
| 73 |
Set<CustomOrderItem> orderItems = pdfModel.getOrderItems();
|
91 |
Set<CustomOrderItem> orderItems = pdfModel.getOrderItems();
|
| - |
|
92 |
ByteArrayOutputStream oos = null;
|
| - |
|
93 |
if(pdfModel.isCancelled()) {
|
| - |
|
94 |
oos = new ByteArrayOutputStream();
|
| - |
|
95 |
PdfWriter.getInstance(document, oos);
|
| 74 |
|
96 |
} else {
|
| 75 |
PdfWriter.getInstance(document, outputStream);
|
97 |
PdfWriter.getInstance(document, outputStream);
|
| - |
|
98 |
}
|
| 76 |
|
99 |
|
| 77 |
document.open();
|
100 |
document.open();
|
| 78 |
document.addTitle(pdfModel.getTitle());
|
101 |
document.addTitle(pdfModel.getTitle());
|
| 79 |
document.addAuthor(pdfModel.getAuther());
|
102 |
document.addAuthor(pdfModel.getAuther());
|
| 80 |
|
103 |
|
| Line 317... |
Line 340... |
| 317 |
|
340 |
|
| 318 |
Paragraph warningParagraph = new Paragraph(sb.toString(), FONT_NORMAL);
|
341 |
Paragraph warningParagraph = new Paragraph(sb.toString(), FONT_NORMAL);
|
| 319 |
warningParagraph.setIndentationLeft(40);
|
342 |
warningParagraph.setIndentationLeft(40);
|
| 320 |
document.add(Chunk.NEWLINE);
|
343 |
document.add(Chunk.NEWLINE);
|
| 321 |
document.add(warningParagraph);
|
344 |
document.add(warningParagraph);
|
| 322 |
|
- |
|
| 323 |
document.close(); // no need to close PDFwriter?
|
345 |
document.close(); // no need to close PDFwriter?
|
| - |
|
346 |
if(pdfModel.isCancelled()) {
|
| - |
|
347 |
stampCancelled(oos, outputStream);
|
| - |
|
348 |
}
|
| 324 |
|
349 |
|
| 325 |
} catch (DocumentException e) {
|
350 |
} catch (DocumentException e) {
|
| 326 |
LOGGER.error("Unable to write data to pdf file : ", e);
|
351 |
LOGGER.error("Unable to write data to pdf file : ", e);
|
| 327 |
} catch (Exception e) {
|
352 |
} catch (Exception e) {
|
| 328 |
// TODO Auto-generated catch block
|
353 |
// TODO Auto-generated catch block
|
| 329 |
e.printStackTrace();
|
354 |
e.printStackTrace();
|
| 330 |
}
|
355 |
}
|
| - |
|
356 |
|
| - |
|
357 |
}
|
| - |
|
358 |
|
| - |
|
359 |
private static void stampCancelled(ByteArrayOutputStream byteStream, OutputStream os) throws IOException, DocumentException {
|
| - |
|
360 |
ByteArrayInputStream bais = new ByteArrayInputStream(byteStream.toByteArray());
|
| - |
|
361 |
PdfReader pdfReader = new PdfReader(bais);
|
| - |
|
362 |
int n = pdfReader.getNumberOfPages();
|
| - |
|
363 |
PdfStamper pdfStamper = new PdfStamper(pdfReader, os);
|
| - |
|
364 |
pdfStamper.setRotateContents(false);
|
| - |
|
365 |
// text watermark
|
| - |
|
366 |
Font f = new Font(FontFamily.HELVETICA, 30);
|
| - |
|
367 |
Phrase p = new Phrase("My watermark (text)", f);
|
| - |
|
368 |
//img watermark
|
| - |
|
369 |
Image img = Image.getInstance(cancelledImgUrl);
|
| - |
|
370 |
float w = img.getScaledWidth();
|
| - |
|
371 |
float h = img.getScaledHeight();
|
| - |
|
372 |
// transparency
|
| - |
|
373 |
PdfGState gs1 = new PdfGState();
|
| - |
|
374 |
gs1.setFillOpacity(0.5f);
|
| - |
|
375 |
//properties
|
| - |
|
376 |
PdfContentByte over;
|
| - |
|
377 |
Rectangle pagesize;
|
| - |
|
378 |
float x, y;
|
| - |
|
379 |
|
| - |
|
380 |
// loop over every page
|
| - |
|
381 |
for (int i = 1; i <= n; i++) {
|
| - |
|
382 |
pagesize = pdfReader.getPageSize(i);
|
| - |
|
383 |
x = (pagesize.getLeft() + pagesize.getRight()) / 2;
|
| - |
|
384 |
y = (pagesize.getTop() + pagesize.getBottom()) / 2;
|
| - |
|
385 |
over = pdfStamper.getOverContent(i);
|
| - |
|
386 |
over.saveState();
|
| - |
|
387 |
over.setGState(gs1);
|
| - |
|
388 |
if (i % 2 == 1)
|
| - |
|
389 |
ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, x, y, 0);
|
| - |
|
390 |
else
|
| - |
|
391 |
over.addImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2));
|
| - |
|
392 |
over.restoreState();
|
| - |
|
393 |
}
|
| - |
|
394 |
pdfStamper.close();
|
| - |
|
395 |
pdfReader.close();
|
| - |
|
396 |
|
| 331 |
}
|
397 |
}
|
| 332 |
|
398 |
|
| 333 |
public static void generateAndWriteDebitNote(List<DebitNotePdfModel> debitNotePdfModels,
|
399 |
public static void generateAndWriteDebitNote(List<DebitNotePdfModel> debitNotePdfModels,
|
| 334 |
OutputStream outputStream) {
|
400 |
OutputStream outputStream) {
|
| 335 |
Document document = new Document();
|
401 |
Document document = new Document();
|
| Line 565... |
Line 631... |
| 565 |
amountInWords.append(StringUtils.capitalize(amountInWordsFormat.format((int) (amount * 100) % 100)));
|
631 |
amountInWords.append(StringUtils.capitalize(amountInWordsFormat.format((int) (amount * 100) % 100)));
|
| 566 |
amountInWords.append(" paise");
|
632 |
amountInWords.append(" paise");
|
| 567 |
return amountInWords.toString();
|
633 |
return amountInWords.toString();
|
| 568 |
}
|
634 |
}
|
| 569 |
|
635 |
|
| 570 |
public static void generateAndWriteCustomerCreditNotes(List<CreditNotePdfModel> creditNotes, OutputStream outputStream) {
|
636 |
public static void generateAndWriteCustomerCreditNotes(List<CreditNotePdfModel> creditNotes,
|
| - |
|
637 |
OutputStream outputStream) {
|
| 571 |
Document document = new Document();
|
638 |
Document document = new Document();
|
| 572 |
document.setMargins(0, 0, 25, 0);
|
639 |
document.setMargins(0, 0, 25, 0);
|
| 573 |
try {
|
640 |
try {
|
| 574 |
PdfWriter.getInstance(document, outputStream);
|
641 |
PdfWriter.getInstance(document, outputStream);
|
| 575 |
|
642 |
|
| 576 |
document.open();
|
643 |
document.open();
|
| 577 |
document.addTitle(creditNotes.get(0).getPdfModel().getTitle());
|
644 |
document.addTitle(creditNotes.get(0).getPdfModel().getTitle());
|
| 578 |
document.addAuthor(creditNotes.get(0).getPdfModel().getAuther());
|
645 |
document.addAuthor(creditNotes.get(0).getPdfModel().getAuther());
|
| 579 |
for (CreditNotePdfModel creditNotePdfModel : creditNotes) {
|
646 |
for (CreditNotePdfModel creditNotePdfModel : creditNotes) {
|
| 580 |
PdfModel pdfModel = creditNotePdfModel.getPdfModel();
|
647 |
PdfModel pdfModel = creditNotePdfModel.getPdfModel();
|
| Line 590... |
Line 657... |
| 590 |
customerAddressStateCode = Utils.getStateCode(customer.getAddress().getState());
|
657 |
customerAddressStateCode = Utils.getStateCode(customer.getAddress().getState());
|
| 591 |
retailerAddressStateCode = Utils.getStateCode(retailer.getAddress().getState());
|
658 |
retailerAddressStateCode = Utils.getStateCode(retailer.getAddress().getState());
|
| 592 |
}
|
659 |
}
|
| 593 |
Set<CustomOrderItem> orderItems = pdfModel.getOrderItems();
|
660 |
Set<CustomOrderItem> orderItems = pdfModel.getOrderItems();
|
| 594 |
|
661 |
|
| 595 |
|
- |
|
| 596 |
Paragraph paragraphTitle = new Paragraph(pdfModel.getTitle(), FONT_TITLE);
|
662 |
Paragraph paragraphTitle = new Paragraph(pdfModel.getTitle(), FONT_TITLE);
|
| 597 |
paragraphTitle.setAlignment(Element.ALIGN_CENTER);
|
663 |
paragraphTitle.setAlignment(Element.ALIGN_CENTER);
|
| 598 |
|
664 |
|
| 599 |
PdfPCell blankCell = new PdfPCell();
|
665 |
PdfPCell blankCell = new PdfPCell();
|
| 600 |
blankCell.setBorder(Rectangle.NO_BORDER);
|
666 |
blankCell.setBorder(Rectangle.NO_BORDER);
|