pu

Buscar este blog

Mostrando entradas con la etiqueta html to pdf. Mostrar todas las entradas
Mostrando entradas con la etiqueta html to pdf. Mostrar todas las entradas

martes, 11 de septiembre de 2018

Crear PDF de html C# .net | create pdf from html C# .net

aqui dejo este codigo de mierda para gente de mierda que quiere ahorrarse tiempo. (dale un click a la publicidad asi me ayudasss ;)

code

using System.IO;
using System.Xml;
using iTextSharp.text; (se baja de paquetes nuget)
using iTextSharp.text.pdf; (se baja de paquetes nuget)

private static bool GenerarPdfFromHtml(string html, string css, string pathArchivo, string orientacionPagina, string tipoPortal)
        {
            bool resp = false;
            try
            {
                string path = System.IO.Path.GetDirectoryName(pathArchivo);
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);


                using (FileStream fs = new FileStream(pathArchivo, FileMode.Create))
                {
                    //using (TextReader reader = new StringReader(html))
                    {

                        using (Document document = new Document(iTextSharp.text.PageSize.LETTER, 36, 36, 70, 70))
                        //using (Document document = new Document(iTextSharp.text.PageSize.A4, 36, 36, 50, 70))
                        {
                            using (PdfWriter writer = PdfWriter.GetInstance(document, fs))
                            {
                                if (orientacionPagina.Equals("H"))
                                    document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());

                                document.Open();

                                using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(css)))
                                {
                                    using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
                                    {
                                        //Parse the HTML
                                        iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, msHtml, msCss);
                                    }
                                }

                                document.Close();
                                fs.Close();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return resp;
        }