HTMLExportUtils.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Threading;
  6. using FastReport.Utils;
  7. namespace FastReport.Export.Html
  8. {
  9. /// <summary>
  10. /// Represents the HTML export format enum
  11. /// </summary>
  12. public enum HTMLExportFormat
  13. {
  14. /// <summary>
  15. /// Represents the message-HTML type
  16. /// </summary>
  17. MessageHTML,
  18. /// <summary>
  19. /// Represents the HTML type
  20. /// </summary>
  21. HTML
  22. }
  23. /// <summary>
  24. /// Specifies the image format in HTML export.
  25. /// </summary>
  26. public enum ImageFormat
  27. {
  28. /// <summary>
  29. /// Specifies the .bmp format.
  30. /// </summary>
  31. Bmp,
  32. /// <summary>
  33. /// Specifies the .png format.
  34. /// </summary>
  35. Png,
  36. /// <summary>
  37. /// Specifies the .jpg format.
  38. /// </summary>
  39. Jpeg,
  40. /// <summary>
  41. /// Specifies the .gif format.
  42. /// </summary>
  43. Gif
  44. }
  45. /// <summary>
  46. /// Specifies the units of HTML sizes.
  47. /// </summary>
  48. public enum HtmlSizeUnits
  49. {
  50. /// <summary>
  51. /// Specifies the pixel units.
  52. /// </summary>
  53. Pixel,
  54. /// <summary>
  55. /// Specifies the percent units.
  56. /// </summary>
  57. Percent
  58. }
  59. public partial class HTMLExport : ExportBase
  60. {
  61. private static string Px(double pixel)
  62. {
  63. return String.Join(String.Empty, new String[] { ExportUtils.FloatToString(pixel), "px;" });
  64. }
  65. private string SizeValue(double value, double maxvalue, HtmlSizeUnits units)
  66. {
  67. FastString sb = new FastString(6);
  68. if (units == HtmlSizeUnits.Pixel)
  69. sb.Append(Px(value));
  70. else if (units == HtmlSizeUnits.Percent)
  71. sb.Append(((int)Math.Round((value * 100 / maxvalue))).ToString()).Append("%");
  72. else
  73. sb.Append(value.ToString());
  74. return sb.ToString();
  75. }
  76. private void WriteMimePart(Stream stream, string mimetype, string charset, string filename)
  77. {
  78. FastString sb = new FastString();
  79. sb.Append("--").AppendLine(boundary);
  80. sb.Append("Content-Type: ").Append(mimetype).Append(";");
  81. if (charset != String.Empty)
  82. sb.Append(" charset=\"").Append(charset).AppendLine("\"");
  83. else
  84. sb.AppendLine();
  85. string body;
  86. byte[] buff = new byte[stream.Length];
  87. stream.Position = 0;
  88. stream.Read(buff, 0, buff.Length);
  89. if (mimetype == "text/html")
  90. {
  91. sb.AppendLine("Content-Transfer-Encoding: quoted-printable");
  92. body = ExportUtils.QuotedPrintable(buff);
  93. }
  94. else
  95. {
  96. sb.AppendLine("Content-Transfer-Encoding: base64");
  97. body = System.Convert.ToBase64String(buff, Base64FormattingOptions.InsertLineBreaks);
  98. }
  99. sb.Append("Content-Location: ").AppendLine(ExportUtils.HtmlURL(filename));
  100. sb.AppendLine();
  101. sb.AppendLine(body);
  102. sb.AppendLine();
  103. Stream.Write(Encoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length);
  104. sb.Clear();
  105. }
  106. private void WriteMHTHeader(Stream Stream, string FileName)
  107. {
  108. FastString sb = new FastString(256);
  109. string s = "=?utf-8?B?" + System.Convert.ToBase64String(Encoding.UTF8.GetBytes(FileName)) + "?=";
  110. sb.Append("From: ").AppendLine(s);
  111. sb.Append("Subject: ").AppendLine(s);
  112. sb.Append("Date: ").AppendLine(ExportUtils.GetRFCDate(SystemFake.DateTime.Now));
  113. sb.AppendLine("MIME-Version: 1.0");
  114. sb.Append("Content-Type: multipart/related; type=\"text/html\"; boundary=\"").Append(boundary).AppendLine("\"");
  115. sb.AppendLine();
  116. sb.AppendLine("This is a multi-part message in MIME format.");
  117. sb.AppendLine();
  118. ExportUtils.Write(Stream, sb.ToString());
  119. sb.Clear();
  120. }
  121. }
  122. /// <summary>
  123. /// For internal use only.
  124. /// </summary>
  125. public class HTMLPageData
  126. {
  127. private string cssText;
  128. private string pageText;
  129. private List<Stream> pictures;
  130. private List<string> guids;
  131. private ManualResetEvent pageEvent;
  132. private int pageNumber;
  133. private float width;
  134. private float height;
  135. /// <summary>
  136. /// For internal use only.
  137. /// </summary>
  138. public float Width
  139. {
  140. get { return width; }
  141. set { width = value; }
  142. }
  143. /// <summary>
  144. /// For internal use only.
  145. /// </summary>
  146. public float Height
  147. {
  148. get { return height; }
  149. set { height = value; }
  150. }
  151. /// <summary>
  152. /// For internal use only.
  153. /// </summary>
  154. public string CSSText
  155. {
  156. get { return cssText; }
  157. set { cssText = value; }
  158. }
  159. /// <summary>
  160. /// For internal use only.
  161. /// </summary>
  162. public string PageText
  163. {
  164. get { return pageText; }
  165. set { pageText = value; }
  166. }
  167. /// <summary>
  168. /// For internal use only.
  169. /// </summary>
  170. public List<Stream> Pictures
  171. {
  172. get { return pictures; }
  173. }
  174. /// <summary>
  175. /// For internal use only.
  176. /// </summary>
  177. public List<string> Guids
  178. {
  179. get { return guids; }
  180. }
  181. /// <summary>
  182. /// For internal use only.
  183. /// </summary>
  184. public ManualResetEvent PageEvent
  185. {
  186. get { return pageEvent; }
  187. }
  188. /// <summary>
  189. /// For internal use only.
  190. /// </summary>
  191. public int PageNumber
  192. {
  193. get { return pageNumber; }
  194. set { pageNumber = value; }
  195. }
  196. /// <summary>
  197. /// For internal use only.
  198. /// </summary>
  199. public HTMLPageData()
  200. {
  201. pictures = new List<Stream>();
  202. guids = new List<string>();
  203. pageEvent = new ManualResetEvent(false);
  204. }
  205. }
  206. }