Example #1
0
        private byte[] HtmlToPdf(string htmlBody, string htmlFooter)
        {
            // get parameters
            string headerUrl = Startup.ServerPath + @"\wwwroot\templates\header.html";
            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();

            // header settings
            converter.Options.DisplayHeader = true;
            converter.Header.Height         = 20;
            PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl)
            {
                AutoFitHeight = HtmlToPdfPageFitMode.AutoFit
            };

            converter.Header.Add(headerHtml);
            // footer settings
            converter.Options.DisplayFooter = true;
            converter.Footer.Height         = 165;
            PdfHtmlSection footerHtml = new PdfHtmlSection(htmlFooter, null)
            {
                AutoFitHeight = HtmlToPdfPageFitMode.AutoFit,
                AutoFitWidth  = HtmlToPdfPageFitMode.AutoFit,
                WebPageWidth  = 793
            };

            converter.Footer.Add(footerHtml);
            // read parameters from the webpage
            PdfPageSize        pageSize       = PdfPageSize.A4;
            PdfPageOrientation pdfOrientation = PdfPageOrientation.Portrait;
            int webPageWidth  = 793;
            int webPageHeight = 0;

            // set converter options
            converter.Options.PdfPageSize        = pageSize;
            converter.Options.PdfPageOrientation = pdfOrientation;
            converter.Options.WebPageWidth       = webPageWidth;
            converter.Options.WebPageHeight      = webPageHeight;
            converter.Options.MarginLeft         = 0;
            converter.Options.MarginRight        = 0;
            //set document permissions
            converter.Options.SecurityOptions.CanAssembleDocument = false;
            converter.Options.SecurityOptions.CanCopyContent      = true;
            converter.Options.SecurityOptions.CanEditAnnotations  = false;
            converter.Options.SecurityOptions.CanEditContent      = false;
            converter.Options.SecurityOptions.CanPrint            = true;
            converter.Options.PdfDocumentInformation.Title        = "CPD Activity Accreditation";
            converter.Options.PdfDocumentInformation.CreationDate = DateTime.UtcNow;
            converter.Options.PdfDocumentInformation.Subject      = "CPD Activity Accreditation";
            converter.Options.PdfDocumentInformation.Keywords     = "scfhs.org.sa";
            // create a new pdf document converting an url
            SelectPdf.PdfDocument doc = converter.ConvertHtmlString(htmlBody);
            // save pdf document
            byte[] pdf = doc.Save();
            // close pdf document
            doc.Close();
            // return resulted pdf document
            return(pdf);
        }
Example #2
0
        public static void ConvertContractHtmlCodeToPdf(PdfModel model)
        {
            HtmlToPdf converter = new HtmlToPdf();

            if (model.HeaderUrl != null)
            {
                // header settings
                converter.Options.DisplayHeader     = true;
                converter.Header.DisplayOnFirstPage = true;
                converter.Header.DisplayOnOddPages  = true;
                converter.Header.DisplayOnEvenPages = true;
                converter.Header.Height             = 70;

                // add some html content to the header
                PdfHtmlSection headerHtml = new PdfHtmlSection(model.HeaderUrl);
                headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
                headerHtml.AutoFitWidth  = HtmlToPdfPageFitMode.AutoFit;
                converter.Header.Add(headerHtml);
            }

            if (model.FooterUrl != null)
            {
                // footer settings
                converter.Options.DisplayFooter     = true;
                converter.Footer.DisplayOnFirstPage = true;
                converter.Footer.DisplayOnOddPages  = true;
                converter.Footer.DisplayOnEvenPages = true;
                converter.Footer.Height             = 60;

                // add some html content to the footer
                PdfHtmlSection footerHtml = new PdfHtmlSection(model.FooterUrl, "");
                footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
                converter.Footer.Add(footerHtml);

                PdfTextSection text = new PdfTextSection(0, 10, "Page: {page_number} of {total_pages}  ", new System.Drawing.Font("Arial", 8));
                text.HorizontalAlign = PdfTextHorizontalAlign.Right;
                converter.Footer.Add(text);
            }

            converter.Options.PdfPageSize        = PdfPageSize.A4;
            converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait;
            converter.Options.MarginLeft         = 30;
            converter.Options.MarginRight        = 30;
            converter.Options.MarginTop          = 20;
            converter.Options.MarginBottom       = 25;

            PdfDocument doc = converter.ConvertHtmlString(model.HtmlString);

            doc.Save(model.RepositoryPath);
            doc.Close();
        }
Example #3
0
        public static void ConvertHtmlCodeToPdf(PdfModel model)
        {
            HtmlToPdf converter = new HtmlToPdf();

            if (model.HeaderUrl != null)
            {
                // header settings
                converter.Options.DisplayHeader     = true;
                converter.Header.DisplayOnFirstPage = true;
                converter.Header.DisplayOnOddPages  = true;
                converter.Header.DisplayOnEvenPages = true;
                converter.Header.Height             = 70;

                // add some html content to the header
                PdfHtmlSection headerHtml = new PdfHtmlSection(model.HeaderUrl);
                headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
                headerHtml.AutoFitWidth  = HtmlToPdfPageFitMode.AutoFit;
                converter.Header.Add(headerHtml);
            }

            if (model.FooterUrl != null)
            {
                // footer settings
                converter.Options.DisplayFooter     = true;
                converter.Footer.DisplayOnFirstPage = true;
                converter.Footer.DisplayOnOddPages  = true;
                converter.Footer.DisplayOnEvenPages = true;
                converter.Footer.Height             = 50;

                // add some html content to the footer
                PdfHtmlSection footerHtml = new PdfHtmlSection(model.FooterUrl, "");
                footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
                converter.Footer.Add(footerHtml);
            }

            converter.Options.PdfPageSize        = PdfPageSize.A4;
            converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait;
            converter.Options.MarginLeft         = 10;
            converter.Options.MarginRight        = 10;
            converter.Options.MarginTop          = 20;
            converter.Options.MarginBottom       = 20;

            PdfDocument doc = converter.ConvertHtmlString(model.HtmlString, null);

            doc.Save(model.RepositoryPath);
            doc.Close();
        }
Example #4
0
        public Byte[] ConvertHtmlCodeToPdf(string html, string footerHml)
        {
            int webPageWidth  = 1050;
            int webPageHeight = 0;



            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();

            // set converter options
            converter.Options.PdfPageSize   = PdfPageSize.A4;
            converter.Options.MarginLeft    = 0;
            converter.Options.MarginRight   = 0;
            converter.Options.MarginTop     = 0;
            converter.Options.MarginBottom  = 0;
            converter.Options.DisplayFooter = true;
            // BuildMyString.com generated code. Please enjoy your string responsibly.

            if (!string.IsNullOrEmpty(footerHml))
            {
                PdfHtmlSection footerHtml = new PdfHtmlSection(footerHml, "");
                converter.Footer.Add(footerHtml);
                converter.Footer.Height  = 50;
                footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            }


            converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait;
            converter.Options.WebPageWidth       = webPageWidth;
            converter.Options.WebPageHeight      = webPageHeight;

            // create a new pdf document converting an url
            SelectPdf.PdfDocument doc = converter.ConvertHtmlString(html, _baseUrl);

            // save pdf document
            byte[] pdf = doc.Save();

            // close pdf document
            doc.Close();

            return(pdf);
        }
        public static void GeneratePDF(string outPath, string docBase, string outFileName, string html, string logoPrifix, string title, string footerWarning, int?totalPages)
        {
            ImageUtil.ValidateInit();

            //path for the PDF file to be generated
            string docDir          = string.Format("{0}/{1}", System.Web.Hosting.HostingEnvironment.MapPath(docBase), outPath);
            string outFileFullPath = string.Format("{0}/{1}", docDir, outFileName);

            if (System.IO.Directory.Exists(docDir))
            {
                try
                {
                    if (System.IO.File.Exists(outFileFullPath))
                    {
                        System.IO.File.Delete(outFileFullPath);
                    }
                }
                catch { }
            }
            else
            {
                System.IO.Directory.CreateDirectory(docDir);
            }

            try
            {
                HtmlToPdf converter = new HtmlToPdf();

                converter.Options.PdfPageSize        = PdfPageSize.A4;
                converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait;
                converter.Options.MarginLeft         = 30;
                converter.Options.MarginRight        = 30;
                converter.Options.MarginTop          = 5;
                converter.Options.MarginBottom       = 10;

                converter.Options.WebPageWidth = 720;
                if (string.IsNullOrEmpty(footerWarning))
                {
                    converter.Options.WebPageHeight = 800;
                }
                else
                {
                    converter.Options.WebPageHeight = 600;
                }
                // header settings
                converter.Options.DisplayHeader     = true;
                converter.Header.DisplayOnFirstPage = true;
                converter.Header.DisplayOnOddPages  = true;
                converter.Header.DisplayOnEvenPages = true;
                converter.Header.Height             = 80;

                // footer settings
                converter.Options.DisplayFooter     = true;
                converter.Footer.DisplayOnFirstPage = true;
                converter.Footer.DisplayOnOddPages  = true;
                converter.Footer.DisplayOnEvenPages = true;
                if (!string.IsNullOrEmpty(footerWarning))
                {
                    converter.Footer.Height = 80;
                }
                else
                {
                    converter.Footer.Height = 60;
                }

                int y = 0;
                if (!string.IsNullOrEmpty(footerWarning))
                {
                    PdfTextSection text2 = new PdfTextSection(0, y,
                                                              footerWarning,
                                                              new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold));
                    text2.HorizontalAlign = PdfTextHorizontalAlign.Center;
                    text2.ForeColor       = System.Drawing.Color.Red;
                    converter.Footer.Add(text2);
                    y += 25;
                }

                PdfHtmlSection footerHtml = new PdfHtmlSection(0, y, footerContent, null);
                footerHtml.AutoFitWidth = HtmlToPdfPageFitMode.AutoFit;
                converter.Footer.Add(footerHtml);
                y += 40;

                // add page numbering element to the footer
                // page numbers can be added using a PdfTextSection object
                PdfTextSection text = new PdfTextSection(0, y,
                                                         title + " Page: {page_number} of {total_pages}  ",
                                                         new System.Drawing.Font("Arial", 8));
                text.HorizontalAlign = PdfTextHorizontalAlign.Center;
                converter.Footer.Add(text);

                // create a new pdf document converting an url
                PdfDocument doc = converter.ConvertHtmlString(html);


                // create memory stream to save PDF
                MemoryStream pdfStream = new MemoryStream();

                // save pdf document into a MemoryStream
                doc.Save(pdfStream);

                // reset stream position
                pdfStream.Position = 0;

                // close pdf document
                doc.Close();

                System.IO.File.WriteAllBytes(outFileFullPath, pdfStream.ToArray());
            }
            finally
            {
            }
        }
        private void BtnCreatePdf_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            string file = "Document.pdf";

            try
            {
                // get parameters
                bool showHeaderOnFirstPage = ChkHeaderFirstPage.Checked;
                bool showHeaderOnOddPages  = ChkHeaderOddPages.Checked;
                bool showHeaderOnEvenPages = ChkHeaderEvenPages.Checked;

                int headerHeight = 50;
                try
                {
                    headerHeight = Convert.ToInt32(TxtHeaderHeight.Text);
                }
                catch { }


                bool showFooterOnFirstPage = ChkFooterFirstPage.Checked;
                bool showFooterOnOddPages  = ChkFooterOddPages.Checked;
                bool showFooterOnEvenPages = ChkFooterEvenPages.Checked;

                int footerHeight = 50;
                try
                {
                    footerHeight = Convert.ToInt32(TxtFooterHeight.Text);
                }
                catch { }

                // instantiate a html to pdf converter object
                HtmlToPdf converter = new HtmlToPdf();

                // header settings
                converter.Options.DisplayHeader = showHeaderOnFirstPage ||
                                                  showHeaderOnOddPages || showHeaderOnEvenPages;
                converter.Header.DisplayOnFirstPage = showHeaderOnFirstPage;
                converter.Header.DisplayOnOddPages  = showHeaderOnOddPages;
                converter.Header.DisplayOnEvenPages = showHeaderOnEvenPages;
                converter.Header.Height             = headerHeight;

                PdfHtmlSection headerHtml = new PdfHtmlSection(Path.GetFullPath(headerUrl));
                headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
                converter.Header.Add(headerHtml);

                // footer settings
                converter.Options.DisplayFooter = showFooterOnFirstPage ||
                                                  showFooterOnOddPages || showFooterOnEvenPages;
                converter.Footer.DisplayOnFirstPage = showFooterOnFirstPage;
                converter.Footer.DisplayOnOddPages  = showFooterOnOddPages;
                converter.Footer.DisplayOnEvenPages = showFooterOnEvenPages;
                converter.Footer.Height             = footerHeight;

                PdfHtmlSection footerHtml = new PdfHtmlSection(Path.GetFullPath(footerUrl));
                footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
                converter.Footer.Add(footerHtml);

                // add page numbering element to the footer
                if (ChkPageNumbering.Checked)
                {
                    // page numbers can be added using a PdfTextSection object
                    PdfTextSection text = new PdfTextSection(0, 10,
                                                             "Page: {page_number} of {total_pages}  ",
                                                             new System.Drawing.Font("Arial", 8));
                    text.HorizontalAlign = PdfTextHorizontalAlign.Right;
                    converter.Footer.Add(text);
                }

                // create a new pdf document converting an url
                PdfDocument doc = converter.ConvertUrl(TxtUrl.Text);

                // save pdf document
                doc.Save(file);

                // close pdf document
                doc.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Error: {0}", ex.Message),
                                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            finally
            {
                Cursor = Cursors.Arrow;
            }

            // open generated pdf
            try
            {
                System.Diagnostics.Process.Start(file);
            }
            catch
            {
                MessageBox.Show("Could not open generated pdf document",
                                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        protected void BtnCreatePdf_Click(object sender, EventArgs e)
        {
            // get parameters
            string headerUrl = Server.MapPath("~/files/header.html");
            string footerUrl = Server.MapPath("~/files/footer.html");

            bool showHeaderOnFirstPage = ChkHeaderFirstPage.Checked;
            bool showHeaderOnOddPages = ChkHeaderOddPages.Checked;
            bool showHeaderOnEvenPages = ChkHeaderEvenPages.Checked;

            int headerHeight = 50;
            try
            {
                headerHeight = Convert.ToInt32(TxtHeaderHeight.Text);
            }
            catch { }

            bool showFooterOnFirstPage = ChkFooterFirstPage.Checked;
            bool showFooterOnOddPages = ChkFooterOddPages.Checked;
            bool showFooterOnEvenPages = ChkFooterEvenPages.Checked;

            int footerHeight = 50;
            try
            {
                footerHeight = Convert.ToInt32(TxtFooterHeight.Text);
            }
            catch { }

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();

            // header settings
            converter.Options.DisplayHeader = showHeaderOnFirstPage ||
                showHeaderOnOddPages || showHeaderOnEvenPages;
            converter.Header.DisplayOnFirstPage = showHeaderOnFirstPage;
            converter.Header.DisplayOnOddPages = showHeaderOnOddPages;
            converter.Header.DisplayOnEvenPages = showHeaderOnEvenPages;
            converter.Header.Height = headerHeight;

            PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl);
            headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Header.Add(headerHtml);

            // footer settings
            converter.Options.DisplayFooter = showFooterOnFirstPage ||
                showFooterOnOddPages || showFooterOnEvenPages;
            converter.Footer.DisplayOnFirstPage = showFooterOnFirstPage;
            converter.Footer.DisplayOnOddPages = showFooterOnOddPages;
            converter.Footer.DisplayOnEvenPages = showFooterOnEvenPages;
            converter.Footer.Height = footerHeight;

            PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl);
            footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Footer.Add(footerHtml);

            // add page numbering element to the footer
            if (ChkPageNumbering.Checked)
            {
                // page numbers can be added using a PdfTextSection object
                PdfTextSection text = new PdfTextSection(0, 10,
                    "Page: {page_number} of {total_pages}  ",
                    new System.Drawing.Font("Arial", 8));
                text.HorizontalAlign = PdfTextHorizontalAlign.Right;
                converter.Footer.Add(text);
            }

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertUrl(TxtUrl.Text);

            // save pdf document
            doc.Save(Response, false, "Sample.pdf");

            // close pdf document
            doc.Close();
        }
Example #8
0
        public String PDFReport(string modelo, string pIdSolicitante, string pIDGenerado, string fechaSolicitud)
        {
            Response.CacheControl = "no-cache";
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("es-CO");


            //string urlbase = Url.Action("Oficio", "OficioSolicitudNovedades");
            //var fullUrl = this.Url.Action("ConstanciaTipo", "OficioSolicitudNovedades", new { id = id }, "http");
            var fullUrl = this.Url.Action("ConstanciaSAAH", "GeneracionPdfs", new { hogarcodigo = "1111" }, "http");


            //this.Request.Url.Scheme remplazado por "https"

            //string headerUrl = this.Url.Action("HeaderUrl", "GeneracionPdfs", new { IDGenerado = pIDGenerado }, "http");
            //string footerUrl = this.Url.Action("FooterUrl", "GeneracionPdfs", new { IDGenerado = pIDGenerado }, "http");
            string headerUrl = this.Url.Action("HeaderUrl", "GeneracionPdfs", "http");
            string footerUrl = this.Url.Action("FooterUrl", "GeneracionPdfs", "http");

            // CREACION DE PDF CON DLL GNU

            SelectPdf.GlobalProperties.LicenseKey = "1f7k9efg5PXm5+Tm9eTt++X15uT75Of77Ozs7A==";


            string      pdf_page_size = "Letter";
            PdfPageSize pageSize      = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true);

            string             pdf_orientation = "portrait";
            PdfPageOrientation pdfOrientation  = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), pdf_orientation, true);

            int webPageWidth = 1024;

            try
            {
                webPageWidth = System.Convert.ToInt32(webPageWidth);
            }
            catch { }

            int webPageHeight = 0;

            try
            {
                webPageHeight = System.Convert.ToInt32(webPageHeight);
            }
            catch { }

            //         try {
            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();


            converter.Options.SecureProtocol = 0;

            // set converter options
            converter.Options.PdfPageSize        = pageSize;
            converter.Options.PdfPageOrientation = pdfOrientation;
            converter.Options.WebPageWidth       = webPageWidth;
            converter.Options.WebPageHeight      = webPageHeight;
            converter.Options.MarginLeft         = 20;
            //converter.Options.MarginRight = 20;

            // header settings
            converter.Options.DisplayHeader     = true;
            converter.Header.DisplayOnFirstPage = true;
            converter.Header.DisplayOnOddPages  = true;
            converter.Header.DisplayOnEvenPages = true;
            converter.Header.Height             = 80;

            // add some html content to the header
            PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl);

            headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Header.Add(headerHtml);

            // footer settings
            converter.Options.DisplayFooter     = true;
            converter.Footer.DisplayOnFirstPage = true;
            converter.Footer.DisplayOnOddPages  = true;
            converter.Footer.DisplayOnEvenPages = true;
            converter.Footer.Height             = 50;

            // add some html content to the footer

            PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl);

            footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Footer.Add(footerHtml);


            // page numbers can be added using a PdfTextSection object
            //PdfTextSection text = new PdfTextSection(0, 20, "Código Verificación: "+ pIDGenerado.ToString() + " Pag: {page_number} de {total_pages}  Fecha: " + fechaSolicitud  , new System.Drawing.Font("Verdana", 7));
            PdfTextSection text = new PdfTextSection(0, 20, "Pag: {page_number} de {total_pages}               Fecha:  " + fechaSolicitud + "                   Código Verificación: " + pIDGenerado.ToString(), new System.Drawing.Font("Verdana", 7));

            //string cusomtSwitches = string.Format("--header-html {0} --footer-right \"Fecha: {1}\" " + "--footer-left \"Codigo Verificacion: {2}\" " + "--footer-center \"Pag: [page] de [toPage]\" --footer-line --footer-font-size \"9\" --footer-spacing 5 --footer-font-name \"calibri light\" ", Url.Action("Footer", "ConsultaIndividual", new { IDGenerado = pIdGenerado.ToString() }, this.Request.Url.Scheme), fechaSolicitud, pIdGenerado.ToString());
            text.HorizontalAlign = PdfTextHorizontalAlign.Default;
            converter.Footer.Add(text);


            // specify the number of seconds the conversion is delayed
            converter.Options.MinPageLoadTime = 10;

            // set the page timeout (in seconds)
            converter.Options.MaxPageLoadTime = 300;

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertUrl(fullUrl);

            //save the file to server temp folder
            string fileName = "ConstanciaSAAH_" + pIDGenerado + ".pdf";

            string fullPath = ConfigurationManager.AppSettings["RutaArchivoConstancia"] + fileName;

            // save pdf document
            doc.Save(fullPath);

            doc.Close();


            Session["NombrePDF"] = fileName;


            return("1");
        }
Example #9
0
        public ActionResult ConstanciaSAAHE(String hogarcodigo)
        {
            if (hogarcodigo == null)
            {
                hogarcodigo = "8HI49";
            }

            Response.CacheControl = "no-cache";
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("es-CO");
            var    fullUrl   = this.Url.Action("ConstanciaSAAH", "GeneracionPdfs", new { hogarcodigo = hogarcodigo }, "http");
            string headerUrl = this.Url.Action("HeaderUrl", "GeneracionPdfs", null, "http");
            string footerUrl = this.Url.Action("FooterUrl", "GeneracionPdfs", null, "http");


            SelectPdf.GlobalProperties.LicenseKey = "1f7k9efg5PXm5+Tm9eTt++X15uT75Of77Ozs7A==";


            string      pdf_page_size = "Letter";
            PdfPageSize pageSize      = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true);

            string             pdf_orientation = "portrait";
            PdfPageOrientation pdfOrientation  = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), pdf_orientation, true);

            int webPageWidth = 1024;

            try
            {
                webPageWidth = System.Convert.ToInt32(webPageWidth);
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
            }

            int webPageHeight = 0;

            try
            {
                webPageHeight = System.Convert.ToInt32(webPageHeight);
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
            }

            //         try {
            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();


            converter.Options.SecureProtocol = 0;

            // set converter options
            converter.Options.PdfPageSize        = pageSize;
            converter.Options.PdfPageOrientation = pdfOrientation;
            converter.Options.WebPageWidth       = webPageWidth;
            converter.Options.WebPageHeight      = webPageHeight;
            converter.Options.MarginLeft         = 20;
            //converter.Options.MarginRight = 20;

            // header settings
            converter.Options.DisplayHeader     = true;
            converter.Header.DisplayOnFirstPage = true;
            converter.Header.DisplayOnOddPages  = true;
            converter.Header.DisplayOnEvenPages = true;
            converter.Header.Height             = 80;

            // add some html content to the header
            PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl);

            headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Header.Add(headerHtml);

            // footer settings
            converter.Options.DisplayFooter     = true;
            converter.Footer.DisplayOnFirstPage = true;
            converter.Footer.DisplayOnOddPages  = true;
            converter.Footer.DisplayOnEvenPages = true;
            converter.Footer.Height             = 50;

            // add some html content to the footer
            PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl);

            footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Footer.Add(footerHtml);

            PdfTextSection text = new PdfTextSection(0, 20, "Pag: {page_number} de {total_pages}               Fecha:  " + "04/12/194" + "                   Código Verificación: " + "prueba".ToString(), new System.Drawing.Font("Verdana", 7));

            text.HorizontalAlign = PdfTextHorizontalAlign.Default;

            // specify the number of seconds the conversion is delayed
            converter.Options.MinPageLoadTime = 2;/*120*/

            // set the page timeout (in seconds)
            converter.Options.MaxPageLoadTime = 300;

            // create a new pdf document converting an url
            PdfDocument doc = new PdfDocument();

            try {
                doc = converter.ConvertUrl(fullUrl);
                //save the file to server temp folder
                string fileName = "ConstanciaSAAH_" + hogarcodigo + ".pdf";

                string fullPath = ConfigurationManager.AppSettings["RutaArchivoConstancias"] + fileName;

                // save pdf document
                doc.Save(fullPath);

                doc.Close();


                Session["NombrePDF"] = fileName;
            } catch (Exception e) {
                Console.WriteLine(e.Message);
            }

            return(RedirectToAction("Inicio", "ConformacionHogar"));
        }
        protected void BtnCreatePdf_Click(object sender, EventArgs e)
        {
            // get parameters
            string headerUrl = Server.MapPath("~/files/header.html");
            string footerUrl = Server.MapPath("~/files/footer.html");

            bool showHeaderOnFirstPage = ChkHeaderFirstPage.Checked;
            bool showHeaderOnOddPages  = ChkHeaderOddPages.Checked;
            bool showHeaderOnEvenPages = ChkHeaderEvenPages.Checked;

            int headerHeight = 50;

            try
            {
                headerHeight = Convert.ToInt32(TxtHeaderHeight.Text);
            }
            catch { }


            bool showFooterOnFirstPage = ChkFooterFirstPage.Checked;
            bool showFooterOnOddPages  = ChkFooterOddPages.Checked;
            bool showFooterOnEvenPages = ChkFooterEvenPages.Checked;

            int footerHeight = 50;

            try
            {
                footerHeight = Convert.ToInt32(TxtFooterHeight.Text);
            }
            catch { }

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();

            // header settings
            converter.Options.DisplayHeader = showHeaderOnFirstPage ||
                                              showHeaderOnOddPages || showHeaderOnEvenPages;
            converter.Header.DisplayOnFirstPage = showHeaderOnFirstPage;
            converter.Header.DisplayOnOddPages  = showHeaderOnOddPages;
            converter.Header.DisplayOnEvenPages = showHeaderOnEvenPages;
            converter.Header.Height             = headerHeight;

            PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl);

            headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Header.Add(headerHtml);

            // footer settings
            converter.Options.DisplayFooter = showFooterOnFirstPage ||
                                              showFooterOnOddPages || showFooterOnEvenPages;
            converter.Footer.DisplayOnFirstPage = showFooterOnFirstPage;
            converter.Footer.DisplayOnOddPages  = showFooterOnOddPages;
            converter.Footer.DisplayOnEvenPages = showFooterOnEvenPages;
            converter.Footer.Height             = footerHeight;

            PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl);

            footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Footer.Add(footerHtml);

            // add page numbering element to the footer
            if (ChkPageNumbering.Checked)
            {
                // page numbers can be added using a PdfTextSection object
                PdfTextSection text = new PdfTextSection(0, 10,
                                                         "Page: {page_number} of {total_pages}  ",
                                                         new System.Drawing.Font("Arial", 8));
                text.HorizontalAlign = PdfTextHorizontalAlign.Right;
                converter.Footer.Add(text);
            }

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertUrl(TxtUrl.Text);

            // save pdf document
            doc.Save(Response, false, "Sample.pdf");

            // close pdf document
            doc.Close();
        }
Example #11
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                PrintUsage();
            }
            var option         = args[0];
            var inputfilepath  = string.Empty;
            var outputfilepath = string.Empty;
            var xsltPath       = string.Empty;

            FileStream fsIn;
            TextReader textReader;

            AppContext.SetSwitch("Switch.System.Xml.AllowDefaultResolver", true);

            try
            {
                switch (option)
                {
                case "tohtml":

                    if (args.Length != 4)
                    {
                        PrintUsage();
                        break;
                    }
                    Console.WriteLine("Beginning conversion from XML to HTML...");

                    inputfilepath  = args[1];
                    outputfilepath = args[2];
                    xsltPath       = args[3];

                    fsIn       = new FileStream(inputfilepath, FileMode.Open);
                    textReader = new StreamReader(fsIn);

                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.LoadXml(textReader.ReadToEnd());

                    XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();
                    XsltArgumentList     xsltArgumentList     = new XsltArgumentList();

                    XsltSettings settings = new XsltSettings(true, false);

                    FileStream fs     = new FileStream(outputfilepath, FileMode.Create, FileAccess.Write);
                    TextWriter writer = new StreamWriter(fs);

                    xslCompiledTransform.Load(xsltPath, settings, new XmlUrlResolver());
                    xslCompiledTransform.Transform(xmlDocument, xsltArgumentList, writer);

                    fs.Close();

                    Console.WriteLine("Converted to HTML");
                    break;

                case "topdf":
                    if (args.Length != 3)
                    {
                        PrintUsage();
                        break;
                    }
                    Console.WriteLine("Beginning conversion from HTML to PDF...");
                    inputfilepath  = args[1];
                    outputfilepath = args[2];

                    fsIn       = new FileStream(inputfilepath, FileMode.Open);
                    textReader = new StreamReader(fsIn);

                    // instantiate the html to pdf converter
                    HtmlToPdf converter = new HtmlToPdf();

                    converter.Options.PdfPageSize        = PdfPageSize.Letter;
                    converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait;
                    converter.Options.CssMediaType       = HtmlToPdfCssMediaType.Print;

                    converter.Options.MarginLeft   = 10;
                    converter.Options.MarginRight  = 10;
                    converter.Options.MarginTop    = 2;
                    converter.Options.MarginBottom = 2;

                    // header settings
                    converter.Options.DisplayHeader     = true;
                    converter.Header.DisplayOnFirstPage = true;
                    converter.Header.DisplayOnOddPages  = true;
                    converter.Header.DisplayOnEvenPages = true;
                    converter.Header.Height             = 30;

                    // footer settings
                    converter.Options.DisplayFooter     = true;
                    converter.Footer.DisplayOnFirstPage = true;
                    converter.Footer.DisplayOnOddPages  = true;
                    converter.Footer.DisplayOnEvenPages = true;
                    converter.Footer.Height             = 30;

                    // add some html content to the footer
                    PdfHtmlSection headerHtml = new PdfHtmlSection(@"<div style=""text-align: right; width: 100%; font-size: 8pt"">Rendered by/Généré par CanPESC</div>", "");
                    converter.Header.Add(headerHtml);

                    // page numbers can be added using a PdfTextSection object
                    PdfTextSection text = new PdfTextSection(-10, 10, "Page {page_number} of/de {total_pages}  ", new System.Drawing.Font("Arial", 8));
                    text.HorizontalAlign = PdfTextHorizontalAlign.Right;
                    converter.Footer.Add(text);

                    PdfTextSection text2 = new PdfTextSection(10, 10, "This is not an official transcript | Ceci n'est pas une relevée de notes officielle", new System.Drawing.Font("Arial", 8));
                    text2.HorizontalAlign = PdfTextHorizontalAlign.Left;
                    converter.Footer.Add(text2);

                    // convert the url to pdf
                    PdfDocument doc = converter.ConvertHtmlString(textReader.ReadToEnd());

                    // PDF Options - can be configured
                    doc.DocumentInformation.Title  = "College Transcript";
                    doc.DocumentInformation.Author = "CanPESC";

                    // save pdf document
                    doc.Save(outputfilepath);

                    // close pdf document
                    doc.Close();

                    Console.WriteLine("Converted to PDF");
                    break;

                default:
                    PrintUsage();
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");

                PrintUsage();
            }
        }
Example #12
0
        public ActionResult GetInvoiceDetails(int ClientId, DateTime?fromdate, DateTime?todate)
        {
            try
            {
                using (MaxDbEntities db = new MaxDbEntities())
                {
                    var          clientDetails   = db.MaxClient_Details(ClientId, fromdate, todate).ToList();
                    var          clientDueAmount = db.MaxClientDueDetails(ClientId).FirstOrDefault();
                    InvoiceModel invoice         = new InvoiceModel();

                    if (clientDueAmount == null)
                    {
                        invoice.DueAmount = 0;
                    }
                    else
                    {
                        invoice.DueAmount = Math.Round(Convert.ToDecimal(clientDueAmount.DueAmount));
                    }

                    List <InvoiceModel> invoices = new List <InvoiceModel>();

                    foreach (var client in clientDetails)
                    {
                        // InvoiceModel invoice = new InvoiceModel();
                        invoice.Client_Id       = client.Client_Id;
                        invoice.Client_Name     = client.ClientName;
                        invoice.ClientShortName = client.ShortName;
                        invoice.Address         = client.AddressLine1;
                        invoice.ServiceType     = client.ServiceType;
                        invoice.ServiceId       = Convert.ToInt32(client.ClientVertical_Id);
                        invoice.PaymentType     = client.PaymentType;
                        invoice.RowNum          = Convert.ToInt32(client.RowNum);
                        invoice.LineCount       = Convert.ToInt32(client.LineCount);
                        invoice.CountryName     = client.Name;
                        if (client.PaymentType == "Per Unit")
                        {
                            invoice.UnitPrice           = Convert.ToDecimal(client.PaymentAmount);
                            invoice.UnitPriceforDisplay = (client.PaymentAmount).ToString();
                            invoice.Amount = Math.Round(Convert.ToDecimal(client.LineCount * client.PaymentAmount));
                        }
                        if (client.PaymentType == "Fixed")
                        {
                            invoice.UnitPriceforDisplay = "Fixed";
                            invoice.UnitPrice           = Convert.ToDecimal(client.PaymentAmount);
                            invoice.Amount = Math.Round(Convert.ToDecimal(client.PaymentAmount));
                        }

                        invoice.TotalAmount = Math.Round(Convert.ToDecimal(invoice.Amount + invoice.DueAmount));

                        invoice.MonthYear = fromdate.Value.ToString("MMMM yyyy");
                        invoices.Add(invoice);
                    }

                    invoice.InvoiceDate        = fromdate.Value.ToShortDateString();
                    invoice.InvoiceDueDate     = fromdate.Value.AddDays(10).ToShortDateString();
                    invoice.InvoiceCurrentDate = DateTime.Now.ToShortDateString();

                    var InvoiceCount = db.DeletedInvoices.Where(inv => inv.Client_Id == ClientId && inv.InvoiceCreatedDate.Value.Month == fromdate.Value.Month).Count();
                    int count        = Convert.ToInt32(InvoiceCount);

                    invoice.InvoiceNumber = "MAX-" + invoice.Client_Id + (fromdate.Value.Month).ToString().Substring(0, 1) + (fromdate.Value.Year).ToString().Substring(2, 2) + "_" + ++count;

                    string    html      = RenderRazorViewToString("GetInvoiceDetails", invoice);
                    string    baseUrl   = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
                    HtmlToPdf converter = new HtmlToPdf();

                    converter.Options.DisplayFooter = true;
                    string         footerUrl  = Server.MapPath("~/views/home/footer.html");
                    PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl);
                    footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
                    converter.Footer.Add(footerHtml);

                    PdfDocument doc = converter.ConvertHtmlString(html, baseUrl);

                    // save pdf document
                    byte[] pdf = doc.Save();

                    // close pdf document
                    doc.Close();

                    // return resulted pdf document
                    FileResult fileResult = new FileContentResult(pdf, "application/pdf");
                    fileResult.FileDownloadName = invoice.InvoiceNumber + "_" + invoice.ClientShortName + ".pdf";
                    return(fileResult);
                    //return View(invoice);
                }
            }
            catch (Exception ex)
            {
                new Error().logAPIError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString(), ex.StackTrace);
                return(View());
            }
        }
        public IActionResult OnPost()
        {
            // get parameters
            string headerUrl = Startup.ServerPath + @"\wwwroot\files\header.html";
            string footerUrl = Startup.ServerPath + @"\wwwroot\files\footer.html";

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();

            // header settings
            converter.Options.DisplayHeader = showHeaderOnFirstPage ||
                                              showHeaderOnOddPages || showHeaderOnEvenPages;
            converter.Header.DisplayOnFirstPage = showHeaderOnFirstPage;
            converter.Header.DisplayOnOddPages  = showHeaderOnOddPages;
            converter.Header.DisplayOnEvenPages = showHeaderOnEvenPages;
            converter.Header.Height             = headerHeight;

            PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl);

            headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Header.Add(headerHtml);

            // footer settings
            converter.Options.DisplayFooter = showFooterOnFirstPage ||
                                              showFooterOnOddPages || showFooterOnEvenPages;
            converter.Footer.DisplayOnFirstPage = showFooterOnFirstPage;
            converter.Footer.DisplayOnOddPages  = showFooterOnOddPages;
            converter.Footer.DisplayOnEvenPages = showFooterOnEvenPages;
            converter.Footer.Height             = footerHeight;

            PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl);

            footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Footer.Add(footerHtml);

            // add page numbering element to the footer
            if (showPageNumbering)
            {
                // page numbers can be added using a PdfTextSection object
                PdfTextSection text = new PdfTextSection(0, 10,
                                                         "Page: {page_number} of {total_pages}  ",
                                                         new System.Drawing.Font("Arial", 8));
                text.HorizontalAlign = PdfTextHorizontalAlign.Right;
                converter.Footer.Add(text);
            }

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertUrl(TxtUrl);

            // save pdf document
            byte[] pdf = doc.Save();

            // close pdf document
            doc.Close();

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");

            fileResult.FileDownloadName = "Document.pdf";
            return(fileResult);
        }
        public ActionResult SubmitAction(FormCollection collection)
        {
            // get parameters
            string headerUrl             = Server.MapPath("~/files/header.html");
            string footerUrl             = Server.MapPath("~/files/footer.html");
            bool   showHeaderOnFirstPage = collection["ChkHeaderFirstPage"] == "on";
            bool   showHeaderOnOddPages  = collection["ChkHeaderOddPages"] == "on";
            bool   showHeaderOnEvenPages = collection["ChkHeaderEvenPages"] == "on";
            int    headerHeight          = 50;

            try
            {
                headerHeight = Convert.ToInt32(collection["TxtHeaderHeight"]);
            }
            catch { }
            bool showFooterOnFirstPage = collection["ChkFooterFirstPage"] == "on";
            bool showFooterOnOddPages  = collection["ChkFooterOddPages"] == "on";
            bool showFooterOnEvenPages = collection["ChkFooterEvenPages"] == "on";
            int  footerHeight          = 50;

            try
            {
                footerHeight = Convert.ToInt32(collection["TxtFooterHeight"]);
            }
            catch { }
            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();

            // header settings
            converter.Options.DisplayHeader = showHeaderOnFirstPage ||
                                              showHeaderOnOddPages || showHeaderOnEvenPages;
            converter.Header.DisplayOnFirstPage = showHeaderOnFirstPage;
            converter.Header.DisplayOnOddPages  = showHeaderOnOddPages;
            converter.Header.DisplayOnEvenPages = showHeaderOnEvenPages;
            converter.Header.Height             = headerHeight;
            PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl);

            headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Header.Add(headerHtml);
            // footer settings
            converter.Options.DisplayFooter = showFooterOnFirstPage ||
                                              showFooterOnOddPages || showFooterOnEvenPages;
            converter.Footer.DisplayOnFirstPage = showFooterOnFirstPage;
            converter.Footer.DisplayOnOddPages  = showFooterOnOddPages;
            converter.Footer.DisplayOnEvenPages = showFooterOnEvenPages;
            converter.Footer.Height             = footerHeight;
            PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl);

            footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Footer.Add(footerHtml);
            // add page numbering element to the footer
            if (collection["ChkPageNumbering"] == "on")
            {
                // page numbers can be added using a PdfTextSection object
                PdfTextSection text = new PdfTextSection(0, 10,
                                                         "Page: {page_number} of {total_pages}  ",
                                                         new System.Drawing.Font("Arial", 8));
                text.HorizontalAlign = PdfTextHorizontalAlign.Right;
                converter.Footer.Add(text);
            }
            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertUrl(collection["TxtUrl"]);

            // custom header on page 3
            if (doc.Pages.Count >= 3)
            {
                PdfPage     page         = doc.Pages[2];
                PdfTemplate customHeader = doc.AddTemplate(
                    page.PageSize.Width, headerHeight);
                PdfHtmlElement customHtml = new PdfHtmlElement(
                    "<div><b>This is the custom header that will " +
                    "appear only on page 3!</b></div>",
                    string.Empty);
                customHeader.Add(customHtml);
                page.CustomHeader = customHeader;
            }
            // save pdf document
            byte[] pdf = doc.Save();
            // close pdf document
            doc.Close();
            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");

            fileResult.FileDownloadName = "Document.pdf";
            return(fileResult);
        }