//Invoice Report public void Invoice(string invoiceId) { var invoice = StripeHelper.GetInvoice(invoiceId); var unitprice = ((double)invoice.StripeInvoiceLineItems.Data.First().Amount) / 100; var quantity = 1; var tax = invoice.Tax == null ? 0 : (double)(invoice.Tax) / 100; var total = unitprice * quantity + tax; var amount = (double)invoice.Total / 100; //after discount var discount = Math.Round(total - amount, 2, MidpointRounding.AwayFromZero); var username = HttpContext.User.Identity.Name + " " + SessionData.LastName; var email = SessionData.Email; var desc = "Monthly Plan - " + invoice.StripeInvoiceLineItems.Data.First().Plan.Name; var fileName = "Invoice_MPP_" + invoice.StripeInvoiceLineItems.Data.First().Plan.Name + "_" + DateTime.Now.ToShortDateString(); Document document = new Document(PageSize.A4, 88f, 88f, 50f, 10f); Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK); using (MemoryStream ms = new MemoryStream()) { PdfWriter writer = PdfWriter.GetInstance(document, ms); Phrase phrase = null; PdfPCell cell = null; PdfPTable table = null; BaseColor color = null; document.Open(); //row1 //Header Table table = new PdfPTable(2); table.TotalWidth = 500f; table.LockedWidth = true; table.SetWidths(new float[] { 0.5f, 0.5f }); //Company Name and Address phrase = new Phrase(); phrase.Add(new Chunk("INVOICE", FontFactory.GetFont("Arial", 20, Font.BOLD, BaseColor.BLACK))); cell = PhraseCell(phrase, Element.ALIGN_LEFT); cell.VerticalAlignment = Element.ALIGN_TOP; table.AddCell(cell); //Company Logo cell = ImageCell("~/content/images/logo.png", 80f, Element.ALIGN_RIGHT); table.AddCell(cell); document.Add(table); // separation line header color = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#A9A9A9")); //line1 DrawLine(writer, 25f, document.Top - 50f, document.PageSize.Width - 25f, document.Top - 50f, color); DrawLine(writer, 25f, document.Top - 51f, document.PageSize.Width - 25f, document.Top - 51f, color); //vendor or user details table = new PdfPTable(3); table.TotalWidth = 500f; table.LockedWidth = true; table.SetWidths(new float[] { 0.3f, 0.3f, 0.4f }); table.SpacingBefore = 80f; //row2 //user address phrase = new Phrase(); phrase.Add(new Chunk(username + "\n\n" + email, FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK))); cell = PhraseCell(phrase, Element.ALIGN_LEFT); table.AddCell(cell); //Date phrase = new Phrase(); phrase.Add(new Chunk("Date\n\n" + invoice.Date.Value.ToShortDateString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK))); cell = PhraseCell(phrase, Element.ALIGN_CENTER); table.AddCell(cell); //vendor address phrase = new Phrase(); phrase.Add(new Chunk("My PPC Pal, LLC \n P.O. Box 2126 \n Yorba Linda, CA 92885", FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK))); cell = PhraseCell(phrase, Element.ALIGN_RIGHT); table.AddCell(cell); document.Add(table); //row 3 table = new PdfPTable(1); table.TotalWidth = 500f; table.LockedWidth = true; table.SpacingBefore = 35f; phrase = new Phrase(); phrase.Add(new Chunk("Invoice Number : " + invoice.Id + "\n", FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK))); cell = PhraseCell(phrase, Element.ALIGN_LEFT); table.AddCell(cell); document.Add(table); DrawLine(writer, 25f, document.Top - 190f, document.PageSize.Width - 25f, document.Top - 190f, BaseColor.BLACK); //line 2 //row 4 var FontColor = new BaseColor(51, 122, 183); var HeaderFont = FontFactory.GetFont("Arial", 10, Font.BOLD, FontColor); table = new PdfPTable(5); table.TotalWidth = 500f; table.LockedWidth = true; table.SpacingBefore = 15f; table.SetWidths(new float[] { 0.4f, 0.2f, 0.2f, 0.1f, 0.1f }); //description phrase = new Phrase(); phrase.Add(new Chunk("Description", HeaderFont)); cell = PhraseCell(phrase, Element.ALIGN_LEFT); table.AddCell(cell); //Unit price phrase = new Phrase(); phrase.Add(new Chunk("Unit Price", HeaderFont)); cell = PhraseCell(phrase, Element.ALIGN_LEFT); table.AddCell(cell); //Discount phrase = new Phrase(); phrase.Add(new Chunk("Discount", HeaderFont)); cell = PhraseCell(phrase, Element.ALIGN_LEFT); table.AddCell(cell); //Tax phrase = new Phrase(); phrase.Add(new Chunk("Tax", HeaderFont)); cell = PhraseCell(phrase, Element.ALIGN_LEFT); table.AddCell(cell); //Amount phrase = new Phrase(); phrase.Add(new Chunk("Amount", HeaderFont)); cell = PhraseCell(phrase, Element.ALIGN_LEFT); table.AddCell(cell); document.Add(table); DrawLine(writer, 25f, document.Top - 220, document.PageSize.Width - 25f, document.Top - 220f, BaseColor.BLACK); //line 3 //row 5 table = new PdfPTable(5); table.TotalWidth = 500f; table.LockedWidth = true; table.SpacingBefore = 25f; table.SetWidths(new float[] { 0.4f, 0.2f, 0.2f, 0.1f, 0.1f }); //description phrase = new Phrase(); phrase.Add(new Chunk(desc, FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK))); cell = PhraseCell(phrase, Element.ALIGN_LEFT); table.AddCell(cell); //Unit price phrase = new Phrase(); phrase.Add(new Chunk("$ " + unitprice.ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK))); cell = PhraseCell(phrase, Element.ALIGN_LEFT); table.AddCell(cell); //Discount phrase = new Phrase(); phrase.Add(new Chunk("$ " + discount.ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK))); cell = PhraseCell(phrase, Element.ALIGN_LEFT); table.AddCell(cell); //Tax phrase = new Phrase(); phrase.Add(new Chunk("$ " + tax.ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK))); cell = PhraseCell(phrase, Element.ALIGN_LEFT); table.AddCell(cell); //Amount phrase = new Phrase(); phrase.Add(new Chunk("$ " + amount.ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK))); cell = PhraseCell(phrase, Element.ALIGN_LEFT); table.AddCell(cell); document.Add(table); DrawLine(writer, 25f, document.Top - 260, document.PageSize.Width - 25f, document.Top - 260f, BaseColor.BLACK); //line 3 //row 6 table = new PdfPTable(2); table.TotalWidth = 500f; table.LockedWidth = true; table.SpacingBefore = 25f; table.SetWidths(new float[] { 0.9f, 0.1f }); //Total phrase = new Phrase(); phrase.Add(new Chunk("Total:", FontFactory.GetFont("Arial", 10, Font.BOLD, BaseColor.BLACK))); cell = PhraseCell(phrase, Element.ALIGN_RIGHT); table.AddCell(cell); //Total Amount phrase = new Phrase(); phrase.Add(new Chunk("$ " + amount.ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK))); cell = PhraseCell(phrase, Element.ALIGN_LEFT); table.AddCell(cell); document.Add(table); //row 7 table = new PdfPTable(1); table.TotalWidth = 500f; table.LockedWidth = true; table.SpacingBefore = 80f; //declaration var card = StripeHelper.GetCard(SessionData.StripeCustId); if (SessionData.StripeCustId != null && card != null && card.FirstOrDefault() != null) { var str = "Your card on file ending in " + StripeHelper.GetCard(SessionData.StripeCustId).FirstOrDefault().Last4 + " will be billed automatically for the total amount shown on this invoice"; phrase = new Phrase(); phrase.Add(new Chunk(str, FontFactory.GetFont("Arial", 9, Font.NORMAL, BaseColor.BLACK))); cell = PhraseCell(phrase, Element.ALIGN_LEFT); table.AddCell(cell); document.Add(table); } //row 8 table = new PdfPTable(1); table.TotalWidth = 500f; table.LockedWidth = true; table.SpacingBefore = 50f; //declaration var renewals = @"Renewals: This order renews for additional 1 month periods, unless either party provides the other with a notice of non-renewal prior to the renewal date. Terms: This order is governed by the terms of the Subscription Services Agreement between the parties, which terms are incorporated into this order for all purposes. If there is a conflict between the terms of this order and the agreement, this order governs. This order and the agreement are the entire agreement between the parties, and they supersede and replace all prior and contemporaneous negotiations, agreements, representations and discussions regarding this subject matter. Only a signed writing of the parties may amend this order."; renewals = renewals.Replace(Environment.NewLine, String.Empty).Replace(" ", String.Empty); Chunk beginning = new Chunk(renewals, FontFactory.GetFont("Arial", 6)); Phrase p1 = new Phrase(beginning); cell = PhraseCell(p1, Element.ALIGN_LEFT); table.AddCell(cell); document.Add(table); document.Close(); byte[] bytes = ms.ToArray(); ms.Close(); Response.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".pdf"); Response.ContentType = "application/pdf"; Response.Buffer = true; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.BinaryWrite(bytes); Response.End(); Response.Close(); } }