source view

Upload: varun-agarwal

Post on 10-Jan-2016

6 views

Category:

Documents


0 download

DESCRIPTION

This is the source code of a java invoice generator. You can use a word template to generate the type of invoice you like. This is a simple swing program that provides the basic functionality

TRANSCRIPT

  • This PDF was generated dynamically using DynamicPDF; Generator for Java. www.dynamicpdf.com

    Invoice.java1 //Although DynamicPDF for Java supports J2SE 1.4.X for most features, this example is compatible only with J2SE 5.0 and above.2 import com.cete.dynamicpdf.*;3 import com.cete.dynamicpdf.pageelements.*;4 import com.cete.dynamicpdf.pageelements.barcoding.Interleaved25;5 import java.io.FileNotFoundException;6 import java.io.IOException;7 import java.math.BigDecimal;8 import java.sql.*;9 import java.text.DecimalFormat;

    10 import java.text.SimpleDateFormat;11 import java.util.Enumeration;12 import java.util.Locale;13 import java.util.Vector;14 import javax.servlet.ServletConfig;15 import javax.servlet.ServletException;16 import javax.servlet.ServletOutputStream;17 import javax.servlet.http.HttpServlet;18 import javax.servlet.http.HttpServletRequest;19 import javax.servlet.http.HttpServletResponse;2021 public class Invoice extends HttpServlet {22 23 ServletOutputStream sOut;24 Connection connection;25 26 public void init(ServletConfig servletConfig) throws ServletException {27 super.init(servletConfig);28 }29 public void doGet(HttpServletRequest req, HttpServletResponse res)30 throws IOException,ServletException {31 CeTeConnection ceTe = (CeTeConnection)getServletContext().getAttribute("cetecon");32 connection = ceTe.getConnection();33 sOut = res.getOutputStream();34 // Create a document and set it's properties35 Document objDocument = new Document();36 objDocument.setCreator("Invoice.html");37 objDocument.setAuthor("ceTe Software");38 objDocument.setTitle("Invoice");39 40 41 42 String[] invoiceNo = req.getParameterValues("invoiceno");43 if (invoiceNo != null) {44 // Add Invoices to the document45 for (int i = 0; i < invoiceNo.length; i++) {46 MyInvoice objInvoice = new MyInvoice();47 //Add the template to the document48 objDocument.setTemplate(objInvoice.getTemplate());49 objInvoice.draw(connection, objDocument,

  • This PDF was generated dynamically using DynamicPDF; Generator for Java. www.dynamicpdf.com

    Invoice.java50 Integer.parseInt(invoiceNo[i]));51 }52 }53 54 55 // Outputs the Invoices to the current web page56 objDocument.drawToWeb(req, res, sOut, "Invoice.pdf");57 ceTe.close();58 sOut.close();59 60 }61 62 private class MyInvoice {63 64 private BigDecimal subTotal = new BigDecimal(0.0);65 private BigDecimal freight = new BigDecimal(0.0);66 private float yOffset = 0;67 Enumeration e1 = null;68 private Template template = new Template();69 private boolean pageTemplateImagesSet = false;70 private RgbColor objBGColor = new WebColor("#E0E0FF");71 private WebColor objTotalBGColor = new WebColor("#FFC0C0");72 private WebColor objThankYouText = new WebColor("#000080");73 private CeteDAO ceteDAO = null;74 75 public MyInvoice() {76 // Top part of Invoice77 template.getElements().add(new Label("Northwind Traders\n1234 " +78 "International Drive\nAnywhere, Earth ABC123", 56, 0, 200, 44,79 Font.getHelvetica()));80 template.getElements().add(new Label("Invoice", 0, 0, 540, 18,81 Font.getHelveticaBold(), 18, TextAlign.RIGHT));82 83 template.getElements().add(new PageNumberingLabel("Page %%SP%% of %%ST%% ",84 450, 253, 90, 20, Font.getHelveticaBold(), 12, TextAlign.CENTER));85 86 // Add Invoice Details Template87 template.getElements().add(getDetailsGroup());88 89 // Add BillTo Template90 template.getElements().add(getBillToGroup());91 92 // Add ShipTo Template93 template.getElements().add(getShipToGroup());94 95 // Add Line Item Template96 template.getElements().add(getLineItemGroup());97 98 // Sets the image to the page template

  • This PDF was generated dynamically using DynamicPDF; Generator for Java. www.dynamicpdf.com

    Invoice.java99 setPageTemplateImage();100 }101 102 103 public Template getTemplate() {104 return template;105 }106 107 private Group getDetailsGroup() {108 // Returns a group containing the details template109 Group objGroup = new Group();110 111 objGroup.add(new Rectangle(340, 24, 200, 72, Grayscale.getBlack(),112 objBGColor, 0.5f));113 objGroup.add(new Label("Order ID:", 343, 25, 85, 12,114 Font.getHelveticaBold(), 12, TextAlign.RIGHT));115 objGroup.add(new Label("Order Date:", 343, 39, 85, 12,116 Font.getHelveticaBold(), 12, TextAlign.RIGHT));117 objGroup.add(new Label("Customer ID:", 343, 53, 85, 12,118 Font.getHelveticaBold(), 12, TextAlign.RIGHT));119 objGroup.add(new Label("Shipped Date:", 343, 67, 85, 12,120 Font.getHelveticaBold(), 12, TextAlign.RIGHT));121 objGroup.add(new Label("Order ID:", 343, 25, 85, 12,122 Font.getHelveticaBold(), 12, TextAlign.RIGHT));123 objGroup.add(new Label("Shipped Via:", 343, 81, 85, 12,124 Font.getHelveticaBold(), 12, TextAlign.RIGHT));125 126 return objGroup;127 }128 129 private Group getBillToGroup() {130 // Returns a group containing the bill to template131 Group objGroup = new Group();132 133 objGroup.add(new Rectangle(25, 120, 200, 90, 0.5f));134 objGroup.add(new Line(25, 136, 225, 136, 0.5f));135 objGroup.add(new Label("Bill To:", 28, 121, 200, 12,136 Font.getHelveticaBold(), 12));137 138 return objGroup;139 }140 141 private Group getShipToGroup() {142 // Returns a group containing the ship to template143 Group objGroup = new Group();144 145 objGroup.add(new Rectangle(315, 120, 200, 90, 0.5f));146 objGroup.add(new Line(315, 136, 515, 136, 0.5f));147 objGroup.add(new Label("Ship To:", 318, 121, 200, 12,

  • This PDF was generated dynamically using DynamicPDF; Generator for Java. www.dynamicpdf.com

    Invoice.java148 Font.getHelveticaBold(), 12));149 150 return objGroup;151 }152 153 private Group getLineItemGroup() {154 155 // Returns a group containing the line items template156 Group objGroup = new Group();157 158 for (int i = 0; i < 10; i++) {159 objGroup.add(new Rectangle(0, 306 + i * 36, 540, 18, objBGColor,160 objBGColor));161 }162 objGroup.add(new Rectangle(450, 250, 90, 20, 0.5f));163 objGroup.add(new Rectangle(450, 702, 90, 18, objTotalBGColor,164 objTotalBGColor));165 objGroup.add(new Rectangle(0, 270, 540, 450, 0.5f));166 objGroup.add(new Line(0, 288, 540, 288, 0.5f));167 objGroup.add(new Line(0, 666, 540, 666, 0.5f));168 objGroup.add(new Line(60, 270, 60, 666, 0.5f));169 objGroup.add(new Line(360, 270, 360, 720, 0.5f));170 objGroup.add(new Line(450, 270, 450, 720, 0.5f));171 objGroup.add(new Line(450, 702, 540, 702, 0.5f));172 objGroup.add(new Label("Quantity", 0, 272, 60, 12,173 Font.getHelveticaBold(), 12, TextAlign.CENTER));174 objGroup.add(new Label("Description", 60, 272, 300, 12,175 Font.getHelveticaBold(), 12, TextAlign.CENTER));176 objGroup.add(new Label("Unit Price", 360, 272, 90, 12,177 Font.getHelveticaBold(), 12, TextAlign.CENTER));178 objGroup.add(new Label("Price", 450, 272, 90, 12,179 Font.getHelveticaBold(), 12, TextAlign.CENTER));180 181 objGroup.add(new Label("Thank you for your purchase.\nWe " +182 "appreciate your business.", 5, 672, 350, 54,183 Font.getHelveticaBold(), 18, objThankYouText));184 objGroup.add(new Label("Sub Total", 364, 668, 82, 12,185 Font.getHelveticaBold(), 12, TextAlign.RIGHT));186 objGroup.add(new Label("Freight", 364, 686, 82, 12,187 Font.getHelveticaBold(), 12, TextAlign.RIGHT));188 objGroup.add(new Label("Total", 364, 704, 82, 12,189 Font.getHelveticaBold(), 12, TextAlign.RIGHT));190 return objGroup;191 }192 193 private void setPageTemplateImage() {194 // Adds the image to page template if it is not already added195 if (!pageTemplateImagesSet) {196 try {

  • This PDF was generated dynamically using DynamicPDF; Generator for Java. www.dynamicpdf.com

    Invoice.java197 template.getElements().add(new Image(getServletContext()198 .getRealPath("images/DPDFLogo.png"),199 0, 0, 0.16f));200 } catch (FileNotFoundException ex) {201 ex.printStackTrace(System.err);202 }203 pageTemplateImagesSet = true;204 }205 }206 207 public void draw(Connection connection, Document document,208 int invoiceNumber) {209 // Each Invoice should begin a new section210 document.getSections().begin();211 // Gets the Invoice data212 Vector v1 = getInvoiceData(connection, invoiceNumber);213 e1 = v1.elements();214 215 // Adds the invoice if there is data216 if (e1 != null) {217 // Draws the invoice data, returns a page object if it is218 // the last page219 Page objLastPage = drawInvoiceData(document);220 // Draws aditional pages if necessary221 while (objLastPage == null) {222 objLastPage = drawInvoiceData(document);223 }224 // Draws the totals to the bottom of the last page of the225 // Invoice226 drawTotals(ceteDAO, objLastPage);227 }228 229 }230 231 private Page drawInvoiceData(Document document) {232 // Tracks if the invoice is finished233 boolean invoiceFinished = true;234 // Tracks the y position on the page235 236 if (e1.hasMoreElements()) {237 ceteDAO = (CeteDAO)e1.nextElement();238 // Sets the Freight amount239 freight = ceteDAO.getFreight();240 }241 242 yOffset = 288;243 // Create a page for the Invoice244 Page objPage = new Page(PageSize.LETTER,245 PageOrientation.PORTRAIT, 36.0f);

  • This PDF was generated dynamically using DynamicPDF; Generator for Java. www.dynamicpdf.com

    Invoice.java246 // Add Details to the Invoice247 drawInvoiceDetails(ceteDAO, objPage);248 // Add bill to address249 drawBillTo(ceteDAO, objPage);250 // Add ship to address251 drawShipTo(ceteDAO, objPage);252 253 // Add line items to the Invoice254 drawLineItem(ceteDAO, objPage);255 256 while (e1.hasMoreElements()) {257 // Break if at the bottom of the page258 if (yOffset >= 666) {259 invoiceFinished = false;260 break;261 }262 ceteDAO = (CeteDAO)e1.nextElement();263 // Add line items to the Invoice264 drawLineItem(ceteDAO, objPage);265 }266 267 // Add the page to the document268 document.getPages().add(objPage);269 270 // If Invoice is finished return the page else return null so271 // another page will be added272 if (invoiceFinished) {273 return objPage;274 } else {275 objPage.getElements().add(new Label("Continued...", 454, 704, 82,276 12, Font.getHelvetica(), 12, TextAlign.RIGHT));277 return null;278 }279 }280 281 private void drawInvoiceDetails(CeteDAO ceteDAO, Page page) {282 283 // Adds Invoice details to the page284 page.getElements().add(new Label(String.valueOf(ceteDAO285 .getOrderID()), 437, 25, 100, 12, Font.getHelvetica(),286 12));287 SimpleDateFormat dateFormat = new SimpleDateFormat("d MMM yyyy");288 page.getElements().add(new Label(dateFormat.format(ceteDAO289 .getOrderDate()), 437, 39, 100, 12,290 Font.getHelvetica(), 12));291 page.getElements().add(new Label(String.valueOf(ceteDAO292 .getCustomerID()), 437, 53, 100, 12,293 Font.getHelvetica(), 12));294 Date date = ceteDAO.getShippedDate();

  • This PDF was generated dynamically using DynamicPDF; Generator for Java. www.dynamicpdf.com

    Invoice.java295 if (date != null) {296 page.getElements().add(new Label(dateFormat.format(date),297 437, 67, 100, 12, Font.getHelvetica(), 12));298 }299 page.getElements().add(new Label(ceteDAO.getShipperName(),300 437, 81, 100, 12, Font.getHelvetica(), 12));301 page.getElements().add(new Interleaved25(String.valueOf(ceteDAO302 .getOrderID()), 380, 4, 18, false));303 304 }305 306 private void drawBillTo(CeteDAO ceteDAO, Page page) {307 308 // Adds bill to address309 String billToAddress = ceteDAO.getBillName() + "\n" +310 ceteDAO.getBillAddress() + "\n" +311 ceteDAO.getBillCity() + ", " +312 ceteDAO.getBillPostalCode() + "\n" +313 ceteDAO.getBillCountry();314 page.getElements().add(new TextArea(billToAddress, 28, 139,315 194, 49, Font.getHelvetica(), 12));316 317 }318 319 private void drawShipTo(CeteDAO ceteDAO, Page page) {320 321 // Adds ship to address322 String shipToAddress = ceteDAO.getShipName() + "\n" +323 ceteDAO.getShipAddress() + "\n" +324 ceteDAO.getShipCity() + ", " +325 ceteDAO.getShipPostalCode() + "\n" +326 ceteDAO.getShipCountry();327 page.getElements().add(new TextArea(shipToAddress, 318, 139,328 194, 49, Font.getHelvetica(), 12));329 330 }331 332 private void drawLineItem(CeteDAO ceteDAO, Page page) {333 // Adds a line item to the invoice334 BigDecimal quantity = new BigDecimal(0.0);335 BigDecimal unitPrice = new BigDecimal(0.0);336 quantity = new BigDecimal(ceteDAO.getQuantity());337 unitPrice = ceteDAO.getUnitPrice();338 339 BigDecimal lineTotal = unitPrice.multiply(quantity);340 subTotal = subTotal.add(lineTotal);341 342 page.getElements().add(new Label(quantity.toString(), 4,343 3 + yOffset, 52, 12, Font.getHelvetica(), 12,

  • This PDF was generated dynamically using DynamicPDF; Generator for Java. www.dynamicpdf.com

    Invoice.java344 TextAlign.RIGHT));345 346 page.getElements().add(new Label(ceteDAO.getProductName(),347 64, 3 + yOffset, 292, 12, Font.getHelvetica(), 12));348 349 unitPrice = unitPrice.setScale(2, BigDecimal.ROUND_HALF_EVEN);350 lineTotal = lineTotal.setScale(2, BigDecimal.ROUND_HALF_EVEN);351 DecimalFormat df = new DecimalFormat("#,##0.00");352 page.getElements().add(new Label(df.format(unitPrice353 .doubleValue()), 364, 3 + yOffset, 82, 12,354 Font.getHelvetica(), 12, TextAlign.RIGHT));355 page.getElements().add(new Label(df.format(lineTotal356 .doubleValue()), 454, 3 + yOffset, 82, 12,357 Font.getHelvetica(), 12, TextAlign.RIGHT));358 359 yOffset += 18; 360 }361 362 private void drawTotals(CeteDAO ceteDAO, Page page) {363 // Add totals to the bottom of the Invoice364 BigDecimal grandTotal = subTotal.add(freight);365 subTotal = subTotal.setScale(2, BigDecimal.ROUND_HALF_EVEN);366 freight = freight.setScale(2, BigDecimal.ROUND_HALF_EVEN);367 grandTotal = grandTotal.setScale(2, BigDecimal.ROUND_HALF_EVEN);368 DecimalFormat df = new DecimalFormat("#,##0.00");369 page.getElements().add(new Label(df.format(subTotal370 .doubleValue()), 454, 667, 82, 12, Font.getHelvetica(), 12,371 TextAlign.RIGHT));372 page.getElements().add(new Label(df.format(freight373 .doubleValue()), 454, 685, 82, 12, Font.getHelvetica(), 12,374 TextAlign.RIGHT));375 page.getElements().add(new Label(df.format(grandTotal376 .doubleValue()), 454, 703, 82, 12, Font.getHelvetica(), 12,377 TextAlign.RIGHT));378 }379 380 private Vector getInvoiceData(Connection connection,381 int invoiceNumber) {382 Vector v1 = null;383 // Creates a ResultSet for the Invioce384 try {385 PreparedStatement ps = connection.prepareStatement(386 "SELECT o.OrderID, o.CustomerID, o.OrderDate, o.ShippedDate, Freight, " +387 "o.ShipName, o.ShipAddress, o.ShipCity, o.ShipPostalCode, o.ShipCountry, " +388 "c.CompanyName as BillName, c.Address as BillAddress, " +389 "c.City as BillCity, c.PostalCode as BillPostalCode, " +390 "c.Country as BillCountry, s.CompanyName as ShipperName, " +391 "p.ProductName, od.UnitPrice, od.Quantity " +392 "FROM Orders o " +

  • This PDF was generated dynamically using DynamicPDF; Generator for Java. www.dynamicpdf.com

    Invoice.java393 "JOIN Customers c ON " +394 "o.CustomerID = c.CustomerID " +395 "JOIN Shippers s ON " +396 "o.ShipVia = s.ShipperID " +397 "JOIN [Order Details] od ON " +398 " o.OrderID = od.OrderID " +399 "JOIN Products p ON " +400 "od.ProductID = p.ProductID " +401 "WHERE o.OrderID = ? ");402 403 ps.setInt(1, invoiceNumber);404 ResultSet rs = ps.executeQuery();405 v1 = new Vector(1,1);406 while (rs.next()) {407 CeteDAO ceteDAO = new CeteDAO(rs.getInt(1), rs.getString(2),408 rs.getDate(3), rs.getDate(4),409 rs.getBigDecimal(5), rs.getString(6),410 rs.getString(7), rs.getString(8),411 rs.getString(9), rs.getString(10),412 rs.getString(11), rs.getString(12),413 rs.getString(13), rs.getString(14),414 rs.getString(15), rs.getString(16),415 rs.getString(17), rs.getBigDecimal(18),416 rs.getShort(19));417 v1.add(ceteDAO);418 }419 } catch (SQLException ex1) {420 ex1.printStackTrace(System.err);421 }422 return v1;423 }424 }425 426 class CeteDAO {427 428 private int orderID;429 private String customerID;430 private Date orderDate;431 private Date shippedDate;432 private BigDecimal freight;433 private String shipName;434 private String shipAddress;435 private String shipCity;436 private String shipPostalCode;437 private String shipCountry;438 private String billName;439 private String billAddress;440 private String billCity;441 private String billPostalCode;

  • This PDF was generated dynamically using DynamicPDF; Generator for Java. www.dynamicpdf.com

    Invoice.java442 private String billCountry;443 private String shipperName;444 private String productName;445 private BigDecimal unitPrice;446 private short quantity;447 448 public CeteDAO(int orderID, String customerID, Date orderDate,449 Date shippedDate, BigDecimal freight, String shipName,450 String shipAddress, String shipCity, String shipPostalCode,451 String shipCountry, String billName, String billAddress,452 String billCity, String billPostalCode, String billCountry,453 String shipperName, String productName, BigDecimal unitPrice,454 short quantity) {455 this.orderID = orderID;456 this.customerID = customerID;457 this.orderDate = orderDate;458 this.shippedDate = shippedDate;459 this.freight = freight;460 this.shipName = shipName;461 this.shipAddress = shipAddress;462 this.shipCity = shipCity;463 this.shipPostalCode = shipPostalCode;464 this.shipCountry = shipCountry;465 this.billName = billName;466 this.billAddress = billAddress;467 this.billCity = billCity;468 this.billPostalCode = billPostalCode;469 this.billCountry = billCountry;470 this.shipperName = shipperName;471 this.productName = productName;472 this.unitPrice = unitPrice;473 this.quantity = quantity;474 }475 476 public int getOrderID() {477 return orderID;478 }479 480 public String getCustomerID() {481 return customerID;482 }483 484 public Date getOrderDate() {485 return orderDate;486 }487 488 public Date getShippedDate() {489 return shippedDate;490 }

  • This PDF was generated dynamically using DynamicPDF; Generator for Java. www.dynamicpdf.com

    Invoice.java491 492 public BigDecimal getFreight() {493 return freight;494 }495 496 public String getShipName() {497 return shipName;498 }499 500 public String getShipAddress() {501 return shipAddress;502 }503 504 public String getShipCity() {505 return shipCity;506 }507 508 public String getShipPostalCode() {509 return shipPostalCode;510 }511 512 public String getShipCountry() {513 return shipCountry;514 }515 516 public String getBillName() {517 return billName;518 }519 520 public String getBillAddress() {521 return billAddress;522 }523 524 public String getBillCity() {525 return billCity;526 }527 528 public String getBillPostalCode() {529 return billPostalCode;530 }531 532 public String getBillCountry() {533 return billCountry;534 }535 536 public String getShipperName() {537 return shipperName;538 }539

  • This PDF was generated dynamically using DynamicPDF; Generator for Java. www.dynamicpdf.com

    Invoice.java540 public String getProductName() {541 return productName;542 }543 544 public BigDecimal getUnitPrice() {545 return unitPrice;546 }547 548 public short getQuantity() {549 return quantity;550 }551 552 553 554 555 }556 557 558 559 560 }