Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Web;
- /// Autor: Raphael A. F. Cardoso - www.csharpbrasil.com.br
- namespace Br.CsharpBrasil
- {
- public class PdfEngine
- {
- private iTextSharp.text.Rectangle rectangle = iTextSharp.text.PageSize.A4;
- #region Enumeration
- public enum Orientation
- {
- Portrait = 0,
- Landscape = 0
- }
- public enum PageSize
- {
- A4 = 0,
- LETTER = 1
- }
- #endregion
- #region Constructor
- public PdfEngine()
- {
- this.rectangle = new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.A4);
- }
- public PdfEngine(PageSize pageSize)
- {
- var pSize = getPageSize(pageSize);
- this.rectangle = new iTextSharp.text.Rectangle(pSize);
- }
- public PdfEngine(Orientation orientation)
- {
- this.rectangle = new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.A4);
- if (orientation == Orientation.Landscape) this.rectangle = this.rectangle.Rotate();
- }
- public PdfEngine(Orientation orientation, PageSize pageSize)
- {
- var pSize = getPageSize(pageSize);
- this.rectangle = new iTextSharp.text.Rectangle(pSize);
- if (orientation == Orientation.Landscape) this.rectangle = this.rectangle.Rotate();
- }
- #endregion
- #region Private Methods
- private iTextSharp.text.Rectangle getPageSize(PageSize pageSize)
- {
- var pSize = iTextSharp.text.PageSize.A4;
- switch (pageSize)
- {
- case PageSize.A4:
- pSize = iTextSharp.text.PageSize.A4;
- break;
- case PageSize.LETTER:
- pSize = iTextSharp.text.PageSize.LETTER;
- break;
- default:
- pSize = iTextSharp.text.PageSize.A4;
- break;
- }
- return pSize;
- }
- private string getImage(string input)
- {
- if (input == null) return string.Empty;
- string tempInput = input;
- string pattern = @"<img(.|\n)+?>";
- string src = string.Empty;
- HttpContext context = HttpContext.Current;
- foreach (System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(input, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.RightToLeft))
- {
- if (m.Success)
- {
- string tempM = m.Value;
- string pattern1 = "src=[\'|\"](.+?)[\'|\"]";
- System.Text.RegularExpressions.Regex reImg = new System.Text.RegularExpressions.Regex(pattern1, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);
- System.Text.RegularExpressions.Match mImg = reImg.Match(m.Value);
- if (mImg.Success)
- {
- src = mImg.Value.ToLower().Replace("src=", "").Replace("\"", "");
- if (src.ToLower().Contains("http://") == false)
- {
- src = "src=\"" + context.Request.Url.Scheme + "://" + System.IO.Path.Combine(context.Request.Url.Authority, src).Replace("\\", "/") + "\"";
- try
- {
- tempM = tempM.Remove(mImg.Index, mImg.Length);
- tempM = tempM.Insert(mImg.Index, src);
- tempInput = tempInput.Remove(m.Index, m.Length);
- tempInput = tempInput.Insert(m.Index, tempM);
- }
- catch (Exception e)
- {
- }
- }
- }
- }
- }
- return tempInput;
- }
- private string getHtmlSourceCode(string htmlPath)
- {
- string htmlText = string.Empty;
- if (htmlPath.Contains("http://") || htmlPath.Contains("https://"))
- {
- HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
- HtmlAgilityPack.HtmlDocument html = web.Load(htmlPath);
- html.OptionOutputAsXml = true;
- htmlText = html.DocumentNode.InnerHtml;
- }
- else
- {
- using (var htmlStream = System.IO.File.OpenText(htmlPath))
- {
- htmlText = htmlStream.ReadToEnd();
- htmlStream.Close();
- }
- }
- return htmlText;
- }
- private string getCssSourceCode(string cssPath)
- {
- string cssText = string.Empty;
- if (!string.IsNullOrEmpty(cssPath))
- {
- using (var cssStream = System.IO.File.OpenText(cssPath))
- {
- cssText = cssStream.ReadToEnd();
- cssStream.Close();
- }
- }
- return cssText;
- }
- #endregion
- #region Public Methods
- public void CreatePDF(string htmlFilePath, string pdfFilePath, string cssFilePath)
- {
- byte[] pdf = new byte[0];
- if (!string.IsNullOrEmpty(pdfFilePath))
- {
- pdf = CreatePDF(htmlFilePath, cssFilePath);
- using (var pdfStream = new System.IO.FileStream(pdfFilePath, System.IO.FileMode.OpenOrCreate))
- {
- pdfStream.Write(pdf, 0, pdf.Length);
- pdfStream.Close();
- }
- }
- }
- public byte[] CreatePDF(string htmlFilePath, string cssFilePath)
- {
- if (string.IsNullOrEmpty(htmlFilePath)) return new byte[0];
- byte[] pdf = new byte[0];
- string htmlText = getHtmlSourceCode(htmlFilePath);
- string cssText = getCssSourceCode(cssFilePath);
- if (!string.IsNullOrEmpty(htmlText))
- {
- htmlText = getImage(htmlText);
- System.IO.MemoryStream msHtml = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlText));
- System.IO.MemoryStream msCss = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText));
- using (var pdfStream = new System.IO.MemoryStream())
- {
- using (iTextSharp.text.Document doc = new iTextSharp.text.Document(this.rectangle))
- {
- using (var pdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, pdfStream))
- {
- doc.Open();
- if (msCss.Length == 0)
- {
- iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, doc, msHtml, System.Text.Encoding.UTF8);
- }
- else
- {
- iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, doc, msHtml, msCss, System.Text.Encoding.UTF8);
- }
- doc.Close();
- pdfWriter.Close();
- }
- }
- pdf = pdfStream.ToArray();
- }
- }
- return pdf;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement