Example #1
0
        void HtmlToPdf(string htmlAnexo, string rutaDestinoAnexo, Rectangle tamanoPagina, int margenDerecho, int margenIzquierdo, int margenTop, int margenBotton, Boolean rotate)
        {
            Byte[]     bytes;
            PdfWriter  writer     = null;
            HTMLWorker htmlWorker = null;

            try
            {
                using (var ms = new MemoryStream())
                {
                    using (var doc = new Document(tamanoPagina, margenDerecho, margenIzquierdo, margenTop, margenBotton))
                    {
                        if (rotate)
                        {
                            doc.SetPageSize(PageSize.LEGAL.Rotate());
                        }

                        writer = PdfWriter.GetInstance(doc, ms);

                        doc.Open();

                        var example_html = htmlAnexo;
                        htmlWorker = new HTMLWorker(doc);

                        using (var sr = new StringReader(example_html))
                        {
                            htmlWorker.Parse(sr);
                        }
                        doc.Close();
                    }
                    bytes = ms.ToArray();
                }

                System.IO.File.WriteAllBytes(rutaDestinoAnexo, bytes);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Dispose();
                }
                if (htmlWorker != null)
                {
                    htmlWorker.Dispose();
                }
            }
        }
Example #2
0
        private string GenerateInvoicePdf(InvoiceModel invoiceModel)
        {
            try
            {
                string HtmlString;
                HtmlString = File.ReadAllText(HttpContext.Current.Server.MapPath("~/Templates/Invoice.html"));
                // HtmlString = HtmlString.Replace("{{ClientName}}", invoiceModel.ClientName);

                string loc      = HttpContext.Current.Server.MapPath(@"/Images/icon/");
                var    compiler = new Helper.HtmlTemplate();
                HtmlString = compiler.Render(HtmlString, new { ClientName     = invoiceModel.ClientName, Country = invoiceModel.Country, EmailId = invoiceModel.EmailId, InvoiceDate = invoiceModel.InvoiceDate, InvoiceNumber = invoiceModel.InvoiceNumber, PlanName = invoiceModel.PlanName, Price = invoiceModel.Price, TotalAccounts = invoiceModel.TotalAccounts, ApplicationFees = invoiceModel.ApplicationFee, SubTotal = invoiceModel.SubTotal, Total = invoiceModel.Total,
                                                               CreditedAmount = invoiceModel.CreditedAmount, EmailImage = loc + "Email_icon.png", CountryImage = loc + "website_icon.png", Logo = loc + "Simple2tradelogo.png", PlanLogo = loc + invoiceModel.PlanLogo });
                //if (Convert.ToInt32(invoiceModel.CreditedAmount) == 0)
                //{
                //    var creditAmountString = HtmlString.Remove();
                //    HtmlString = HtmlString.Remove();
                //}
                string path = HttpContext.Current.Server.MapPath(@"/Invoices");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                Document   document        = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
                string     fileName        = invoiceModel.InvoiceNumber + "_" + Guid.NewGuid().ToString() + ".pdf";
                string     filePathAndName = "/Invoices/" + fileName;
                string     fileSavingPath  = HttpContext.Current.Server.MapPath(@filePathAndName);
                FileStream fileStream      = new FileStream(fileSavingPath, FileMode.Create);
                PdfWriter  writer          = PdfWriter.GetInstance(document, fileStream);
                document.Open();
                HTMLWorker   hw = new HTMLWorker(document);
                StringReader sr = new StringReader(HtmlString);
                //hw.Parse(new StringReader(HtmlString.ToString()));
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, sr);
                document.Close();
                hw.Dispose();
                sr.Dispose();
                fileStream.Close();
                writer.Close();

                return(filePathAndName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }