Esempio n. 1
0
        public FileContentResult Imprimir(string registros)
        {
            string html = "";

            if (registros == "TabelaAluno")
            {
                html = RenderRazorViewToString("TabelaAluno", new AlunoDal().ObterTodos().ToList());
            }
            if (registros == "TabelaCidade")
            {
                html = RenderRazorViewToString("TabelaCidade", new CidadeDal().ObterTodos().ToList());
            }
            if (registros == "TabelaAlunoCidade")
            {
                html = RenderRazorViewToString("TabelaAlunoCidade", new CidadeDal().ObterTodos().ToList());
            }

            var properties = new ConverterProperties();
            var baseUrl    = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"));

            properties.SetBaseUri(baseUrl);

            MediaDeviceDescription med = new MediaDeviceDescription(MediaType.ALL);

            med.SetOrientation("landscape");
            properties.SetMediaDeviceDescription(med);

            using (var stream = new MemoryStream())
            {
                HtmlConverter.ConvertToPdf(html, stream, properties);

                return(File(stream.ToArray(), "application/pdf"));
            }
        }
Esempio n. 2
0
        public void ManipulatePdf(string htmlSource, string pdfDest, string resourceLoc, PageSize pageSize, float screenWidth)
        {
            PdfWriter   writer = new PdfWriter(pdfDest);
            PdfDocument pdfDoc = new PdfDocument(writer);

            // Set the result to be tagged
            pdfDoc.SetTagged();
            pdfDoc.SetDefaultPageSize(pageSize);

            ConverterProperties converterProperties = new ConverterProperties();

            // Set media device description details
            MediaDeviceDescription mediaDescription = new MediaDeviceDescription(MediaType.SCREEN);

            mediaDescription.SetWidth(screenWidth);
            converterProperties.SetMediaDeviceDescription(mediaDescription);

            FontProvider fp = new DefaultFontProvider();

            // Register external font directory
            fp.AddDirectory(resourceLoc);

            converterProperties.SetFontProvider(fp);
            // Base URI is required to resolve the path to source files
            converterProperties.SetBaseUri(resourceLoc);

            // Create acroforms from text and button input fields
            converterProperties.SetCreateAcroForm(true);

            HtmlConverter.ConvertToPdf(new FileStream(htmlSource, FileMode.Open, FileAccess.Read, FileShare.Read),
                                       pdfDoc, converterProperties);

            pdfDoc.Close();
        }
Esempio n. 3
0
        /// <summary>
        /// Creates the PDF file.
        /// </summary>
        /// <param name="baseUri">the base URI</param>
        /// <param name="src">the path to the source HTML file</param>
        /// <param name="dest">the path to the resulting PDF</param>
        public void CreatePdf(String baseUri, String src, String dest)
        {
            ConverterProperties properties = new ConverterProperties();

            properties.SetBaseUri(baseUri);
            MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.PRINT);

            properties.SetMediaDeviceDescription(mediaDeviceDescription);
            HtmlConverter.ConvertToPdf(new FileStream(src, FileMode.Open, FileAccess.Read),
                                       new FileStream(dest, FileMode.Create), properties);
        }
        /// <summary>
        /// Creates the PDF file.
        /// </summary>
        /// <param name="url">the URL object for the web page</param>
        /// <param name="dest">the path to the resulting PDF</param>
        public void CreatePdf(Uri url, String dest)
        {
            ConverterProperties    properties             = new ConverterProperties();
            MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.PRINT);

            properties.SetMediaDeviceDescription(mediaDeviceDescription);
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            var httpResponse   = (HttpWebResponse)httpWebRequest.GetResponse();

            HtmlConverter.ConvertToPdf(httpResponse.GetResponseStream(),
                                       new FileStream(dest, FileMode.Create), properties);
        }
Esempio n. 5
0
        public void ManipulatePdf(string htmlSource, string pdfDest, string resourceLoc)
        {
            // Base URI is required to resolve the path to source files
            ConverterProperties converterProperties = new ConverterProperties().SetBaseUri(resourceLoc);

            // Set media device type to correctly parsing html with media handling
            converterProperties.SetMediaDeviceDescription(new MediaDeviceDescription(MediaType.PRINT));

            HtmlConverter.ConvertToPdf(
                new FileStream(htmlSource, FileMode.Open),
                new FileStream(pdfDest, FileMode.Create, FileAccess.Write),
                converterProperties);
        }
Esempio n. 6
0
        public void createPdf(String src, String dest)
        {
            ConverterProperties properties = new ConverterProperties();

            properties.SetBaseUri(css);
            //FontProvider dfp = new DefaultFontProvider();
            CssStyleCollection style = new Panel().Style;



            MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.PRINT);

            properties.SetMediaDeviceDescription(mediaDeviceDescription);
            HtmlConverter.ConvertToPdf(new FileStream(src, FileMode.Open), new FileStream(dest, FileMode.Create), properties);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates the PDF file.
        /// </summary>
        /// <param name="url">the URL object for the web page</param>
        /// <param name="dest">the path to the resulting PDF</param>
        public void CreatePdf(Uri url, String dest)
        {
            PdfWriter   writer   = new PdfWriter(dest);
            PdfDocument pdf      = new PdfDocument(writer);
            PageSize    pageSize = new PageSize(850, 1700);

            pdf.SetDefaultPageSize(pageSize);
            ConverterProperties    properties             = new ConverterProperties();
            MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.SCREEN);

            mediaDeviceDescription.SetWidth(pageSize.GetWidth());
            properties.SetMediaDeviceDescription(mediaDeviceDescription);
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            var httpResponse   = (HttpWebResponse)httpWebRequest.GetResponse();

            HtmlConverter.ConvertToPdf(httpResponse.GetResponseStream(), pdf, properties);
        }
Esempio n. 8
0
        /// <summary>
        /// Creates the PDF file.
        /// </summary>
        /// <param name="baseUri">the base URI</param>
        /// <param name="src">the path to the source HTML file</param>
        /// <param name="dest">the path to the resulting PDF</param>
        public void CreatePdf(String baseUri, String src, String dest)
        {
            PdfWriter   writer = new PdfWriter(dest);
            PdfDocument pdf    = new PdfDocument(writer);

            pdf.SetTagged();
            PageSize pageSize = new PageSize(440, 2000);

            pdf.SetDefaultPageSize(pageSize);
            ConverterProperties properties = new ConverterProperties();

            properties.SetBaseUri(baseUri);
            MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.SCREEN);

            mediaDeviceDescription.SetWidth(pageSize.GetWidth());
            properties.SetMediaDeviceDescription(mediaDeviceDescription);
            HtmlConverter.ConvertToPdf(new FileStream(src, FileMode.Open, FileAccess.Read), pdf, properties);
        }
Esempio n. 9
0
        public byte[] Convert(string htmlContent)
        {
            byte[] pdf = null;
            using (var memoryStream = new MemoryStream())
            {
                var writterProperties = new WriterProperties()
                                        .SetFullCompressionMode(true);
                using (PdfWriter writer = new PdfWriter(memoryStream, writterProperties))
                {
                    PdfDocument pdfDoc = new PdfDocument(writer);
                    pdfDoc.SetTagged();

                    PageSize pageSize = new PageSize(900, 1000);
                    pdfDoc.SetDefaultPageSize(pageSize);

                    ConverterProperties converterProperties = new ConverterProperties();
                    converterProperties.SetCreateAcroForm(true);

                    var fp = new DefaultFontProvider(true, false, false);
                    converterProperties.SetFontProvider(fp);

                    MediaDeviceDescription mediaDescription = new MediaDeviceDescription(MediaType.SCREEN);
                    converterProperties.SetMediaDeviceDescription(mediaDescription);

                    var                elements        = HtmlConverter.ConvertToElements(htmlContent, converterProperties);
                    Document           document        = new Document(pdfDoc);
                    CJKSplitCharacters splitCharacters = new CJKSplitCharacters();
                    document.SetSplitCharacters(splitCharacters);
                    document.SetProperty(Property.SPLIT_CHARACTERS, splitCharacters);
                    foreach (IElement element in elements)
                    {
                        document.Add((IBlockElement)element);
                    }
                    document.Close();

                    pdf = memoryStream.ToArray();

                    memoryStream.Close();
                    pdfDoc.Close();
                }
            }

            return(pdf);
        }
Esempio n. 10
0
        public virtual void ResponsiveIText()
        {
            PageSize[] pageSizes = new PageSize[] { null, new PageSize(PageSize.A3.GetHeight(), PageSize.A4.GetHeight(
                                                                           )), new PageSize(760, PageSize.A4.GetHeight()), new PageSize(PageSize.A5.GetWidth(), PageSize.A4.GetHeight
                                                                                                                                            ()) };
            String htmlSource = sourceFolder + "responsiveIText.html";

            foreach (PageSize pageSize in pageSizes)
            {
                float?pxWidth = null;
                if (pageSize != null)
                {
                    pxWidth = CssUtils.ParseAbsoluteLength(pageSize.GetWidth().ToString());
                }
                String              outName             = "responsiveIText" + (pxWidth != null ? "_" + (int)(float)pxWidth : "") + ".pdf";
                PdfWriter           writer              = new PdfWriter(destinationFolder + outName);
                PdfDocument         pdfDoc              = new PdfDocument(writer);
                ConverterProperties converterProperties = new ConverterProperties();
                if (pageSize != null)
                {
                    pdfDoc.SetDefaultPageSize(pageSize);
                    MediaDeviceDescription mediaDescription = new MediaDeviceDescription(MediaType.SCREEN);
                    mediaDescription.SetWidth((float)pxWidth);
                    converterProperties.SetMediaDeviceDescription(mediaDescription);
                }
                using (FileStream fileInputStream = new FileStream(htmlSource, FileMode.Open, FileAccess.Read)) {
                    HtmlConverter.ConvertToPdf(fileInputStream, pdfDoc, converterProperties);
                }
                pdfDoc.Close();
            }
            foreach (PageSize pageSize in pageSizes)
            {
                float?pxWidth = null;
                if (pageSize != null)
                {
                    pxWidth = CssUtils.ParseAbsoluteLength(pageSize.GetWidth().ToString());
                }
                String outName = "responsiveIText" + (pxWidth != null ? "_" + (int)(float)pxWidth : "") + ".pdf";
                String cmpName = "cmp_" + outName;
                NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + outName, sourceFolder
                                                                                 + cmpName, destinationFolder, "diffResponsive_"));
            }
        }
Esempio n. 11
0
        public byte[] GenerateHTML(string html)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                PdfWriter   writer = new PdfWriter(ms);//@"D:\Docs\demp.pdf"
                PdfDocument pdfDoc = new PdfDocument(writer);

                // Set the result to be tagged
                pdfDoc.SetTagged();
                pdfDoc.SetDefaultPageSize(PageSize.A4);

                ConverterProperties converterProperties = new ConverterProperties();

                // Set media device description details
                MediaDeviceDescription mediaDescription = new MediaDeviceDescription(MediaType.SCREEN);
                //mediaDescription.SetWidth(screenWidth);
                converterProperties.SetMediaDeviceDescription(mediaDescription);

                FontProvider fp = new DefaultFontProvider();

                // Register external font directory
                ////fp.AddDirectory(resourceLoc);

                ////converterProperties.SetFontProvider(fp);
                // Base URI is required to resolve the path to source files
                ////converterProperties.SetBaseUri(resourceLoc);

                // Create acroforms from text and button input fields
                converterProperties.SetCreateAcroForm(true);

                // HtmlConverter.ConvertToPdf(html, pdfDoc, converterProperties);
                HtmlConverter.ConvertToPdf(html, pdfDoc, converterProperties);

                pdfDoc.Close();
                return(ms.ToArray());
            }
        }
Esempio n. 12
0
        public FileResult M15()
        {
            var client = new WebClient();

            client.Encoding = Encoding.UTF8;
            var p = MvcHelpers.RenderViewToString(this.ControllerContext, "M15View", db.Orders.First());

            var workStream = new MemoryStream();

            using (var writer = new PdfWriter(workStream))
            {
                PdfDocument pdf = new PdfDocument(writer);
                pdf.SetTagged();
                PageSize pageSize = PageSize.A4.Rotate();
                pageSize.IncreaseHeight(4);
                pdf.SetDefaultPageSize(pageSize);
                ConverterProperties properties = new ConverterProperties();
                properties.SetMediaDeviceDescription(new MediaDeviceDescription(MediaType.PRINT));
                writer.SetCloseStream(false);
                using (var document = HtmlConverter.ConvertToDocument(p, pdf, properties)) { }
            }
            workStream.Position = 0;
            return(File(workStream, "application/pdf"));
        }