@WebServlet(name = "Servlet")
public class PDFServlet extends HttpServlet {
private String RESOURCE = "images/logo-new.png";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
response.setContentType("application/pdf");
Document document = new Document(PageSize.LETTER);
PdfWriter writer = null;
try {
writer = PdfWriter.getInstance(document, response.getOutputStream());
document.open();
document.add(createHeaderTable());
document.add(comapnyNameTable());
document.add(createAddressTable());
document.add(getTable());
document.add(createFooterTable());
PdfContentByte canvas = writer.getDirectContent();
canvas.setLineWidth((float) 10 / 10);
canvas.moveTo(40, 765 - (5 * 10));
canvas.lineTo(570, 770 - (5 * 10));
canvas.stroke();
document.close();
} catch (DocumentException e) {
e.printStackTrace();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
private PdfPTable createHeaderTable(){
Font bold = new Font(Font.FontFamily.HELVETICA, 8f, Font.BOLD);
Font normal = new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL);
PdfPTable table = new PdfPTable(2);
try{
table.setWidthPercentage(100f);
table.setHorizontalAlignment(0);;
table.setSpacingBefore(10);
table.setWidths(new int[]{2, 6});
Image img = Image.getInstance(RESOURCE);
img.scalePercent(28f);
PdfPCell pdfPCell = new PdfPCell(img,true);
//pdfPCell.addElement(img);
pdfPCell.setBorder(Rectangle.NO_BORDER);
pdfPCell.setFixedHeight(50);
table.addCell(pdfPCell);
Paragraph paragraph = new Paragraph(10);
paragraph.add(new Chunk("Date:",bold));
paragraph.add(new Chunk("11-09-2015",normal));
paragraph.add(new Chunk("\nOrder Id:",bold));
paragraph.add(new Chunk("OrderId#12345",normal));
paragraph.setIndentationLeft(260);
table.addCell(newPdfPcel(paragraph,50));
}
catch (DocumentException e){
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
return table;
}
private PdfPTable createAddressTable(){
PdfPTable table = new PdfPTable(2);
Font bold = new Font(Font.FontFamily.HELVETICA, 8f, Font.BOLD);
Font normal = new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL);
try{
table.setWidthPercentage(100f);
table.setHorizontalAlignment(0);;
table.setSpacingBefore(10);
table.setSpacingAfter(10);
table.setWidths(new int[]{2, 6});
Paragraph paragraph = new Paragraph(10);
paragraph.add(new Chunk("Billed To",bold));
paragraph.add(new Chunk("\nOrdered By tdhdghfjghjg fvgfdghgfhg gfhggjdjh ghgfhgjhgjhg gfhghjghj dghgfh ghggfhgfh ghgffgh ghjggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", normal));
paragraph.setIndentationRight(40);
table.addCell(newPdfPcel(paragraph,60));
Paragraph paragraph2 = new Paragraph(10);
paragraph2.add(new Chunk("Service By ",bold));
paragraph2.add(new Chunk("\nService B gfdsgfdhghhhhy tdhdghfjghjg fvgfdghgfhg gfhggjdjh ghgfhgjhgjhg gfhghjghj dghgfhh ghfddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",normal));
paragraph2.setIndentationLeft(260);
table.addCell(newPdfPcel(paragraph2,60));
Paragraph paragraph3 = new Paragraph(10);
paragraph3.add(new Chunk("Payment Method:", bold));
paragraph3.add(new Chunk("\nVisa ending with *888888888888888888888888888888888888888888888**********123,\nseeenu@gmail.com",normal));
table.addCell(newPdfPcel(paragraph3,60));
Paragraph paragraph4 = new Paragraph(10);
paragraph4.add(new Chunk("Order Date:",bold));
paragraph4.add(new Chunk("\nMarch 7,2014",normal));
paragraph4.setIndentationLeft(260);
table.addCell(newPdfPcel(paragraph4,60));
}
catch (DocumentException e){
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
return table;
}
private PdfPTable comapnyNameTable(){
PdfPTable table = new PdfPTable(1);
Font bold = new Font(Font.FontFamily.HELVETICA, 8f, Font.BOLD);
Font normal = new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL);
try{
table.setWidthPercentage(100f);
Paragraph paragraph = new Paragraph(10);
paragraph.add(new Chunk("Travels Company",bold));
paragraph.add(new Chunk("\nOrdered By tdhdghfjghjg fvgfdghgfhg gfhggjdjh ghgfhgjhgjhg gfhghjghj dghgfh ghggfhgfh ghgffgh ghjggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg",normal));
paragraph.setIndentationLeft(200f);
paragraph.setIndentationRight(200f);
table.addCell(newPdfPcel(paragraph,60));
}
catch (Exception e){
e.printStackTrace();
}
return table;
}
private PdfPTable createFooterTable(){
PdfPTable table = new PdfPTable(2);
Font bold = new Font(Font.FontFamily.HELVETICA, 8f, Font.BOLD);
Font normal = new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL);
try{
table.setWidthPercentage(100f);
table.setHorizontalAlignment(0);;
table.setSpacingBefore(10);
table.setSpacingAfter(10);
table.setWidths(new int[]{2, 6});
Paragraph paragraph = new Paragraph(10);
paragraph.add(new Chunk("Tearms & Conditions",bold));
paragraph.add(new Chunk("\ngfdhgfhgfh",normal));
Paragraph paragraph2 = new Paragraph(10);
paragraph2.add(new Chunk("Aggrigate Service By",bold));
paragraph2.add(new Chunk("\nTruckWay",normal));
paragraph2.setIndentationLeft(260);
table.addCell(newPdfPcel(paragraph2,0));
}
catch (DocumentException e){
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
return table;
}
private Paragraph createParagraph(String text,Font font,float indentationLeft,float indentationRight){
Paragraph paragraph = new Paragraph(10);
paragraph.add(new Chunk(text,font));
paragraph.setIndentationLeft(indentationLeft);
paragraph.setIndentationRight(indentationRight);
return paragraph;
}
private PdfPCell newPdfPcel(Paragraph paragraph,float fixedHight){
PdfPCell pdfPCell = new PdfPCell();
pdfPCell.addElement(paragraph);
pdfPCell.setBorder(Rectangle.NO_BORDER);
pdfPCell.setFixedHeight(fixedHight);
return pdfPCell;
}
public PdfPTable getTable()throws DocumentException, IOException {
// Create a table with 7 columns
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100f);
// Add the first header row
PdfPCell cell = new PdfPCell(new Phrase("Order Details", FontFactory.getFont("Arial",10,Font.BOLD,BaseColor.BLACK)));
cell.setBackgroundColor(BaseColor.CYAN);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setColspan(7);
table.addCell(cell);
Font font = new Font();
font.setSize(10);
font.setFamily("Arial");
table.addCell(createCel("From Place",font,Element.ALIGN_LEFT));
table.addCell(createCel("Hyderabad",font,Element.ALIGN_LEFT));
table.addCell(createCel("To Place",font,Element.ALIGN_LEFT));
table.addCell(createCel("Bangolore",font,Element.ALIGN_LEFT));
table.addCell(createCel("Truck RequestDate",font,Element.ALIGN_LEFT));
table.addCell(createCel("09/9/15",font,Element.ALIGN_LEFT));
table.addCell(createCel("Truck Type",font,Element.ALIGN_LEFT));
table.addCell(createCel("6 wheeler",font,Element.ALIGN_LEFT));
table.addCell(createCel("No OfTrucks",font,Element.ALIGN_LEFT));
table.addCell(createCel("1",font,Element.ALIGN_LEFT));
table.addCell(createCel("Total Capacity",font,Element.ALIGN_LEFT));
table.addCell(createCel("8",font,Element.ALIGN_LEFT));
table.addCell(createCel("Total Amount",font,Element.ALIGN_LEFT));
table.addCell(createCel("1000.00",font,Element.ALIGN_LEFT));
table.addCell(createCel("From Addressy",font,Element.ALIGN_LEFT));
table.addCell(createCel("hyd",font,Element.ALIGN_LEFT));
table.addCell(createCel("To Address",font,Element.ALIGN_LEFT));
table.addCell(createCel("Bang",font,Element.ALIGN_LEFT));
table.addCell(createCel("Advance",font,Element.ALIGN_RIGHT));
table.addCell(createCel("1000.00",font,Element.ALIGN_RIGHT));
table.addCell(createCel("Service Charges",font,Element.ALIGN_RIGHT));
table.addCell(createCel("25.00",font,Element.ALIGN_RIGHT));
table.addCell(createCel("Total",font,Element.ALIGN_RIGHT));
table.addCell(createCel("1000.25",font,Element.ALIGN_RIGHT));
return table;
}
private PdfPCell createCel(String text,Font font,int alignment){
PdfPCell pdfPCel = new PdfPCell(new Phrase(text,font));
pdfPCel.setHorizontalAlignment(alignment);
return pdfPCel;
}
}